【问题标题】:Joining tables in R based on multiple but incomplete ID columns by using ifelse statements使用 ifelse 语句基于多个但不完整的 ID 列连接 R 中的表
【发布时间】:2021-02-22 13:35:50
【问题描述】:

我有以下问题。 对于一个项目,我拥有来自许多不同学校的(化名)学生数据。我将不同的文件组合成一个大数据框。 类似于下表,其中包含一些(唯一)标识符(例如学号、name_code):

table1<-tribble(~Name_code, ~Student_number,    ~School,    ~Other_variables_I_measured,
    "Name_1", 123456, "A",  0.360,
    "Name_2", 234567, "A",  0.813,
    "Name_3", 345678, "A",  0.518,
    "Name_4", 456789, "A",  0.048,
    "Name_5", 567900, "A",  0.096,
    "Name_6", 679011, "B",  0.319,
    "Name_7", 790122, "B",  0.704,
    "Name_8", 901233, "B",  0.574,
    "Name_9", 112344, "B",  0.662,
    "Name_10", 123455, "B", 0.178)

(table1)

Name_code   Student_number  School  Other_variables_I_measured
Name_1      123456             A    0.360
Name_2      234567             A    0.813
Name_3      345678             A    0.518
Name_4      456789             A    0.048
Name_5      567900             A    0.096
Name_6      679011             B    0.319
Name_7      790122             B    0.704
Name_8      901233             B    0.574
Name_9      112344             B    0.662
Name_10     123455             B    0.178

在学年结束时,学校提供了额外的数据(GPA、保留率等), 但是,根据学校的不同,个别学生的报告可能只包含其中一个标识符,这使得将信息链接到表 1 变得很困难,因为当我结合成绩数据时,每个标识符都包含缺失。例如。学校 A 的报告可能只包括学生编号,学校 B 的报告可能只包括(重新编码的)姓名。

table2<-tribble(~Student_number,    ~Name_code, ~GPA,   ~School,
    123456,     NA,     8,  "A",
    234567,     NA,     9,  "A",
    345678,     NA,     7,  "A",
    456789,     NA,     8,  "A",
    567900,     NA,     7,  "A",
    NA,     "Name_6",   4,  "B",
    NA,     "Name_7",   5,  "B",
    NA,     "Name_8",   4,  "B",
    NA,     "Name_9",   5,  "B",
    NA,     "Name_10",  7,  "B")

(表2)

Student_number  Name_code   GPA School
123456             NA         8    A
234567             NA         9    A
345678             NA         7    A
456789             NA         8    A
567900             NA         7    A
  NA             Name_6       4    B
  NA             Name_7       5    B
  NA             Name_8       4    B
  NA             Name_9       5    B
  NA             Name_10      7    B

有没有办法根据不同的完整 ID 值连接表 1 和表 2? (= 通过 ID 变量 1 加入,如果缺少,则使用 ID 变量 2)

Some pseudo code like:
    dplyr::left_join(table1, table2, by= (ifelse(!is.na(Student_number), "Student_number", "Name_code"))))

应该产生table3

Name_code   Student_number  School  Other_variables_I_measured  GPA
Name 1      123456            A      0.360                  8
Name 2      234567            A      0.813                  9
Name 3      345678            A      0.518                  7
Name 4      456789            A      0.048                  8
Name 5      567900            A      0.096                  7
Name 6      679011            B      0.319                  4
Name 7      790122            B      0.704                  5
Name 8      901233            B      0.574                  4
Name 9      112344            B      0.662                  5
Name 10     123455            B      0.178                  7

最简单的方法是什么?或者甚至有可以完全绕过 ifelse 部分的函数?

【问题讨论】:

    标签: r if-statement conditional-statements left-join data-wrangling


    【解决方案1】:

    问题是left_join 似乎在提供多个键时对键强加了 AND 条件。例如,这不会给出正确的输出。

    colnames(table1) <-  paste0(colnames(table1),1)
    colnames(table2) <-  paste0(colnames(table2),2)
    left_join(table1, table2, by = c("Name_code1" = "Name_code2", "Student_number1" = "Student_number2"))
     # A tibble: 10 x 10
       Name_code1 Student_number1 School1 Other_variables_I_measured1 namematch match GPA2.x School2.x GPA2.y School2.y
       <chr>                <dbl> <chr>                         <dbl>     <int> <int>  <dbl> <chr>      <dbl> <chr>    
     1 Name_1              123456 A                             0.36         NA     1      8 A             NA NA       
     2 Name_2              234567 A                             0.813        NA     2      9 A             NA NA       
    

    因此,我会做的是:

    table1$match <- match(table1$Name_code1, table2$Name_code2)
    table1$match <- ifelse(!is.na(table1$match), table1$match, match(table1$Student_number1, table2$Student_number2))
    
    table1[,c("GPA2", "School2")] <- table2[table1$match,c("GPA2", "School2")]
    table1
    # A tibble: 10 x 8
       Name_code1 Student_number1 School1 Other_variables_I_measured1 namematch match  GPA2 School2
       <chr>                <dbl> <chr>                         <dbl>     <int> <int> <dbl> <chr>  
     1 Name_1              123456 A                             0.36         NA     1     8 A      
     2 Name_2              234567 A                             0.813        NA     2     9 A      
    

    这里table1$match 列返回table2 中对应的行号,用于table1 中的所有行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2017-03-12
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多