2017-08-14 101 views
1

我試圖找到基於多列的數據幀的最小值。我能夠使用下面的聚合函數成功地完成此操作。但是,結果不包含輸入數據框中沒有數據的因素組合。因子水平的所有組合的綜合值,包括缺失的因子

我已經有了:

# all possibilities of fruits, cities, and vegetables: 
fruits<-c('apple','banana','grape') 
cities<-c('new york','chicago','los angeles') 
vegetables<-c('cucumber','mushroom') 

#my input (ie, a sample from a test: 
inputdf<-data.frame(fruit=c('apple','apple','apple','banana','banana','banana','grape','grape','grape'),city=c('new york','new york','new york','new york','chicago','los angeles','chicago','chicago','chicago'),vegetable=c('cucumber','cucumber','mushroom','cucumber','mushroom','mushroom','cucumber','cucumber','cucumber'),value=c(5,3,4,6,5,7,2,7,4)) 

#my aggregation: 
outdf<-aggregate(value ~ fruit + city + vegetable,inputdf,function(x) min(x)) 

我得到的輸出是:

fruit city  vegetable value 
grape chicago  cucumber 2 
apple new york cucumber 3 
banana new york cucumber 6 
banana chicago  mushroom 5 
banana los angeles mushroom 7 
apple new york mushroom 4 

這是正確的,但是,我也想對應的列是didnt組合的行在輸入df存在:

fruit city  vegetable value 
apple new york cucumber 3 
apple new york mushroom 4 
apple chicago  cucumber NA 
apple chicago  mushroom NA 
apple los angeles cucumber NA 
apple los angeles mushroom NA 
banana new york cucumber 6 
banana new york mushroom NA 
banana chicago  cucumber NA 
banana chicago  mushroom 5 
banana los angeles cucumber NA 
banana los angeles mushroom 7 
grape new york cucumber NA 
grape new york mushroom NA 
grape chicago  cucumber 2 
grape chicago  mushroom NA 
grape los angeles cucumber NA 
grape los angeles mushroom NA 

我希望能夠做到這一點的任何數量的列上,其中噸o結合。有沒有簡單的方法來做到這一點?我想要輸出的原因是因爲我需要將NA轉換爲特定值並再次在相同子集上平均這些值。謝謝!

回答

2

可以使用expand.grid生成所有組合,然後利用merge

outdf<-aggregate(value ~ fruit + city + vegetable,inputdf,function(x) min(x)) 
DF=expand.grid(fruits, cities, vegetables) 
outdf=merge(outdf,DF,by.x=c('fruit','city','vegetable'),by.y=c('Var1','Var2','Var3'),all.y=T) 
> outdf 
    fruit  city vegetable value 
1 apple  chicago cucumber NA 
2 apple  chicago mushroom NA 
3 apple los angeles cucumber NA 
4 apple los angeles mushroom NA 
5 apple new york cucumber  3 
6 apple new york mushroom  4 
7 banana  chicago cucumber NA 
8 banana  chicago mushroom  5 
9 banana los angeles cucumber NA 
10 banana los angeles mushroom  7 
11 banana new york cucumber  6 
12 banana new york mushroom NA 
13 grape  chicago cucumber  2 
14 grape  chicago mushroom NA 
15 grape los angeles cucumber NA 
16 grape los angeles mushroom NA 
17 grape new york cucumber NA 
18 grape new york mushroom NA 
+0

完美。謝謝! – Joe

+0

@Joe,很高興幫助,美好的一天 – Wen