根据匹配的案例,将一个数据框中的特定列添加到另一个数据框

时间:2017-11-28 17:37:52

标签: r

我有两个数据框,我想在 data2 数据框中添加两列 AGE IQ data1 数据框仅用于匹配 ID 。以下是两个数据帧的示例:

    data1= structure(list(ID = c(1L, 2L, 3L, 4L, 5L), CONDITION = structure(c(1L, 1L, 1L, 2L, 2L), .Label=c("ANX","ADHD"), class = "factor")), class = "data.frame", row.names = c(NA, -5L))

    data2= structure(list(ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L), CONDITION = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L), .Label=c("ANX","ADHD", "COM"), class = "factor"), AGE = c(7L, 8L, 8L, 9L, 9L, 10L, 11L), IQ =c(78L, 83L, 116L, 90L, 80L, 85L, 87L)), .Names= c("ID", "CONDITION", "AGE", "IQ"), class = "data.frame", row.names = c(NA, -7L))

到目前为止,我尝试过类似的事情:

    merge(data1, data2[, c("AGE", "IQ")], by= "ID")

任何帮助将不胜感激!提前谢谢。

0 个答案:

没有答案