R字符串拆分函数中的非字符参数(strsplit) [英] Non character argument in R string split function (strsplit)
本文介绍了R字符串拆分函数中的非字符参数(strsplit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
x < - 0.466:1.187:2.216:1.196
y< - as .numeric(unlist(strsplit(x,:)))
blat $ LRwAvg
全部看起来像 X
以上,但是这不起作用
<$ (1:50){
y< - as.numeric(unlist(strsplit(blat $ LRwAvg [i],\\:)) )
blat $ meanLRwAvg [i] = mean(y)
}
因为:
$ b
strsplit错误(blat $ LRwAvg [i],\:):非字符参数
如果我有一个,两个或空的反斜杠,这并不重要。
我的问题是什么? (不是一般,我的意思是在这个特殊的任务,技术上)
解决方案
由于agstudy暗示 blat $ LRwAvg < - as.character(blat $ LRwAvg)
在循环之前修复它
blat $ meanLRwAvg< ; - blat $ gtFrqAvg#或数据框中的其他一些等长的变量
blat $ LRwAvg< - as.character(blat $ LRwAvg)
for(i in 1:50){$ b $通过< - as.numeric(unlist(strsplit(blat $ LRwAvg [i],\\:)))
blat $ meanLRwAvg [i] = mean(y)
}
This works
x <- "0.466:1.187:2.216:1.196"
y <- as.numeric(unlist(strsplit(x, ":")))
Values of blat$LRwAvg
all look like X
above but this doesn't work
for (i in 1:50){
y <- as.numeric(unlist(strsplit(blat$LRwAvg[i], "\\:")))
blat$meanLRwAvg[i]=mean(y)
}
Because of:
Error in strsplit(blat$LRwAvg[i], "\:") : non-character argument
It doesn't matter if I have one, two or null backslashes.
What's my problem? (Not generally, I mean in this special task, technically)
解决方案
As agstudy implied blat$LRwAvg <- as.character(blat$LRwAvg)
before loop fixed it
blat$meanLRwAvg <- blat$gtFrqAvg #or some other variable in data frame with equal length
blat$LRwAvg <- as.character(blat$LRwAvg)
for (i in 1:50){
y <- as.numeric(unlist(strsplit(blat$LRwAvg[i], "\\:")))
blat$meanLRwAvg[i]=mean(y)
}
这篇关于R字符串拆分函数中的非字符参数(strsplit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文