[R] conditional Dataframe filling

Blaser Nello nblaser at ispm.unibe.ch
Wed Mar 27 11:02:54 CET 2013


Here's a possible solution. 

dd <- structure(list(a = c(TRUE, FALSE, FALSE),
                     b = c(TRUE, FALSE, TRUE),
                     c = c(TRUE, FALSE, FALSE),
                     d = c(TRUE, TRUE, FALSE)), 
                .Names = c("a", "b", "c", "d"), 
                row.names = c(NA, -3L), 
                class = "data.frame")

ds <- as.data.frame(t(apply(!dd, 1, cumsum)-apply(dd, 1, cumsum)))
ds[as.matrix(dd)] <- 0
ds

Best,
Nello

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Camilo Mora
Sent: Mittwoch, 27. März 2013 09:32
To: r-help at r-project.org
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