Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: String must be an atomic vector R

Tags:

r

vector

I am new to R, and I am having trouble using stringr on a dataset. I am trying to subset the data by variables with the word restaurant in them against those with out. I constantly get this error message.

str_detect(matrix(expandedDataFrame[1,12:21],1,
                 ncol(expandedDataFrame[,12:21])),"Restaurants")
Error: String must be an atomic vector
like image 992
SJSU2013 Avatar asked Dec 20 '22 21:12

SJSU2013


1 Answers

You get an error because str_detect is expecting an atomic type as first argument. I guess that if you use as.matrix and not matrix your code will work.

Indeed:

is.atomic(matrix(data.frame(c=1:10,c1=5:1)))
[1] FALSE
> is.atomic(as.matrix(data.frame(c=1:10,c1=5:1)))
[1] TRUE

So your code becomes:

str_detect(as.matrix(expandedDataFrame[1,12:21],1,
                 ncol(expandedDataFrame[,12:21])),"Restaurants")

But since you don't give a reproducible example, this is just a guess....

like image 121
agstudy Avatar answered Dec 22 '22 11:12

agstudy