【问题标题】:R: Rethinking data in (How I can rearrange one column in set of columns?)R:重新思考数据(如何重新排列一组列中的一列?)
【发布时间】:2021-09-15 16:13:52
【问题描述】:

我有这样的表:

  A   B    C
1 a   b'   1
2 a   b'   2
3 a   b''  3
4 a2  b1   1
5 a3  b2   3

例如: A 列 - 是一个属(如 E.),B 列是一个物种(E. coli) C列是项目的一个类别(不计)

所以我需要了解,项目 b 包含多少个类:

  A  B   C1 C2 C3
1 a  b'  +  +  -
2 a  b'' -  -  +
3 a2 b1  +  -  -
4 a3 b2  -  -  +

【问题讨论】:

  • 对不起,我没看懂C1、C2、C3背后的逻辑
  • 除非您可以添加更多细节,并解释+ 和- 的含义,否则我认为这将作为how to pivot data from long to wide format 的副本关闭
  • C 列只是对应于对象类别的列(在我的研究中)所以,我在问如何将此列重新排列成集合。

标签: r dataframe


【解决方案1】:

使用tidyverse

df <- data.frame(A = c('a','a','a','a2','a3'), 
                 B = c("b'", "b'", "b''", "b1", "b2"),
                 C = c(1,2,3,1,3))

df %>%
  mutate( C = paste("C",C, sep = ""),
          D = "+") %>%
  pivot_wider(names_from = C, values_from = D, values_fill = "-")

首先创建data.frame,然后创建C1,C2,C3。你请求了一个+/- 系统,所以我在它存在的地方输入了一个+,所以当我pivot_wider 时。现在pivot_wider 将以+ 和NA 值结束;所以要纠正它,请使用values_fill = 选项并将其设置为-。

# A tibble: 4 x 5
  A     B     C1    C2    C3   
  <chr> <chr> <chr> <chr> <chr>
1 a     b'    +     +     -    
2 a     b''   -     -     +    
3 a2    b1    +     -     -    
4 a3    b2    -     -     +    

【讨论】:

    【解决方案2】:

    使用dcast

    library(data.table)
    dcast(setDT(df)[, tmp := "+"], A + B ~ paste0("C", C), value.var = "tmp", fill = "-")
        A   B C1 C2 C3
    1:  a  b'  +  +  -
    2:  a b''  -  -  +
    3: a2  b1  +  -  -
    4: a3  b2  -  -  +
    

    【讨论】:

      【解决方案3】:

      更新: 在 akrun 的帮助下生成的更紧凑的版本:

      df %>%      
        mutate(C = as.factor(C)) %>%
        bind_cols((map(setNames(levels(.$C), paste0("C", levels(.$C))), function(x) ifelse(x == .$C, "+", "-")))) %>% 
        group_by(A, B) %>%  summarise(across(everything(), ~ first(.[order(. != '+')])), .groups = 'drop')
      

      这是另一个更复杂的解决方案:在 akrun Combine, merge, coalesce rows by group and replace certain value by another value without pivoting 的帮助下生成

      library(tidyverse)
      df %>% 
          mutate(C = as.factor(C)) %>% 
          cbind(sapply(levels(.$C), `==`, .$C)) %>% 
          rename_with(.cols = 4:6, ~ paste0("C", .x)) %>% 
          mutate(across(C1:C3, ~ifelse(.==TRUE,"+", "-"))) %>% 
          select(-C) %>% 
          group_by(A, B) %>% 
          summarise(across(everything(), 
                           ~ first(.[order(. != '+')])), .groups = 'drop')
      

      输出:

        A     B     C1    C2    C3   
        <chr> <chr> <chr> <chr> <chr>
      1 a     b'    +     +     -    
      2 a     b''   -     -     +    
      3 a2    b1    +     -     -    
      4 a3    b2    -     -     +  
      

      【讨论】:

      • 或者更简洁一点df %&gt;% mutate(C = as.factor(C)) %&gt;% bind_cols((map(setNames(levels(.$C), paste0("C", levels(.$C))), function(x) ifelse(x == .$C, "+", "-")))) %&gt;% group_by(A, B) %&gt;% summarise(across(everything(), ~ first(.[order(. != '+')])), .groups = 'drop')
      • 太棒了。我已经添加到答案中了!
      猜你喜欢
      • 2018-11-27
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      相关资源
      最近更新 更多