Hi there I just want to add 2 to all the values in dataframe. I tried using sapply but it seem to die all the time. Any help would be appreciated. Thanks Ramya -- View this message in context: http://r.789695.n4.nabble.com/Adding-a-numeric-to-all-values-in-the-dataframe-tp3537594p3537594.html Sent from the R help mailing list archive at Nabble.com.
Jorge Ivan Velez
2011-May-20 04:18 UTC
[R] Adding a numeric to all values in the dataframe
Hi, Does the following work for you? set.seed(123) d <- data.frame(x = rpois(10, 4), y = rnorm(10)) d d + 2 See ?within for one more option. HTH, Jorge On Thu, May 19, 2011 at 11:54 PM, Ramya <> wrote:> Hi there > > I just want to add 2 to all the values in dataframe. > > I tried using sapply but it seem to die all the time. > > Any help would be appreciated. > > Thanks > Ramya > > -- > View this message in context: > http://r.789695.n4.nabble.com/Adding-a-numeric-to-all-values-in-the-dataframe-tp3537594p3537594.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Bill.Venables at csiro.au
2011-May-20 04:30 UTC
[R] Adding a numeric to all values in the dataframe
For that kind of operation (unusual as it is) work with numeric matrices. When you are finished, if you still want a data frame, make it then, not before. If your data starts off as data frame to begin with, turn it into a matrix first. E.g. myMatrix <- data.frame(myData) myMatrix2 <- myMatrix + 2 myData2 <- data.frame(myMatrix2) Bill Venables. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ramya Sent: Friday, 20 May 2011 1:54 PM To: r-help at r-project.org Subject: [R] Adding a numeric to all values in the dataframe Hi there I just want to add 2 to all the values in dataframe. I tried using sapply but it seem to die all the time. Any help would be appreciated. Thanks Ramya -- View this message in context: http://r.789695.n4.nabble.com/Adding-a-numeric-to-all-values-in-the-dataframe-tp3537594p3537594.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Bill.Venables at csiro.au
2011-May-21 01:37 UTC
[R] Adding a numeric to all values in the dataframe
Oops The first line of my template should use data.matrix() rather than data.frame() data.matrix() is guaranteed to return a numerical matrix from a data frame, making arithmetic always possible. Bill Venables. ________________________________________ From: Venables, Bill (CMIS, Dutton Park) Sent: 20 May 2011 14:30 To: 'Ramya'; 'r-help at r-project.org' Subject: RE: [R] Adding a numeric to all values in the dataframe For that kind of operation (unusual as it is) work with numeric matrices. When you are finished, if you still want a data frame, make it then, not before. If your data starts off as data frame to begin with, turn it into a matrix first. E.g. myMatrix <- data.frame(myData) myMatrix2 <- myMatrix + 2 myData2 <- data.frame(myMatrix2) Bill Venables. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ramya Sent: Friday, 20 May 2011 1:54 PM To: r-help at r-project.org Subject: [R] Adding a numeric to all values in the dataframe Hi there I just want to add 2 to all the values in dataframe. I tried using sapply but it seem to die all the time. Any help would be appreciated. Thanks Ramya -- View this message in context: http://r.789695.n4.nabble.com/Adding-a-numeric-to-all-values-in-the-dataframe-tp3537594p3537594.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.