Esmail Bonakdarian
2008-Jun-12 01:26 UTC
[R] Adding new columns to (output) data - e.g., read 5 cols write 8
Hello, I have the following task I'd like to accomplish: A file contains 5 columns of data (several hundred rows), let's call them a, b, c, d and e (ie these are their column headers) I also have a set of definitions, e.g., f = a + b g = a * 3 h = c + d etc. I would like to write out a new .rda file that contains columns a b c d e f g h etc. I.e. , the original data plus new columns (with headers and data). It seems that there ought to be a simple way to do this, could someone provide some guidance on the best way to accomplish this task? (I tried a few things as this seemed rather trivial, but did not succeed. I still hope/assume this is a trivial thing to do if one knows R well) Thanks, Esmail ps: I want to thank everyone again who posted their solutions to my previous query. Seeing different solutions for the same problem is a tremendously effective way to learn something new.
jim holtman
2008-Jun-12 01:45 UTC
[R] Adding new columns to (output) data - e.g., read 5 cols write 8
yourDF <- cbind(yourDF, f=yourDF$a+yourDF$b, g=yourDF$a * 3, h=yourDF$c + yourDF$d) On Wed, Jun 11, 2008 at 9:26 PM, Esmail Bonakdarian <esmail.js at gmail.com> wrote:> Hello, I have the following task I'd like to accomplish: > > A file contains 5 columns of data (several hundred rows), let's call > them a, b, c, d and e (ie these are their column headers) > > I also have a set of definitions, e.g., > > f = a + b > g = a * 3 > h = c + d > etc. > > I would like to write out a new .rda file that contains columns > > a b c d e f g h etc. > > I.e. , the original data plus new columns (with headers and data). > > It seems that there ought to be a simple way to do this, could > someone provide some guidance on the best way to accomplish > this task? (I tried a few things as this seemed rather trivial, > but did not succeed. I still hope/assume this is a trivial thing > to do if one knows R well) > > Thanks, > > Esmail > > ps: I want to thank everyone again who posted their solutions > to my previous query. Seeing different solutions for the same > problem is a tremendously effective way to learn something > new. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Erik Iverson
2008-Jun-12 01:48 UTC
[R] Adding new columns to (output) data - e.g., read 5 cols write 8
Esmail - Esmail Bonakdarian wrote:> Hello, I have the following task I'd like to accomplish: > > A file contains 5 columns of data (several hundred rows), let's call > them a, b, c, d and e (ie these are their column headers)Are these 5 vectors of data stored in a data.frame? I assume so.> > I also have a set of definitions, e.g., > > f = a + b > g = a * 3 > h = c + d > etc. > > I would like to write out a new .rda file that contains columns > > a b c d e f g h etc. > > I.e. , the original data plus new columns (with headers and data).Example, test <- data.frame(a = 1:10, b = 2:11, c = 3:12) test2 <- transform(test, d = 2*a + b, e = 3*c) save(test2, file = "test2.Rdata") Does this help? Best, Erik Iverson
Esmail Bonakdarian
2008-Jun-12 02:53 UTC
[R] Adding new columns to (output) data - e.g., read 5 cols write 8
jim holtman wrote:> yourDF <- cbind(yourDF, f=yourDF$a+yourDF$b, g=yourDF$a * 3, > h=yourDF$c + yourDF$d)Thanks Jim, I also learned about the transform() method from Erik which will also work beautifully. Esmail