filling up string in R studio

I would like to fill up string in this data set , so far fill up does not work , any other way to make it ?  .

          X1 
1               
2               
3     sunday    
4               
5               
6               
7               
8     monday    
9               
10   tuesday    
11              
12              
13              
14              
15 wednesday    

I tried this but it just fill a couple of spaces and I have to run this many times so that it can be complete.

for(i in 1:nrow(x))

if(x$X1[i]=="")
x$X1[i] <- x$X1[i+1]

x

Are you looking for something like this?

DF <- data.frame(X1 = c(NA,NA,"Monday", NA,NA,NA,NA,"Tuesday",NA,NA,"Wednesday"))
DF
#>           X1
#> 1       <NA>
#> 2       <NA>
#> 3     Monday
#> 4       <NA>
#> 5       <NA>
#> 6       <NA>
#> 7       <NA>
#> 8    Tuesday
#> 9       <NA>
#> 10      <NA>
#> 11 Wednesday
library(tidyr)
DF$X1 <- fill(DF, X1, .direction = "up")
DF
#>           X1
#> 1     Monday
#> 2     Monday
#> 3     Monday
#> 4    Tuesday
#> 5    Tuesday
#> 6    Tuesday
#> 7    Tuesday
#> 8    Tuesday
#> 9  Wednesday
#> 10 Wednesday
#> 11 Wednesday

Created on 2023-03-24 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.