问题描述
因此,我在下面有一个基本数据框,其中包含用逗号分隔的长字符串。我使用了Tidyr的 separate创建新列。
So I have the basic dataframe below which contains long strings separated by a comma.I used Tidyr's "separate" to create new columns.
我如何添加另一个新列,计算包含答案的每个人有多少新列? (无NA)。
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?
任何帮助将不胜感激。我想留在Tidyverse和dplyr中。
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=",")
推荐答案
我们可以尝试使用 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
这篇关于使用Tidyr的“ separate”分隔字符串。分成多个列,然后创建一个带有计数的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!