【问题标题】:Extract parameters from a string and create other factor or logical variables [duplicate]从字符串中提取参数并创建其他因素或逻辑变量
【发布时间】:2022-01-05 04:40:28
【问题描述】:

如果我不能把问题说清楚,我很抱歉。我很难形成搜索关键字/短语,所以我来了。

所以,我目前有一个类似这样的数据框:

song.title <- c("A", "B", "C", "D")
genre <- c("rock", "pop, rock", "jazz, pop, funk", "funk, rock")
df <- data.frame(song.title, genre)
view(df)

然后,我需要将数据框转换成这样的:

song.title <- c("A", "B", "C", "D")
is.pop <- c("no", "yes", "yes", "no")
is.rock <- c("yes", "yes", "no", "yes")
is.jazz <- c("no", "no", "yes", "no")
is.funk <- c("no", "no", "yes", "yes")
df_needed <- data.frame(song.title, is.pop, is.rock, is.jazz, is.funk)
view(df_needed)

我正在处理的actual data 有 10.000 多行,因此很难确定有多少因素(流派/标签)应该“转换”为列。在 R 中转换此类数据有哪些选择?谢谢。

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    这是一个tidyverse 选项。在这里,我将流派列中的列表分开,以便每个元素都在自己的行中。然后,我简单地粘贴“是”。到每个流派词,因为当我们旋转表格时,这将用于列名。然后,我创建一个带有“是”的新列,以记录每首歌曲标题的流派。然后,我们可以旋转整个表格并将NA 值替换为“否”。无论您有多少流派/标签,此解决方案都可以正常工作。唯一需要注意的是,我假设所有类型/标签都将用逗号分隔。

    library(tidyverse)
    
    df %>%
      separate_rows(genre, sep = ",") %>%
      mutate(genre = trimws(genre),
             genre = paste0("is.", genre),
             yn = "yes") %>%
      pivot_wider(names_from = "genre", values_from = "yn") %>%
      mutate(across(-song.title, replace_na, "no")) %>% 
      select(1, 3, 2, 4:5)
    

    输出

    # A tibble: 4 × 5
      song.title is.pop is.rock is.jazz is.funk
      <chr>      <chr>  <chr>   <chr>   <chr>  
    1 A          no     yes     no      no     
    2 B          yes    yes     no      no     
    3 C          yes    no      yes     yes    
    4 D          no     yes     no      yes 
    

    数据

    df <-
      structure(list(
        song.title = c("A", "B", "C", "D"),
        genre = c("rock", "pop, rock", "jazz, pop, funk", "funk, rock")
      ),
      class = "data.frame",
      row.names = c(NA,-4L))
    

    【讨论】:

      【解决方案2】:

      我们可以在这里使用grepl 作为基本 R 选项:

      is.pop <- ifelse(grepl("\\bpop\\b", df$genre), "yes", "no")
      is.rock <- ifelse(grepl("\\brock\\b", df$genre), "yes", "no")
      is.jazz <- ifelse(grepl("\\bjazz\\b", df$genre), "yes", "no")
      is.funk <- ifelse(grepl("\\bfunk\\b", df$genre), "yes", "no")
      df_needed <- data.frame(song.title, is.pop, is.rock, is.jazz, is.funk)
      df_needed
      
        song.title is.pop is.rock is.jazz is.funk
      1          A     no     yes      no      no
      2          B    yes     yes      no      no
      3          C    yes      no     yes     yes
      4          D     no     yes      no     yes
      

      数据:

      song.title <- c("A", "B", "C", "D")
      genre <- c("rock", "pop, rock", "jazz, pop, funk", "funk, rock")
      df <- data.frame(song.title, genre)
      

      【讨论】:

      • 这似乎很麻烦,尤其是因为他们说他们不知道需要多少标签
      • 所以不赞成? ... 无赖
      • @TimBiegeleisen 为这项努力投票 :=)
      猜你喜欢
      • 1970-01-01
      • 2019-01-02
      • 2021-08-31
      • 2017-10-14
      • 1970-01-01
      • 2019-12-17
      • 2022-11-30
      • 2022-10-14
      • 2015-12-08
      相关资源
      最近更新 更多