Dear all, maybe somebody can provide some help for this problem: Example: I've got the following dataframe "data": grid.id<-c(1:4) lat<-c(10,12,13,15) species1<-c(0,0,0,1) species2<-c(1,1,0,0) species3<-c(1,1,1,1) data<-data.frame(cbind(grid.id,lat,species1,species2,species3)) How can I, out of "data" make a new dataframe, where the cells of value "1" in the species columns ("species1" to "species3") are replaced by the respective "lat" values automatically - so that the final dataframe looks like this: specieslat1<-c(0,0,0,15) specieslat2<-c(10,12,0,0) specieslat3<-c(10,12,13,15) data.frame(cbind(grid.id,lat,specieslat1,specieslat2,specieslat3)) ? Thanks a lot, cheers Christian -- Christian Hof .. PhD student Biodiversity & Global Change Lab .. C/Jos? Guti?rrez Abascal, 2 Museo Nacional de Ciencias Naturales .. E-28006 Madrid (Espa?a) www.biochange-lab.eu Center for Macroecology .. University of Copenhagen www.macroecology.ku.dk mobile ES .. +34 697 508 519 mobile DE .. +49 176 205 189 27 mail .. chof at bi.ku.dk blog .. www.vogelwart.de
Christian Try: data.frame(data[,1:2], data[,3:5]*data[,2]) Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Christian Hof > Sent: Thursday, 31 January 2008 7:48 a.m. > To: r-help at r-project.org > Subject: [R] data.frame transformation > > Dear all, > maybe somebody can provide some help for this problem: > > Example: > I've got the following dataframe "data": > > grid.id<-c(1:4) > lat<-c(10,12,13,15) > species1<-c(0,0,0,1) > species2<-c(1,1,0,0) > species3<-c(1,1,1,1) > data<-data.frame(cbind(grid.id,lat,species1,species2,species3)) > > How can I, out of "data" make a new dataframe, where the > cells of value "1" in the species columns ("species1" to > "species3") are replaced by the respective "lat" values > automatically - so that the final dataframe looks like this: > > specieslat1<-c(0,0,0,15) > specieslat2<-c(10,12,0,0) > specieslat3<-c(10,12,13,15) > data.frame(cbind(grid.id,lat,specieslat1,specieslat2,specieslat3)) > > ? > > Thanks a lot, > cheers > Christian > > -- > Christian Hof .. PhD student > Biodiversity & Global Change Lab .. C/Jos? Guti?rrez > Abascal, 2 Museo Nacional de Ciencias Naturales .. E-28006 > Madrid (Espa?a) > www.biochange-lab.eu > Center for Macroecology .. University of Copenhagen > www.macroecology.ku.dk > mobile ES .. +34 697 508 519 > mobile DE .. +49 176 205 189 27 > mail .. chof at bi.ku.dk > blog .. www.vogelwart.de > > ______________________________________________ > 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. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.
hits=-2.6 tests=BAYES_00 X-USF-Spam-Flag: NO Hi Christian, Use the fact that 0 * lat[i] == 0 and 1 * lat[i] == lat[i], where lat[i] is one of your lat values. Also use the fact that R is vectorized. So we have: grid.id<-c(1:4) lat<-c(10,12,13,15) species1<-c(0,0,0,1) species2<-c(1,1,0,0) species3<-c(1,1,1,1) ## don't need cbind here & ## data is not a good name, use something else df <- data.frame(grid.id, lat, species1, species2, species3) So to get what you want: df[,-(1:2)] * df$lat and if you want to assign this into df to make the change permanent: df[,-(1:2)] <- df[,-(1:2)] * df$lat We drop the first two columns so you don't need to know how many species you have, just that there are two columns we don't want to mess with. HTH G On Wed, 2008-01-30 at 19:47 +0100, Christian Hof wrote:> Dear all, > maybe somebody can provide some help for this problem: > > Example: > I've got the following dataframe "data": > > grid.id<-c(1:4) > lat<-c(10,12,13,15) > species1<-c(0,0,0,1) > species2<-c(1,1,0,0) > species3<-c(1,1,1,1) > data<-data.frame(cbind(grid.id,lat,species1,species2,species3)) > > How can I, out of "data" make a new dataframe, where the cells of value > "1" in the species columns ("species1" to "species3") are replaced by > the respective "lat" values automatically - so that the final dataframe > looks like this: > > specieslat1<-c(0,0,0,15) > specieslat2<-c(10,12,0,0) > specieslat3<-c(10,12,13,15) > data.frame(cbind(grid.id,lat,specieslat1,specieslat2,specieslat3)) > > ? > > Thanks a lot, > cheers > Christian >-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%