Skip to content
Prev 278899 / 398502 Next

how to call a function for each row

Isn't this even easier?
[1] 67 88 68

Or if you really need a function:
[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:
______________________________________________
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.