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

santoku picture santoku · Apr 4, 2017 · Viewed 14.3k times · Source

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.

Answer

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 = ""))