Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non character argument in R string split function (strsplit)

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)

like image 668
AWE Avatar asked Mar 15 '13 10:03

AWE


People also ask

What is the code used to break apart a string using the Strsplit () function?

Strsplit() Function SyntaxStrsplit(): An R Language function which is used to split the strings into substrings with split arguments. Where: X = input data file, vector or a stings. Split = Splits the strings into required formats.

How does string split work in R?

R split string To split a string in R, use the strsplit() method. The strsplit() is a built-in R function that splits the string vector into sub-strings. The strsplit() method returns the list, where each list item resembles the item of input that has been split.


1 Answers

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) } 
like image 156
AWE Avatar answered Sep 21 '22 08:09

AWE