【问题标题】:Create a new variable containing the column name in case the value isn't NA创建一个包含列名的新变量,以防值不是 NA
【发布时间】:2021-07-05 21:51:36
【问题描述】:

假设我有以下数据集:

data = tibble::tibble(
  id = c("x", "y", "x"),
  inputA = c(1, NA, NA),
  inputB = c(2, 1, NA),
  inputC = c(3, 2, 3)
)

看起来像这样:

# A tibble: 3 x 4
  id    inputA inputB inputC
  <chr>  <dbl>  <dbl>  <dbl>
1 x          1      2      3
2 y         NA      1      2
3 x         NA     NA      3

我想为每个 id(每个唯一行)创建一个变量,用于标识 id 的输入。我的意思是,如果输入变量没有丢失 (NA),则新变量应该指示 id 的输入。

所需的输出应如下所示:

# A tibble: 3 x 5
  id    inputA inputB inputC inputs              
  <chr>  <dbl>  <dbl>  <dbl> <chr>               
1 x          1      2      3 inputA-inputB-inputC
2 y         NA      1      2 inputB-inputC       
3 x         NA     NA      3 inputC   

我要创建的变量是inputs

【问题讨论】:

    标签: r dataframe dplyr tibble data-wrangling


    【解决方案1】:

    dplyr 中使用rowwise

    library(dplyr)
    
    cols <- names(data)[-1]
    
    data %>%
      rowwise() %>%
      mutate(inputs = paste0(cols[!is.na(c_across(all_of(cols)))], collapse = '-'))
    
    #   id    inputA inputB inputC inputs              
    #  <chr>  <dbl>  <dbl>  <dbl> <chr>               
    #1 x          1      2      3 inputA-inputB-inputC
    #2 y         NA      1      2 inputB-inputC       
    #3 x         NA     NA      3 inputC              
    

    在基础 R 中:

    data$inputs <- apply(!is.na(data[cols]), 1, function(x) 
                         paste0(cols[x], collapse = '-'))
    

    【讨论】:

    • 太棒了。为什么 c_across 与 rowwise 一起工作,而 across 却意味着 rowwise?
    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多