将 dplyr 函数应用于矩阵的多个列
Apply dplyr functions to several columns of a matrix
我有一个38467*59的数据框。这是整个数据集的一个子集
mydata.2
Unique.Groups Protein.group.IDs Intensity.R1 Intensity.R2
1 yes 0 9701600 17971000
2 yes 0 0 29308000
3 yes 1 49083000 75300000
4 yes 2 53926000 13926000
5 yes 3 35255000 35750000
6 yes 3 302600000 295410000
7 yes 4 72766000 51914000
8 yes 5 33657000 31027000
9 yes 5 16504000 20689000
10 yes 5 191170000 209230000
11 yes 5 155710000 150280000
12 yes 5 402890000 529300000
13 yes 5 633480000 522180000
14 yes 5 92339000 0
15 yes 6 0 30646000
16 yes 6 67494000 92326000
17 yes 6 20205000 0
18 yes 7 94183000 76848000
19 yes 7 15584000 0
20 yes 7 27247000 38961000
21 yes 7 98433000 390870000
22 yes 8 0 36542000
23 yes 8 83978000 0
24 yes 8 53744000 95173000
25 yes 8 0 53494000
我将以下代码应用到 Intensity.R1 列,我想将它自动应用到数据框的所有 Intensities 列。
在每一轮中,我都需要 运行 Intensity.R1、Intensity.R2 等相同的代码。
data.normalized <- filter(mydata.2, Unique.Groups == "yes") %>%
group_by(Protein.group.IDs) %>%
arrange(desc(Intensity.R1)) %>%
top_n(3, Intensity.R1) %>%
mutate(sum.top.3.R1 = sum(Intensity.R1)) %>%
filter(!duplicated(Protein.group.IDs)) %>%
drop.levels() %>%
arrange(Protein.group.IDs)
每一轮都应该生成一个新列(即 sum.top.3),我想在最终 table 中将其绑定在一起。
这是 Intensity.R1 列的示例。
data.normalized
Source: local data frame [9 x 5]
Groups: Protein.group.IDs
Unique.Groups Protein.group.IDs Intensity.R1 Intensity.R2 sum.top.3.R1
1 yes 0 9701600 17971000 9701600
2 yes 1 49083000 75300000 49083000
3 yes 2 53926000 13926000 53926000
4 yes 3 302600000 295410000 337855000
5 yes 4 72766000 51914000 72766000
6 yes 5 633480000 522180000 1227540000
7 yes 6 67494000 92326000 87699000
8 yes 7 98433000 390870000 219863000
9 yes 8 83978000 0 137722000
我创建了一个简单的示例,其中包含相同的列名供您修改。
library(dplyr)
library(gdata)
mydata.2 = data.frame(Unique.Groups = rep("yes",8),
Protein.group.IDs = c(1,1,1,1,2,2,2,2),
Intensity.R1 = c(10,40,20,30,40,50,70,60),
Intensity.R2 = c(70,40,50,60,40,10,20,30))
# function that orders a vector and keeps top 3 values and calculates the sum
ff = function(x) {x[order(-x)][1:3] %>% sum()}
filter(mydata.2, Unique.Groups == "yes") %>%
group_by(Protein.group.IDs) %>%
select(-Unique.Groups) %>% # keep only grouping variable and the ones you need to calculate
summarise_each(funs(ff))
即使在您的代码中,如果您使用 "summarise" 而不是 "mutate",您也不必过滤掉重复的 ID。
我不知道我是否遗漏了此处的 drop.levels 内容。
另外,我真的不认为你需要第一个 "arrange",因为 top_n 将保持前 3 名,但顺序不正确(这不是问题)。
我有一个38467*59的数据框。这是整个数据集的一个子集
mydata.2
Unique.Groups Protein.group.IDs Intensity.R1 Intensity.R2
1 yes 0 9701600 17971000
2 yes 0 0 29308000
3 yes 1 49083000 75300000
4 yes 2 53926000 13926000
5 yes 3 35255000 35750000
6 yes 3 302600000 295410000
7 yes 4 72766000 51914000
8 yes 5 33657000 31027000
9 yes 5 16504000 20689000
10 yes 5 191170000 209230000
11 yes 5 155710000 150280000
12 yes 5 402890000 529300000
13 yes 5 633480000 522180000
14 yes 5 92339000 0
15 yes 6 0 30646000
16 yes 6 67494000 92326000
17 yes 6 20205000 0
18 yes 7 94183000 76848000
19 yes 7 15584000 0
20 yes 7 27247000 38961000
21 yes 7 98433000 390870000
22 yes 8 0 36542000
23 yes 8 83978000 0
24 yes 8 53744000 95173000
25 yes 8 0 53494000
我将以下代码应用到 Intensity.R1 列,我想将它自动应用到数据框的所有 Intensities 列。 在每一轮中,我都需要 运行 Intensity.R1、Intensity.R2 等相同的代码。
data.normalized <- filter(mydata.2, Unique.Groups == "yes") %>%
group_by(Protein.group.IDs) %>%
arrange(desc(Intensity.R1)) %>%
top_n(3, Intensity.R1) %>%
mutate(sum.top.3.R1 = sum(Intensity.R1)) %>%
filter(!duplicated(Protein.group.IDs)) %>%
drop.levels() %>%
arrange(Protein.group.IDs)
每一轮都应该生成一个新列(即 sum.top.3),我想在最终 table 中将其绑定在一起。 这是 Intensity.R1 列的示例。
data.normalized
Source: local data frame [9 x 5]
Groups: Protein.group.IDs
Unique.Groups Protein.group.IDs Intensity.R1 Intensity.R2 sum.top.3.R1
1 yes 0 9701600 17971000 9701600
2 yes 1 49083000 75300000 49083000
3 yes 2 53926000 13926000 53926000
4 yes 3 302600000 295410000 337855000
5 yes 4 72766000 51914000 72766000
6 yes 5 633480000 522180000 1227540000
7 yes 6 67494000 92326000 87699000
8 yes 7 98433000 390870000 219863000
9 yes 8 83978000 0 137722000
我创建了一个简单的示例,其中包含相同的列名供您修改。
library(dplyr)
library(gdata)
mydata.2 = data.frame(Unique.Groups = rep("yes",8),
Protein.group.IDs = c(1,1,1,1,2,2,2,2),
Intensity.R1 = c(10,40,20,30,40,50,70,60),
Intensity.R2 = c(70,40,50,60,40,10,20,30))
# function that orders a vector and keeps top 3 values and calculates the sum
ff = function(x) {x[order(-x)][1:3] %>% sum()}
filter(mydata.2, Unique.Groups == "yes") %>%
group_by(Protein.group.IDs) %>%
select(-Unique.Groups) %>% # keep only grouping variable and the ones you need to calculate
summarise_each(funs(ff))
即使在您的代码中,如果您使用 "summarise" 而不是 "mutate",您也不必过滤掉重复的 ID。 我不知道我是否遗漏了此处的 drop.levels 内容。 另外,我真的不认为你需要第一个 "arrange",因为 top_n 将保持前 3 名,但顺序不正确(这不是问题)。