Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill down every other row with level above in tidyverse

Tags:

r

dplyr

tidyr

I'm learning R and am hitting a snag with tidry and dplyr.

I've got a data frame in R where the first column is a factor that only has a level every other row. I'm trying to figure out how to use tidyverse tools to fill down.

I've tried using fill and replace but neither seems to work.

td <- data.frame("State" = c("NY", "", "OH", ""), "Your" = c(101:104), "Name" = c(5:8))

td
State    Your    Name
<fctr>   <int>   <int>
NY       101     5      
         102     6      
OH       103     7      
         104     8  

What I want to get is

State    Your    Name
<fctr>   <int>   <int>
NY       101     5      
NY       102     6      
OH       103     7      
OH       104     8  

using tidyr or dplyr

like image 200
ajbentley Avatar asked Sep 28 '18 15:09

ajbentley


People also ask

How do I sum across rows in R Dplyr?

Syntax: mutate(new-col-name = rowSums(.)) The rowSums() method is used to calculate the sum of each row and then append the value at the end of each row under the new column name specified. The argument . is used to apply the function over all the cells of the data frame. Syntax: rowSums(.)

Is Tidyr in Tidyverse?

tidyr is the Tidyverse package for getting data frames to tidy. Recall that in a tidy data frame: each row is a unit of observation. each column is a single piece of information.

In which either the previous value or the next value is used to fill the missing value?

Fills missing values in selected columns using the next or previous entry. This is useful in the common output format where values are not repeated, and are only recorded when they change.


2 Answers

One option is to replace the blanks ("") with NA (na_if) and use fill from tidyr to fill the NA elements with the non-NA adjacent element before it

library(tidyverse)
td %>% 
   mutate(State = na_if(State, "")) %>%
   fill(State)
#    State Your Name
#1    NY  101    5
#2    NY  102    6
#3    OH  103    7
#4    OH  104    8

Or using base R

i1 <- td$State != ""
td$State <- with(td, State[i1][cumsum(i1)])
like image 148
akrun Avatar answered Sep 25 '22 19:09

akrun


Solution from zoo

library(zoo)
td[td=='']=NA
zoo::na.locf(td)
  State Your Name
1    NY  101    5
2    NY  102    6
3    OH  103    7
4    OH  104    8
like image 31
BENY Avatar answered Sep 25 '22 19:09

BENY