[R] Fill in empty spaces modified

Ek Esawi esawiek at gmail.com
Wed Aug 9 23:20:45 CEST 2017


Hi All—

I was looking at a posting from June-17. I managed to solve it. However,
when I changed the example in the posting, my solution will work only once
at a time which was mentioned by Jim Lemon  on his response to the original
posting. This means that my solution will have to be repeated as many times
as the maximum number of spaces on each gap; something that may not work
well for large files.

I am trying to solve the new example with base R functions only. I thought
of splitting the first column to multiple lists and use one of the apply
functions, but was not successful.

Would apprecaite some hints on how to go about it.

Thanks as always in advance—EK

The posted data frame from the original posting:
names val
1 Mandy   1
2         2
3 John    2
4         2

My modified data frame:
val <- c(1,2,3,4,5,6,7,8)
nam <- c("mandy","", "John","","","","Zara","")
df1 <- data.frame(nam,val)

    nam val
1 mandy   1
2         2
3  John   3
4         4
5         5
6         6
7  Zara   7
8         8

My code for solving the original data farme
> which(df1$nam=="")
> a <- which(df1$nam=="")
> df1$nam[a] <- df1$nam[a-1]

	[[alternative HTML version deleted]]



More information about the R-help mailing list