Skip to content
Prev 320424 / 398502 Next

conditional Dataframe filling

Dear Camilo,

How do you want to deal with the NAs?

If I remove the NAs:
dat1 <- structure(list(
w = c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE),
x = c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),
y = c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE),
z = c(TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,FALSE)),
row.names = c(NA, -13L),
class = "data.frame")

dat1<-t(dat1)
colnames(dat1)<-c("a","b","c","d","e","f","g","h","i","j","k", "l","m")
dat1<- as.data.frame(na.omit(dat1))
dat2<-dat1
dat2[]<-t(apply(!dat1,1,function(x) unlist(lapply(split(x,cumsum(c(0,abs(diff(x))))),cumsum))))
?dat2
#? a b c d e f g h i j k l m
#w 0 0 0 0 0 1 2 3 4 0 0 0 0
#y 1 2 3 4 5 0 0 1 2 0 0 0 1
#z 0 0 0 0 1 0 0 0 1 0 0 0 1


?dat1
#????? a???? b???? c???? d???? e???? f???? g???? h???? i??? j??? k??? l???? m
#w? TRUE? TRUE? TRUE? TRUE? TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE? TRUE
#y FALSE FALSE FALSE FALSE FALSE? TRUE? TRUE FALSE FALSE TRUE TRUE TRUE FALSE
#z? TRUE? TRUE? TRUE? TRUE FALSE? TRUE? TRUE? TRUE FALSE TRUE TRUE TRUE FALSE


A.K.





----- Original Message -----
From: Camilo Mora <cmora at dal.ca>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, March 27, 2013 3:27 PM
Subject: Re: [R] conditional Dataframe filling

Dear Arun,

Thank you very? much for your help with this.I did not know where to start looking to solve that problem, so I truly appreciate your input.

The line of code you sent seems to work but it duplicates the results. Do you know why that may happen?
Below is a larger database, to which I apply your line of code.

Thank you very much again,
Camilo


dat1 <- structure(list(
w = c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE),
x = c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),
y = c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE),
z = c(TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,FALSE)),
row.names = c(NA, -13L),
class = "data.frame")

dat1<-t(dat1)
colnames(dat1)<-c("a","b","c","d","e","f","g","h","i","j","k", "l","m")

dat2<-dat1

dat2[]<-t(apply(!dat1,1,function(x) unlist(lapply(split(x,cumsum(c(0,abs(diff(x))))),cumsum))))













Camilo Mora, Ph.D.
Department of Geography, University of Hawaii
Currently available in Colombia
Phone:?  Country code: 57
? ? ? ?  Provider code: 313
? ? ? ?  Phone 776 2282
? ? ? ?  From the USA or Canada you have to dial 011 57 313 776 2282
http://www.soc.hawaii.edu/mora/



Quoting arun <smartpink111 at yahoo.com>: