根据另一列中的唯一值对一列中的值求和
Summing up values in one column based on unique values in another column
我正在尝试根据 B.For 列实例中的唯一值添加 C 列中的值,对于 B = 1,我想添加 C 列中的所有行,即 5+4+3= 12.
A B C
1 1 5
2 1 4
3 1 3
4 2 1
5 2 3
for(i in unique(df$B)){
df$D = sum(df$C)
}
另外,我想加上B列每个数据出现的次数。
解决方案:
A B C D E
1 1 5 12 3
2 1 4 12 3
3 1 3 12 3
4 2 1 4 2
5 2 3 4 2
我的任务示例:
docIdx newsgroup_ID freq
1 1 768
2 1 125
3 1 29
4 1 51
5 1 198
6 1 34
7 1 64
8 2 35
9 2 70
10 2 45
您可以 aggregate
然后 merge
使用原始数据框的结果:
df <- read.table(text="A B C
1 1 5
2 1 4
3 1 3
4 2 1
5 2 3", header = TRUE)
merge(df, aggregate(C ~ B, df, FUN = function(x) c(sum=sum(x), length=length(x))), by = "B")
#> B A C.x C.y.sum C.y.length
#> 1 1 1 5 12 3
#> 2 1 2 4 12 3
#> 3 1 3 3 12 3
#> 4 2 4 1 4 2
#> 5 2 5 3 4 2
由 reprex package (v0.2.1)
于 2019-02-18 创建
在基础 R 中你可以使用 ave
df[, c("D", "E")] <- with(df, sapply(c(sum, length), function(x) ave(C, B, FUN = x)))
df
# A B C D E
#1 1 1 5 12 3
#2 2 1 4 12 3
#3 3 1 3 12 3
#4 4 2 1 4 2
#5 5 2 3 4 2
或使用dplyr
library(dplyr)
df <- df %>%
group_by(B) %>%
mutate(D = sum(C), E = length(C))
df
## A tibble: 5 x 5
## Groups: B [2]
# A B C D E
# <int> <int> <int> <int> <int>
#1 1 1 5 12 3
#2 2 1 4 12 3
#3 3 1 3 12 3
#4 4 2 1 4 2
#5 5 2 3 4 2
示例数据
df <- read.table(text =
"A B C
1 1 5
2 1 4
3 1 3
4 2 1
5 2 3", header = T)
它适用于您修改后的数据
df <- read.table(text =
"docIdx newsgroup_ID freq
1 1 768
2 1 125
3 1 29
4 1 51
5 1 198
6 1 34
7 1 64
8 2 35
9 2 70
10 2 45", header = T)
df[, c("sum.freq", "length.freq")] <- with(df, sapply(c(sum, length), function(x)
ave(freq, newsgroup_ID, FUN = x)))
# docIdx newsgroup_ID freq sum.freq length.freq
#1 1 1 768 1269 7
#2 2 1 125 1269 7
#3 3 1 29 1269 7
#4 4 1 51 1269 7
#5 5 1 198 1269 7
#6 6 1 34 1269 7
#7 7 1 64 1269 7
#8 8 2 35 150 3
#9 9 2 70 150 3
#10 10 2 45 150 3
此处 ave(freq, newsgroup_ID, FUN = x)
通过 newsgroup_ID
将函数 x
应用到 freq
。
B <- c(1,1,1,2,2)
C <- c(5,4,3,1,3)
x <- cbind(B,C)
sum <- 0
for (i in 1:nrow(x)) {
if (x[i] == 1) {
sum <- x[i, 2] + sum
}
sum
}
希望对您有所帮助。
如果您想使用循环条件执行相同的逻辑
for (i in unique (df$B)){
xx <- sum(df$C[df$B==i])
yy <- length(df$C[df$B==i])
df$D[df$B==i] <- xx
df$E[df$B==i] <- yy
}
print(df)
A B C D E
1 1 1 5 12 3
2 2 1 4 12 3
3 3 1 3 12 3
4 4 2 1 4 2
5 5 2 3 4 2
B <- c(1,1,1,2,2)
C <- c(5,4,3,1,3)
x <- cbind(B,C)
holder1 <- c()
holder2 <- c()
for (num in unique(x[,1])) {
sum <- 0
count <- 0
for (i in 1:nrow(x)) {
if (x[i] == num) {
sum <- x[i, 2] + sum
count <- 1 + count
}
}
print(count)
holder1 <- c(holder1, rep(count, count))
holder2 <- c(holder2, rep(sum, count))
}
x <- as.data.frame(x)
x <- add_column(x, E = holder1, .after = "C")
x <- add_column(x, D = holder2, .after = "C")
> x
B C D E
1 1 5 12 3
2 1 4 12 3
3 1 3 12 3
4 2 1 4 2
5 2 3 4 2
注意:
确保我们有相同的变量。 (理解代码)
我不知道高级功能,所以我使用了基本功能。
我正在尝试根据 B.For 列实例中的唯一值添加 C 列中的值,对于 B = 1,我想添加 C 列中的所有行,即 5+4+3= 12.
A B C
1 1 5
2 1 4
3 1 3
4 2 1
5 2 3
for(i in unique(df$B)){
df$D = sum(df$C)
}
另外,我想加上B列每个数据出现的次数。
解决方案:
A B C D E
1 1 5 12 3
2 1 4 12 3
3 1 3 12 3
4 2 1 4 2
5 2 3 4 2
我的任务示例:
docIdx newsgroup_ID freq
1 1 768
2 1 125
3 1 29
4 1 51
5 1 198
6 1 34
7 1 64
8 2 35
9 2 70
10 2 45
您可以 aggregate
然后 merge
使用原始数据框的结果:
df <- read.table(text="A B C
1 1 5
2 1 4
3 1 3
4 2 1
5 2 3", header = TRUE)
merge(df, aggregate(C ~ B, df, FUN = function(x) c(sum=sum(x), length=length(x))), by = "B")
#> B A C.x C.y.sum C.y.length
#> 1 1 1 5 12 3
#> 2 1 2 4 12 3
#> 3 1 3 3 12 3
#> 4 2 4 1 4 2
#> 5 2 5 3 4 2
由 reprex package (v0.2.1)
于 2019-02-18 创建在基础 R 中你可以使用 ave
df[, c("D", "E")] <- with(df, sapply(c(sum, length), function(x) ave(C, B, FUN = x)))
df
# A B C D E
#1 1 1 5 12 3
#2 2 1 4 12 3
#3 3 1 3 12 3
#4 4 2 1 4 2
#5 5 2 3 4 2
或使用dplyr
library(dplyr)
df <- df %>%
group_by(B) %>%
mutate(D = sum(C), E = length(C))
df
## A tibble: 5 x 5
## Groups: B [2]
# A B C D E
# <int> <int> <int> <int> <int>
#1 1 1 5 12 3
#2 2 1 4 12 3
#3 3 1 3 12 3
#4 4 2 1 4 2
#5 5 2 3 4 2
示例数据
df <- read.table(text =
"A B C
1 1 5
2 1 4
3 1 3
4 2 1
5 2 3", header = T)
它适用于您修改后的数据
df <- read.table(text =
"docIdx newsgroup_ID freq
1 1 768
2 1 125
3 1 29
4 1 51
5 1 198
6 1 34
7 1 64
8 2 35
9 2 70
10 2 45", header = T)
df[, c("sum.freq", "length.freq")] <- with(df, sapply(c(sum, length), function(x)
ave(freq, newsgroup_ID, FUN = x)))
# docIdx newsgroup_ID freq sum.freq length.freq
#1 1 1 768 1269 7
#2 2 1 125 1269 7
#3 3 1 29 1269 7
#4 4 1 51 1269 7
#5 5 1 198 1269 7
#6 6 1 34 1269 7
#7 7 1 64 1269 7
#8 8 2 35 150 3
#9 9 2 70 150 3
#10 10 2 45 150 3
此处 ave(freq, newsgroup_ID, FUN = x)
通过 newsgroup_ID
将函数 x
应用到 freq
。
B <- c(1,1,1,2,2)
C <- c(5,4,3,1,3)
x <- cbind(B,C)
sum <- 0
for (i in 1:nrow(x)) {
if (x[i] == 1) {
sum <- x[i, 2] + sum
}
sum
}
希望对您有所帮助。
如果您想使用循环条件执行相同的逻辑
for (i in unique (df$B)){
xx <- sum(df$C[df$B==i])
yy <- length(df$C[df$B==i])
df$D[df$B==i] <- xx
df$E[df$B==i] <- yy
}
print(df)
A B C D E
1 1 1 5 12 3
2 2 1 4 12 3
3 3 1 3 12 3
4 4 2 1 4 2
5 5 2 3 4 2
B <- c(1,1,1,2,2)
C <- c(5,4,3,1,3)
x <- cbind(B,C)
holder1 <- c()
holder2 <- c()
for (num in unique(x[,1])) {
sum <- 0
count <- 0
for (i in 1:nrow(x)) {
if (x[i] == num) {
sum <- x[i, 2] + sum
count <- 1 + count
}
}
print(count)
holder1 <- c(holder1, rep(count, count))
holder2 <- c(holder2, rep(sum, count))
}
x <- as.data.frame(x)
x <- add_column(x, E = holder1, .after = "C")
x <- add_column(x, D = holder2, .after = "C")
> x
B C D E
1 1 5 12 3
2 1 4 12 3
3 1 3 12 3
4 2 1 4 2
5 2 3 4 2
注意: 确保我们有相同的变量。 (理解代码) 我不知道高级功能,所以我使用了基本功能。