从两个文件开始,如下所示:
Crop, Value
Coconut, 12300
Amaranthus, 123
Cowpea, 100
Brinjal, 210
Coconut, 500
Brinjal, 10
Coconut, 1000
Cowpea, 105
Crop, Value
Rubber, 10
Cardamom, 22
Amaranthus, 12
Garcinia, 10
Brinjal, 10
Coconut, 1000
Brinjal, 25
> d1 = read.csv("test1.r")
> d2 = read.csv("test2.r")
> sum1 = tapply(d1$Value,d1$Crop,sum)
> sum2 = tapply(d2$Value,d2$Crop,sum)
> sum1
Amaranthus Brinjal Coconut Cowpea
123 220 13800 205
> sum2
Amaranthus Brinjal Cardamom Coconut Garcinia Rubber
12 35 22 1000 10 10
我想将sum1和sum2合并到一个数组中,方法是将值为'sum'的运算符应用于该值,如果两个文件中存在相应的值(如Amaranthus,Brinjal和Coconut),或者只是插入名称 - 值对(如果它只存在于一个文件中)。简而言之,输出应如下所示(为方便起见,并假设'sum'运算符):
Amaranthus 135
Brinjal 255
Cardamom 22
Coconut 14800
Cowpea 205
Garcinia 10
Rubber 10
任何帮助的TIA,
S1B