简体   繁体   中英

Creating a column that repeats adjacent values in R data.frame

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM