[R] Fill in empty spaces modified
Jim Lemon
drjimlemon at gmail.com
Thu Aug 10 00:01:40 CEST 2017
Hi Ek,
As I may have mentioned previously, if you don't mind stepping through
the data frame row by row you can do it like this:
eedf<-read.table(text="nam,val
mandy,1
,2
John,3
,4
,5
,6
Zara,7
,8",sep=",",header=TRUE,stringsAsFactors=FALSE)
for(row in 1:nrow(eedf))
if(eedf$nam[row] == "")
eedf$nam[row]<-eedf$nam[row-1]
Jim
On Thu, Aug 10, 2017 at 7:20 AM, Ek Esawi <esawiek at gmail.com> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list