Reputation: 71
I am wondering if it is possible to use dplyr
's mutate
function to add a new column to a data frame that is a function of multiple row values from other columns. For example, if I had a data frame df
with columns a
and b
and wanted to make a new column c
, where c = a[n] + b[n +1]
, can I use mutate
with a for
loop?
I tried the below code but it didn't work:
for(n in 1:nrow(df)){
df %>%
mutate(c = a[n] + b[n + 1])
}
Upvotes: 1
Views: 446
Reputation: 7248
The function you're probably looking for it lead
, to get a view of b
offset by one record. Then the command would be
df %>% mutate(c=a + lead(b, 1))
For example,
> data.frame(a=1:10, b=1:10) %>% mutate(c = a + lead(b, 1))
a b c
1 1 1 3
2 2 2 5
3 3 3 7
4 4 4 9
5 5 5 11
6 6 6 13
7 7 7 15
8 8 8 17
9 9 9 19
10 10 10 NA
Upvotes: 4