嗨,我想彙總幾欄。按字符聚合字段
d <- structure(list(Gene = structure(1:3, .Label = c("k141_20041_1",
"k141_27047_2", "k141_70_3"), class = "factor"), phylum = structure(c(1L,
1L, 1L), .Label = "Firmicutes", class = "factor"), class = structure(c(1L,
1L, 1L), .Label = "Bacillales", class = "factor"), order = structure(c(1L,
1L, 1L), .Label = "Bacilli", class = "factor"), family = structure(c(1L,
1L, 1L), .Label = "Bacillaceae", class = "factor"), genus = structure(c(1L,
1L, 1L), .Label = "Bacillus", class = "factor"), species = structure(c(1L,
1L, 2L), .Label = c("Bacillus subtilis", "unknown"), class = "factor"),
SampleA = c(0, 0, 0), SampleB = c(0, 0, 0), SampleCtrl = c(3.98888888888889,
11.5555555555556, 3.35978835978836)), .Names = c("Gene",
"phylum", "class", "order", "family", "genus", "species", "SampleA",
"SampleB", "SampleCtrl"), row.names = c(21918L, 40410L, 40857L
), class = "data.frame")
這在輸入數據幀聚合
Gene phylum class order family genus species SampleA SampleB
k141_20041_1 Firmicutes Bacillales Bacilli Bacillaceae Bacillus Bacillus subtilis 0 0
k141_27047_2 Firmicutes Bacillales Bacilli Bacillaceae Bacillus Bacillus subtilis 0 0
k141_70_3 Firmicutes Bacillales Bacilli Bacillaceae Bacillus unknown 0 0
SampleCtrl
3.99
11.56
3.36
什麼,我想在結束與所有列的一個單行。在這種情況下,它看起來像這樣(我們可以刪除基因列)。
phylum class order family genus species SampleA SampleB SampleCtrl
Firmicutes Bacillales Bacilli Bacillaceae Bacillus Bacillus subtilis 0 0 15.6
Firmicutes Bacillales Bacilli Bacillaceae Bacillus Bacillus Unknown 0 0 3.36
請注意,這是一個非常簡單的例子。我在原始數據框中有20個樣本和500多個物種。
我想彙總所有樣本列(不只是samplectrl列)。我的問題並不清楚。這個例子只會聚合Samplectrl列。我有20多欄,我是否必須在這種情況下列出所有欄? – david
@david我編輯了我的答案。現在,SampleA和SampleB也被彙總,因爲它們也是數字。 –
完美的作品。非常感謝 – david