本文介绍了在R中仅合并来自不同数据帧的一或两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两组数据框.以下是每行的前五行.
I have two sets of dataframes. Below are the first five lines for each.
First Data frame Name: sampel_sort
name id supplier usage
ABC 10000079 811121 1
DEF 10000182 541513 4
Supplier C 10000484 531110 1
Supplier D 10000523 541320 1
Supplier E 10000592 524210 1
Supplier F 10012711 237110 1
Second data frame Name: MBE
id State total CATEGORY
10000070 MD 5 MBE
10000182 PR 14 MBE
10000484 TX 1 MBE
10000526 MI 3 MBE
10000592 FL 1 MBE
10000680 ID 14 MBE
我的实际数据集还有很多列.我想合并两个数据框,但只想导入类别列.以下合并语句有效:
My actual dataset has lots more columns. I want to combine the two dataframes, but would like to import only the category column. the following merge statement works:
ncombined <- merge(x = sample_sort, y = MBE, by = "id", all.x = TRUE)
但这给了我MBE数据集中的所有列.我以不同的方式尝试了以下操作(以便仅导入类别列).但是我没有运气.我收到错误消息
But this gives me all the columns from the MBE dataset. I tried the following in different ways (so that only the category column gets imported). But I am not having any luck. I get an error
ncombined <- merge(x = sample_sort, y = MBE[,c("CATEGORY")], by = "id", all.x = TRUE)
最终结果应如下:
First Data frame Name: sample_sort
name id supplier usage CATEGORY
ABC 10000079 811121 1 MBE
DEF 10000182 541513 4 MBE
Supplier C 10000484 531110 1 MBE
Supplier D 10000523 541320 1 MBE
Supplier E 10000592 524210 1 MBE
Supplier F 10012711 237110 1 NA
推荐答案
尝试在合并之前取出列,例如
Try taking out the columns before merging, eg
ncombined <- merge(x = sample_sort, y = MBE[,c(1:4)], by = "id", all.x = TRUE)
这篇关于在R中仅合并来自不同数据帧的一或两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!