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

AWE*_*AWE 29 string for-loop r strsplit

这有效

x <- "0.466:1.187:2.216:1.196"
y <- as.numeric(unlist(strsplit(x, ":")))
Run Code Online (Sandbox Code Playgroud)

blat$LRwAvg所有的值都X如上所示,但这不起作用

for (i in 1:50){
  y <- as.numeric(unlist(strsplit(blat$LRwAvg[i], "\\:")))
  blat$meanLRwAvg[i]=mean(y)
}
Run Code Online (Sandbox Code Playgroud)

因为:

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

如果我有一个,两个或空反斜杠并不重要.

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

AWE*_*AWE 33

由于agstudy隐含blat$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)
}
Run Code Online (Sandbox Code Playgroud)