![](/img/trans.png)
[英]R dplyr - select values from one column based on position of a specific value in another column
[英]R dplyr add values based on previous value and value from another column
我有一个数据框
> df
A B
1 a x
2 b y
3 c z
4 d n
5 e m
我希望将 A 列中的先前值与 B 列中的当前值相加以替换当前 A 列,以便所需的输出变为
> df
A B
1 a x
2 a+y y
3 a+y+z z
4 a+y+z+n n
5 a+y+z+n+m m
创建数据框的代码
df = data.frame(A = c('a','b','c', 'd', 'e'), B = c('x', 'y', 'z', 'n', 'm'))
我写了for循环
for(i in df){
df$A = lag(df$A) + df$B
}
但它没有用
编辑:实际值是数字。 我用字母让你快速阅读。 (也许我不应该!)
我们可以使用Reduce
和accumulate = TRUE
Reduce(function(x, y) paste(x, y, sep = "+"), df$B[-1], accumulate = TRUE,
init = df$A[1])
#[1] "a" "a+y" "a+y+z" "a+y+z+n" "a+y+z+n+m"
同样,我们也可以使用来自purrr
accumulate
library(dplyr)
library(purrr)
df %>% mutate(A = accumulate(B[-1], paste, sep = "+", .init = first(A)))
# A B
#1 a x
#2 a+y y
#3 a+y+z z
#4 a+y+z+n n
#5 a+y+z+n+m m
数据
df <- data.frame(A = c('a','b','c', 'd', 'e'), B = c('x', 'y', 'z', 'n', 'm'),
stringsAsFactors = FALSE)
您可以使用cumsum
。 这是使用一些numeric
数据的最小示例
df <- data.frame(A = 1:5, B = 6:10)
在基础 R
transform(df, A = A[1] + cumsum(c(0, B[-1])))
# A B
#1 1 6
#2 8 7
#3 16 8
#4 25 9
#5 35 10
或者使用dplyr
library(dplyr)
df %>% mutate(A = A[1] + cumsum(c(0, B[-1])))
给出相同的结果。
这是使用for
循环的答案:
# need to make sure they are not factors
df = data.frame(A = c('a','b','c', 'd', 'e'),
B = c('x', 'y', 'z', 'n', 'm'),
stringsAsFactors = F)
# start at 2, not 1, then get the previous row within the loop itself
for (i in 2:nrow(df)){
df$A[i] <- paste0(df$A[i-1], '+', df$B[i])
}
如果您希望它与数字数据一起使用,请使用
for (i in 2:nrow(df)){
df$A[i] <- df$A[i-1] + df$B[i]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.