【问题标题】:How to extract unique values within each row in dataframe?如何在R中的数据框中的每一行中提取唯一值
【发布时间】:2021-07-05 18:10:56
【问题描述】:

我正在尝试在不使用 for 循环的情况下提取 R 中每行数据帧中的唯一值。

df <- data.frame(customer = c('joe','jane','john','mary'), fruit = c('orange, apple, orange', NA, 'apple', 'orange, orange'))

df

  customer                 fruit
1      joe orange, apple, orange
2     jane                  <NA>
3     john                 apple
4     mary        orange, orange

我想要的 fruit 列是: '橙色,苹果',NA,'苹果','橙色'

  customer                 fruit
1      joe         orange, apple
2     jane                  <NA>
3     john                 apple
4     mary                orange

我尝试了一些类似的东西

apply(df, 1, function(x) unique(unlist(str_split(x[, "fruit"], ", "))))

它不工作。

如何在数据框中的每一行中获取唯一值?

【问题讨论】:

    标签: r


    【解决方案1】:

    更新的解决方案 我刚刚修改了我的代码以匹配您希望输出的内容。

    library(dplyr)
    library(tidyr)
    
    df %>%
      separate_rows(fruit) %>%
      distinct(customer, fruit) %>%
      group_by(customer) %>%
      summarise(fruit = paste(sort(fruit, na.last = FALSE), collapse = ", "))
    
    # A tibble: 4 x 2
      customer fruit        
      <chr>    <chr>        
    1 jane     NA           
    2 joe      apple, orange
    3 john     apple        
    4 mary     orange
    
    

    【讨论】:

    • 我对这个网站很陌生,所以我花了一段时间来编辑 OP.. 非常感谢您的回答。看起来真的很有希望!有没有办法保持每个客户的独特价值?我编辑以显示我想要的。根据您的回答,我想我可以 group_by(customer) 然后从那里开始?
    • 我对其进行了修改,以便我们为每个客户在每一行中分隔唯一值。
    【解决方案2】:

    这是一个使用基础 R 的潜在解决方案,没有库。很多丑陋的括号,但我认为它有效..

    df$fruit <-lapply(1:nrow(df),function(n)unique(trimws(unlist(strsplit(df$fruit[n],",")))))
    
    

    输出如下

    > df
      customer         fruit
    1      joe orange, apple
    2     jane            NA
    3     john         apple
    4     mary        orange
    

    【讨论】:

    • apply 函数旨在将函数应用于对象的行或列。 lapply 是您刚刚解决的列表。
    【解决方案3】:

    基础 R 选项:

    用逗号分割字符串,保持唯一值并将值粘贴到逗号分隔的字符串中。

    df$fruit <- sapply(strsplit(df$fruit, ',\\s+'), function(x) toString(unique(x)))
    df
    
    #  customer         fruit
    #1      joe orange, apple
    #2     jane            NA
    #3     john         apple
    #4     mary        orange
    

    【讨论】:

      【解决方案4】:

      使用dplyrpurrr::map 的简单管道语法

      df %>% mutate(fruit = str_split(fruit, ", "),
                    fruit = map(fruit, ~ unique(.x)))
        customer         fruit
      1      joe orange, apple
      2     jane            NA
      3     john         apple
      4     mary        orange
      

      或仅 BaseR

      df$fruit <- Map(unique, strsplit(df$fruit, ", "))
      df
      
      > df
        customer         fruit
      1      joe orange, apple
      2     jane            NA
      3     john         apple
      4     mary        orange
      

      注意:假设每个字符串都用逗号和空格分隔,如示例所示

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        • 1970-01-01
        相关资源
        最近更新 更多