Home > Enterprise >  Copy content from cell in a row to adjacent right-hand cell in R
Copy content from cell in a row to adjacent right-hand cell in R

Time:06-28

I have a data frame that looks like this:

 df <- data.frame(col1=c(NA, NA),
                  col2=c("arroz", "unit"),
                  col3=c(NA, "area"),
                  col4=c("arveja", "unit"),
                  col5=c(NA, "area"),
                  col6=c("frejol", "unit"),
                  col7=c(NA, "area"))

  col1  col2 col3   col4 col5   col6 col7
1   NA arroz <NA> arveja <NA> frejol <NA>
2   NA  unit area   unit area   unit area
                  

As you can see, there are NAs in the first row. I would like to copy the content of the non-NA cells and paste it into the right-hand NA cell, so that I obtain a df like this; the very first column has to stay as NA:

df_output <- data.frame(col1=c(NA, NA),
                         col2=c("arroz", "unit"),
                         col3=c("arroz", "area"),
                         col4=c("arveja", "unit"),
                         col5=c("arveja", "area"),
                         col6=c("frejol", "unit"),
                         col7=c("frejol", "area"))

  col1  col2  col3   col4   col5   col6   col7
1   NA arroz arroz arveja arveja frejol frejol
2   NA  unit  area   unit   area   unit   area

This is a toy example from larger dataset.

Thanks!!

CodePudding user response:

library(tidyverse)

df <- data.frame(col1=c(NA, NA),
                 col2=c("arroz", "unit"),
                 col3=c(NA, "area"),
                 col4=c("arveja", "unit"),
                 col5=c(NA, "area"),
                 col6=c("frejol", "unit"),
                 col7=c(NA, "area")) %>% 
  as_tibble()


df %>%
  pivot_longer(-col1) %>%  
  fill(value) %>%  
  pivot_wider(names_from = name, values_from = value) %>%  
  unnest()

# A tibble: 2 x 7
  col1  col2  col3  col4   col5   col6   col7  
  <lgl> <chr> <chr> <chr>  <chr>  <chr>  <chr> 
1 NA    arroz arroz arveja arveja frejol frejol
2 NA    unit  area  unit   area   unit   area  

CodePudding user response:

You can apply the na.locf0 on all rows using the following code:

library(zoo)
data.frame(t(apply(df, 1, na.locf0)))

Output:

    X1    X2    X3     X4     X5     X6     X7
1 <NA> arroz arroz arveja arveja frejol frejol
2 <NA>  unit  area   unit   area   unit   area
  • Related