Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Error: expecting a single value what does it mean?

I'm doing a simple operation using dplyr in R and got 'expecting single value' error

test <- data.frame(a=rep("item",3),b=c("step1","step2","step3"))
test%>%group_by(a)%>%(summarize(seq=paste0(b))

I've seen similar threads but those use cases were more complex, and I couldn't figure out why these 2 lines don't work.

like image 556
santoku Avatar asked Mar 10 '23 10:03

santoku


1 Answers

Since you only have one group ("item") the paste0 will get a vector of the three items in b as input and will return a vector of three strings, but your summarize is expecting a single value (since there is only one group). You need to collapse the paste0 to a single string like this:

library(dplyr)
test <- data.frame(a=rep("item",3), b=c("step1","step2","step3"))
test %>% group_by(a) %>% summarize(seq = paste0(b, collapse = ""))
like image 57
Kristoffer Winther Balling Avatar answered Mar 12 '23 14:03

Kristoffer Winther Balling