Separate a String using Tidyr's “separate” into Multiple Columns and then Create a New Column with Counts

十年热恋 提交于 2019-12-08 10:06:55

问题


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

回答1:


We can try with str_count

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


来源:https://stackoverflow.com/questions/40625071/separate-a-string-using-tidyrs-separate-into-multiple-columns-and-then-create

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!