Hi I have a data-frame which look like this X1 X2 X3 1 3 5 2 4 6 3 6 1 I want to apply a formula Y=6*X1 + 7*X2 + 8*X3 for every row Thanks in Advance -- View this message in context: http://r.789695.n4.nabble.com/how-to-call-a-function-for-each-row-tp4122906p4122906.html Sent from the R help mailing list archive at Nabble.com.
how to call a function for each row
6 messages · arunkumar1111, R. Michael Weylandt, jim holtman +3 more
Read ?apply This is easiest: df <- matrix(c(1,2,3,3,4,6,5,6,1), 3) apply(df, 1, function(x) 6*x[1]+7*x[2]+8*x[3]) But it's much more efficient to do it with matrix multiplication. In keeping with the best of tradition, this is left as an exercise to the reader. Michael
On Wed, Nov 30, 2011 at 8:10 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:
Hi ?I have ?a data-frame which look like this X1 X2 X3 1 ? 3 ?5 2 ? 4 ?6 3 ? 6 ?1 I want to apply a formula Y=6*X1 + 7*X2 + 8*X3 ?for every row Thanks in Advance -- View this message in context: http://r.789695.n4.nabble.com/how-to-call-a-function-for-each-row-tp4122906p4122906.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.
will this do it:
x <- read.table(text = "X1 X2 X3
+ 1 3 5 + 2 4 6 + 3 6 1", header = TRUE)
x
X1 X2 X3 1 1 3 5 2 2 4 6 3 3 6 1
apply(x, 1, function(a) 6 * a[1] + 7 * a[2] + 8 * a[3])
[1] 67 88 68
On Wed, Nov 30, 2011 at 8:10 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:
Hi ?I have ?a data-frame which look like this X1 X2 X3 1 ? 3 ?5 2 ? 4 ?6 3 ? 6 ?1 I want to apply a formula Y=6*X1 + 7*X2 + 8*X3 ?for every row Thanks in Advance -- View this message in context: http://r.789695.n4.nabble.com/how-to-call-a-function-for-each-row-tp4122906p4122906.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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Homework. ? I would say that this indiicates that you need to open the R tutorials and start reading. -- Bert
On Wed, Nov 30, 2011 at 5:10 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:
Hi ?I have ?a data-frame which look like this X1 X2 X3 1 ? 3 ?5 2 ? 4 ?6 3 ? 6 ?1 I want to apply a formula Y=6*X1 + 7*X2 + 8*X3 ?for every row Thanks in Advance -- View this message in context: http://r.789695.n4.nabble.com/how-to-call-a-function-for-each-row-tp4122906p4122906.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.
Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Isn't this even easier?
X1 <- c(1:3) X2 <- c(3, 4, 6) X3 <- c(5, 6, 1) Y <- 6*X1 + 7*X2 + 8*X3 Y
[1] 67 88 68 Or if you really need a function:
MakeY <- function(x, y, z) 6*x + 7*y + 8*z MakeY(X1, X2, X3)
[1] 67 88 68 ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of R. Michael Weylandt Sent: Wednesday, November 30, 2011 8:17 AM To: arunkumar1111 Cc: r-help at r-project.org Subject: Re: [R] how to call a function for each row Read ?apply This is easiest: df <- matrix(c(1,2,3,3,4,6,5,6,1), 3) apply(df, 1, function(x) 6*x[1]+7*x[2]+8*x[3]) But it's much more efficient to do it with matrix multiplication. In keeping with the best of tradition, this is left as an exercise to the reader. Michael
On Wed, Nov 30, 2011 at 8:10 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:
Hi ?I have ?a data-frame which look like this X1 X2 X3 1 ? 3 ?5 2 ? 4 ?6 3 ? 6 ?1 I want to apply a formula Y=6*X1 + 7*X2 + 8*X3 ?for every row Thanks in Advance -- View this message in context: http://r.789695.n4.nabble.com/how-to-call-a-function-for-each-row-tp41 22906p4122906.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.
______________________________________________ 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.
if you want to store the result in a column of your data.frame: within(df, Y <- 6*X1+7*X2+8*X3)
On Wed, Nov 30, 2011 at 9:59 AM, David L Carlson <dcarlson at tamu.edu> wrote:
Isn't this even easier?
X1 <- c(1:3) X2 <- c(3, 4, 6) X3 <- c(5, 6, 1) Y <- 6*X1 + 7*X2 + 8*X3 Y
[1] 67 88 68 Or if you really need a function:
MakeY <- function(x, y, z) 6*x + 7*y + 8*z MakeY(X1, X2, X3)
[1] 67 88 68 ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of R. Michael Weylandt Sent: Wednesday, November 30, 2011 8:17 AM To: arunkumar1111 Cc: r-help at r-project.org Subject: Re: [R] how to call a function for each row Read ?apply This is easiest: df <- matrix(c(1,2,3,3,4,6,5,6,1), 3) apply(df, 1, function(x) 6*x[1]+7*x[2]+8*x[3]) But it's much more efficient to do it with matrix multiplication. In keeping with the best of tradition, this is left as an exercise to the reader. Michael On Wed, Nov 30, 2011 at 8:10 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:
Hi ?I have ?a data-frame which look like this X1 X2 X3 1 ? 3 ?5 2 ? 4 ?6 3 ? 6 ?1 I want to apply a formula Y=6*X1 + 7*X2 + 8*X3 ?for every row Thanks in Advance -- View this message in context: http://r.789695.n4.nabble.com/how-to-call-a-function-for-each-row-tp41 22906p4122906.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.
______________________________________________ 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. ______________________________________________ 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.