string R字符串拆分函数中的非字符参数(strsplit)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15430016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:51:13  来源:igfitidea点击:

Non character argument in R string split function (strsplit)

stringrfor-loopstrsplit

提问by AWE

This works

这有效

x <- "0.466:1.187:2.216:1.196"
y <- as.numeric(unlist(strsplit(x, ":")))

Values of blat$LRwAvgall look like Xabove but this doesn't work

blat$LRwAvg所有的值都像X上面一样,但这不起作用

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

strsplit(blat$LRwAvg[i], "\:") 中的错误:非字符参数

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)

我的问题是什么?(不是一般的,我的意思是在这个特殊任务中,技术上)

回答by AWE

As agstudy implied blat$LRwAvg <- as.character(blat$LRwAvg)before loop fixed it

正如 agstudyblat$LRwAvg <- as.character(blat$LRwAvg)在循环修复它之前所暗示的那样

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)
}