Reputation: 1532
I want to create a column y
that mirrors the value of another column x
and sets the adjacent values to the non-NAs.
Let's say I've got a data.frame df
:
df = data.frame('index' = 1:10, 'x' = c(NA, NA, 1, NA, NA, NA, 2, NA, NA, NA))
> df
index x
1 1 NA
2 2 NA
3 3 1
4 4 NA
5 5 NA
6 6 NA
7 7 2
8 8 NA
9 9 NA
10 10 NA
Now I want to create the column df$y
which forms some 'context' around the non-NAs in df$x
. Specifically, value of these df$x
(here: 1 and 2) applies to the their index PLUS one before and one after their index, so that:
> df
index x y
1 1 NA NA
2 2 NA 1
3 3 1 1
4 4 NA 1
5 5 NA NA
6 6 NA 2
7 7 2 2
8 8 NA 2
9 9 NA NA
10 10 NA NA
I've tried to do this by finding the relevant starting and ending indices of the "context" around the 1 and the 2 with:
temp_list = sapply(df$index, function(i){
if(!is.na(df$x[i])){
target_index_start = i - 1
target_index_end = i + 1
mini_context_iter = df$x[target_index_start:target_index_end]
} else {
NULL
}
})
... and this returns a nice list.
The problem is that this does not seem to handle the indices that are outside of the -1:+1
context. A related question is this SO post but it stops before creating the new column.
Any ideas how I could address this more precisely?
Upvotes: 0
Views: 57
Reputation: 79338
library(tidyverse)
df%>%mutate(y=coalesce(x,lead(x),lag(x)))
index x y
1 1 NA NA
2 2 NA 1
3 3 1 1
4 4 NA 1
5 5 NA NA
6 6 NA 2
7 7 2 2
8 8 NA 2
9 9 NA NA
10 10 NA NA
Upvotes: 1