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
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(.)
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.
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.
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)])
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With