【问题标题】:Add a column with an ID for matching rows添加具有 ID 的列以匹配行
【发布时间】:2022-11-13 01:20:04
【问题描述】:

我有如下数据:

library(data.table)
dat <- fread("Variable_codes_2022 Variables_2022
              Cat1_1              This_question
              Cat1_2              Other_question
              Cat2_1              One_question
              Cat2_2              Another_question
              Cat3_1              Some_question
              Cat3_2              Extra_question
              Cat3_3              This_question
              Cat4_1              One_question
              Cat4_2              Wrong_question")

我想做的是创建一个新列,它提供一个唯一的新变量代码,用于匹配变量。我从创建一个显示重复项的列开始,但这只会给TRUE 第二次出现而不是两者。此外,我仍然必须为 TRUE 值提供唯一名称。

dat$Common_codes_2022 <-  duplicated(dat[,2])

我应该怎么做?

期望的输出:

   Variable_codes_2022   Variables_2022 Common_codes_2022
1:              Cat1_1    This_question Com_1
2:              Cat1_2   Other_question
3:              Cat2_1     One_question Com_2
4:              Cat2_2 Another_question
5:              Cat3_1    Some_question
6:              Cat3_2   Extra_question
7:              Cat3_3    This_question Com_1
8:              Cat4_1     One_question Com_2
9:              Cat4_2   Wrong_question 

【问题讨论】:

  • Common_codes_2022 中空单元格的逻辑是什么?
  • @jay.sf 空单元格没有重复出现。
  • 您需要 Common_codes_2022 是连续的(例如,"Com_1""Com_2"、...)或者它们可以是随机的,只要它们是唯一的(例如,"Com_3""Com_6"、...)?

标签: r


【解决方案1】:

如果通用代码变量只是标记特定的Variables_2022 响应,您可以使用条件语句,例如这个case_when() 示例。

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last

dat <- fread("Variable_codes_2022 Variables_2022
              Cat1_1              This_question
              Cat1_2              Other_question
              Cat2_1              One_question
              Cat2_2              Another_question
              Cat3_1              Some_question
              Cat3_2              Extra_question
              Cat3_3              This_question
              Cat4_1              One_question
              Cat4_2              Wrong_question")

dat %>%
  mutate(Common_codes_2022 = case_when(
    Variables_2022 == "This_question" ~ "Com_1",
    Variables_2022 == "One_question" ~ "Com_2",
    TRUE ~ "")
  )
#>    Variable_codes_2022   Variables_2022 Common_codes_2022
#> 1:              Cat1_1    This_question             Com_1
#> 2:              Cat1_2   Other_question                  
#> 3:              Cat2_1     One_question             Com_2
#> 4:              Cat2_2 Another_question                  
#> 5:              Cat3_1    Some_question                  
#> 6:              Cat3_2   Extra_question                  
#> 7:              Cat3_3    This_question             Com_1
#> 8:              Cat4_1     One_question             Com_2
#> 9:              Cat4_2   Wrong_question

创建于 2022 年 11 月 12 日,reprex v2.0.2

【讨论】:

  • 谢谢你的回答 Seth,但我一直在寻找一个更通用的解决方案。例如,我现在不事先做哪些问题出现不止一次或他们的名字是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2021-03-25
  • 2022-07-06
  • 2021-01-15
  • 2022-12-18
相关资源
最近更新 更多