So I have the basic dataframe below which contains long strings separated by a comma.I used Tidyr's "separate" to create new columns.
How do I add another new column with counts of how many new columns there are for each person that contain an answer? (no NA's).
I suppose the columns can be counted after being separated, or before, by counting how many string elements there are that are separated by a comma?
Any help would be appreciated. I would like to stay within the Tidyverse and dplyr.
Name<-c("John","Chris","Andy") Goal<-c("Go back to school,Learn to drive,Learn to cook","Go back to school,Get a job,Learn a new Skill,Learn to cook","Learn to drive,Learn to Cook") df<-data_frame(Name,Goal) df<-df%>%separate(Goal,c("Goal1","Goal2","Goal3","Goal4"),sep=",")Answer1:
We can try with
library(stringr) df %>% separate(Goal,paste0("Goal", 1:4), sep=",", remove=FALSE) %>% mutate(Count = str_count(Goal, ",")+1) %>% select(-Goal) # Name Goal1 Goal2 Goal3 Goal4 Count # <chr> <chr> <chr> <chr> <chr> <dbl> #1 John Go back to school Learn to drive Learn to cook <NA> 3 #2 Chris Go back to school Get a job Learn a new Skill Learn to cook 4 #3 Andy Learn to drive Learn to Cook <NA> <NA> 2