【问题标题】:Separate a String using Tidyr's "separate" into Multiple Columns and then Create a New Column with Counts使用 Tidyr 的“分离”将一个字符串分隔成多个列,然后创建一个带有计数的新列
【发布时间】:2016-11-16 06:00:56
【问题描述】:

所以我有下面的基本数据框,其中包含用逗号分隔的长字符串。我使用 Tidyr 的“单独”来创建新列。

如何添加另一个新列,其中计算每个人有多少个包含答案的新列? (没有 NA)。

我想可以在分隔之后或之前计算列,通过计算有多少由逗号分隔的字符串元素?

任何帮助将不胜感激。我想留在 Tidyverse 和 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=",")

【问题讨论】:

    标签: r dplyr tidyr stringr


    【解决方案1】:

    我们可以试试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
    

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 2011-12-26
      • 2021-10-25
      • 2020-01-15
      • 2021-04-01
      • 2018-09-27
      • 2019-11-07
      • 1970-01-01
      • 2020-10-06
      相关资源
      最近更新 更多