如何根据匹配从另一个数据框中引入值?

时间:2019-04-15 11:44:54

标签: r

我有两个数据帧:import { UpgradeModule } from '@angular/upgrade/static'; @NgModule({ imports:[UpgradeModule]}) export class MyModule { constructor(private readonly upgrade: UpgradeModule) {} ngDoBootstrap() { this.upgrade.bootstrap(document.body, [name], { strictDi: true }); } } df1

codesDesc包含具有某些代码的信息,我想通过在df1中进行查找将相关描述添加到df1$desc(新列)中。

我尝试过这样的事情:

codesDesc

或者这个:

df1$desc <- codesDesc$desc[df1$code %in% codesDesc$code]

但是由于替换行数不匹配,两者均失败。

我在这里想念什么?我猜这是我的语法错误。

df1$desc <- codesDesc$desc[which(df1$code %in% codesDesc$code)]

dput(df1)

structure(list(dx = structure(1:108, .Label = c("Dx010", "Dx0101", "Dx0103", "Dx0104", "Dx0105", "Dx0106", "Dx0107", "Dx011", "Dx0111", "Dx0112", "Dx01120", "Dx01121", "Dx01122", "Dx0113", "Dx0114", "Dx0115", "Dx0116", "Dx0117", "Dx0118", "Dx0119", "Dx012", "Dx0121", "Dx0122", "Dx0126", "Dx0127", "Dx013", "Dx014", "Dx016", "Dx0162", "Dx02", "Dx03", "Dx05", "Dx06", "Dx07", "Dx08", "Dx09", "Dx10", "Dx106", "Dx108", "Dx11", "Dx110", "Dx111", "Dx115", "Dx116", "Dx117", "Dx118", "Dx119", "Dx12", "Dx120", "Dx13", "Dx14", "Dx15", "Dx16", "Dx18", "Dx19", "Dx20", "Dx21", "Dx22", "Dx28", "Dx30", "Dx31", "Dx32", "Dx321", "Dx322", "Dx323", "Dx324", "Dx325", "Dx326", "Dx327", "Dx328", "Dx329", "Dx330", "Dx332", "Dx333", "Dx334", "Dx335", "Dx336", "Dx34", "Dx35", "Dx38", "Dx39", "Dx404", "Dx45", "Dx46", "Dx48", "Dx49", "Dx50", "Dx58", "Dx59", "Dx75", "Dx76", "Dx77", "Dx78", "Dx80", "Dx81", "Dx82", "Dx85", "Dx86", "Dx87", "Dx88", "Dx89", "Dx91", "Dx92", "Dx93", "Dx94", "Dx96", "Dx97", "Dx98", "NULL"), class = "factor"), freq = c(24L, 20L, 6L, 2L, 76L, 90L, 13L, 33L, 11L, 912L, 1L, 67L, 22L, 98L, 121L, 15L, 41L, 87L, 38L, 172L, 146L, 75L, 93L, 6L, 3L, 12L, 10L, 20L, 10L, 1026L, 309L, 4255L, 3006L, 1180L, 2580L, 158L, 40L, 33L, 1893L, 4521L, 9L, 1L, 2L, 126L, 1L, 5L, 18L, 557L, 11L, 398L, 249L, 250L, 169L, 34L, 135L, 432L, 644L, 163L, 101L, 9L, 28L, 910L, 258L, 171L, 744L, 90L, 225L, 24L, 6L, 2L, 39L, 5L, 1L, 3231L, 924L, 3213L, 6L, 23L, 1101L, 1208L, 64L, 2L, 27L, 114L, 5L, 11L, 21L, 66L, 27L, 513L, 565L, 129L, 210L, 59L, 5L, 376L, 653L, 65L, 68L, 3L, 18L, 1L, 95L, 64L, 2L, 274L, 2L, 1L)), row.names = c(NA, 108L), class = "data.frame")

dput(codesDesc)

1 个答案:

答案 0 :(得分:0)

merge数据集如何? 在这种情况下,左联接:

merged <- merge(x = df1, y = codesDesc, by = "dx", all.x = TRUE)
 head(merged)
      dx freq                                                        disposition
1  Dx010   24 \n\nEmergency Ambulance Response for Potential Cardiac Arrest \n\n
2 Dx0101   20          Emergency Ambulance Response for Potential Cardiac Arrest
3 Dx0103    6                       Emergency Ambulance response for Fitting Now
4 Dx0104    2                  Emergency Ambulance Response for Major Blood Loss
5 Dx0105   76                  Emergency Ambulance Response for Potential Shock 
6 Dx0106   90              Emergency Ambulance Response for Respiratory Distress

或使用dplyr

library(dplyr)
 k <- df1 %>% left_join(codesDesc)

请注意,您的codesDesc中有一些重复说明,因此结果中的行多于df1

library(dplyr)
double_ <- as.data.frame.table(table( codesDesc$dx)) %>% filter(Freq >= 2)

在df1中,您有一些双重代码:

df1[df1$dx %in% double_$Var1,]