Re: [R] Applying a function to a column of a data frame

2012-06-09 Thread David Winsemius
On Jun 9, 2012, at 1:02 PM, Onur Uncu wrote: Thank you Michael. One follow up question: In the solution using apply(), why are we feeding x[2] inside testfun instead of x[[2]]? Aren't we supposed to feed the vector of values to the function, which is achieved by x[[2]]? `apply` sends the colu

Re: [R] Applying a function to a column of a data frame

2012-06-09 Thread Onur Uncu
Thank you Michael. One follow up question: In the solution using apply(), why are we feeding x[2] inside testfun instead of x[[2]]? Aren't we supposed to feed the vector of values to the function, which is achieved by x[[2]]? Thnks. On Sat, Jun 9, 2012 at 3:38 PM, R. Michael Weylandt wrote: > N

Re: [R] Applying a function to a column of a data frame

2012-06-09 Thread R. Michael Weylandt
No worries -- it's an important question and it introduces you to one of the most important R idioms. [And you get bonus points for having a well formed question!] You're probably looking for something like this: apply(testframe, 1, function(x) testfun(x[2])) Which goes row-by-row and substitute

[R] Applying a function to a column of a data frame

2012-06-09 Thread Onur Uncu
Apologees the novice question. Currently climbing up the learning curve of R. Suppose I have the following function and the data.frame: testfun<-function(x=1,y=2) x+y testframe=data.frame(col1=c(1,2),col2=c(3,4)) When evaluating testfun, I want to use the default value for y (which is 2) and fo