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

时间:2022-02-11 21:38:41

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

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)

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

1 个解决方案

#1


19  

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

正如agstudy在loop修复之前暗示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)
}

#1


19  

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

正如agstudy在loop修复之前暗示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)
}