Stuart Luppescu
2010-Jun-29 18:54 UTC
[R] Conditionally constructing columns in a data frame
Hello, I have to construct 5 new columns in a data frame depending on the value of another of the columns in the data frame. The only way I could figure out to do this was to subset the data frame five times, do the variable construction, and then rbind the subsets back together. Here's part of the code I used: read001 <- read[read$existstr=="001",] read001$era1end <- NA read001$era2base <- NA read001$era2end <- NA read001$era3base <- read001$era1base read001$era3end <- read001$era3base + (6 * read001$era3tr) read011 <- read[read$existstr=="011",] read011$era1end <- NA read011$era2base <- read011$era1base read011$era2end <- read011$era2base + (4 * read011$era2tr) read011$era3base <- read011$era2end read011$era3end <- read011$era2end + (6 * read011$era3tr) ... read2 <- rbind(read001, read011, read100, read110, read111) Isn't there an easier way to do this? Thanks. -- Stuart Luppescu <slu at ccsr.uchicago.edu> University of Chicago
I'm sure there's an easier way, but it's going to be easiest to get a useful response if we have a reproducible, minimal example, as the posting guide requests. ?tapply is probably involved. Stuart Luppescu wrote:> Hello, I have to construct 5 new columns in a data frame depending on > the value of another of the columns in the data frame. The only way I > could figure out to do this was to subset the data frame five times, do > the variable construction, and then rbind the subsets back together. > Here's part of the code I used: > > read001 <- read[read$existstr=="001",] > > read001$era1end <- NA > read001$era2base <- NA > read001$era2end <- NA > read001$era3base <- read001$era1base > read001$era3end <- read001$era3base + (6 * read001$era3tr) > > read011 <- read[read$existstr=="011",] > > read011$era1end <- NA > read011$era2base <- read011$era1base > read011$era2end <- read011$era2base + (4 * read011$era2tr) > read011$era3base <- read011$era2end > read011$era3end <- read011$era2end + (6 * read011$era3tr) > > ... > > read2 <- rbind(read001, read011, read100, read110, read111) > > > Isn't there an easier way to do this? > > Thanks. >
David Winsemius
2010-Jun-29 19:36 UTC
[R] Conditionally constructing columns in a data frame
On Jun 29, 2010, at 2:54 PM, Stuart Luppescu wrote:> Hello, I have to construct 5 new columns in a data frame depending on > the value of another of the columns in the data frame. The only way I > could figure out to do this was to subset the data frame five times, > do > the variable construction, and then rbind the subsets back together. > Here's part of the code I used: > > read001 <- read[read$=="001",] > > read001$era1end <- NA > read001$era2base <- NA > read001$era2end <- NA > read001$era3base <- read001$era1base > read001$era3end <- read001$era3base + (6 * read001$era3tr) > > read011 <- read[read$existstr=="011",] > > read011$era1end <- NA > read011$era2base <- read011$era1base > read011$era2end <- read011$era2base + (4 * read011$era2tr) > read011$era3base <- read011$era2end > read011$era3end <- read011$era2end + (6 * read011$era3tr) > > ...?split processed_list <- split(read, read$existstr) # then you have the dataframe in sections determined by existstr's value # process within groups (but your example does not generalize in an obvious manner.) # then: final <- do.call(rbind , processed_list) -- David.> > read2 <- rbind(read001, read011, read100, read110, read111) > > > Isn't there an easier way to do this? > > Thanks. > > -- > Stuart Luppescu <slu at ccsr.uchicago.edu> > University of Chicago > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT