[R] conditional Dataframe filling

Camilo Mora cmora at dal.ca
Wed Mar 27 20:27:33 CET 2013


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>:

> HI,
>
> Just a correction:
>
> :
>
> dat2[]<-t(apply(!dat1,1,function(x)  
> unlist(lapply(split(x,cumsum(c(0,abs(diff(x))))),cumsum))))  #should  
> also work
> A.K.
>
>
>
> ----- Original Message -----
> From: arun <smartpink111 at yahoo.com>
> To: Camilo Mora <cmora at dal.ca>
> Cc: R help <r-help at r-project.org>
> Sent: Wednesday, March 27, 2013 9:09 AM
> Subject: Re: [R] conditional Dataframe filling
>
>
>
> Hi,
> You could try:
> dat1<- read.table(text="
> a    b    c    d
> TRUE  TRUE  TRUE  TRUE
> FALSE FALSE FALSE TRUE
> FALSE  TRUE  FALSE  FALSE
> ",sep="",header=TRUE)
> dat2<-dat1
>  dat2[]<-t(apply(1*!dat1,1,function(x)  
> unlist(lapply(split(x,cumsum(c(0,abs(diff(x))))),cumsum))))
>  dat2
> #  a b c d
> #1 0 0 0 0
> #2 1 2 3 0
> #3 1 0 1 2
> A.K.
>
>
> ----- Original Message -----
> From: Camilo Mora <cmora at dal.ca>
> To: r-help at r-project.org
> Cc:
> Sent: Wednesday, March 27, 2013 4:31 AM
> Subject: [R] conditional Dataframe filling
>
> Hi everyone:
>
> This may be trivial but I just have not been able to figure it out.
>
> Imagine the following dataframe:
> a     b     c     d
> TRUE  TRUE  TRUE  TRUE
> FALSE FALSE FALSE TRUE
> FALSE  TRUE  FALSE  FALSE
>
> I would like to create a new dataframe, in which TRUE gets 0 but if  
> false then add 1 to the cell to the left. So the results for the  
> example above should be something like:
>
> a     b     c     d
> 0     0     0     0
> 1     2     3     0
> 1     0     1     2
>
> I wonder if you may know?.
>
> Thanks,
>
> Camilo
>
>
>
>
> 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/
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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