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轉換爲特定值並再次在相同子集上平均這些值。謝謝!
完美。謝謝! – Joe
@Joe,很高興幫助,美好的一天 – Wen