tkrex
tkrex

Reputation: 49

Applying a function to a data frame

I can't figure out why this isn't working. I have a data set with 5 columns, n rows. I just want to apply a function to each row and have the result returned in an n by 1 vector.

Just to test out how everything works, i made this simple function:

f1 <- function(uniqueid,Perspvalue,expvalue,stddevi,stddevc) {
    uniqueid+ Perspvalue- expvalue+ stddevi+stddevc
}

and here's the first few rows of my data set:

> data
     uniqueid   Perspvalue    expvalue      stddevi      stddevc
1           1 2.404421e+03  3337239.00 8.266566e+03 3.324624e+03
2           2 1.345307e+03  3276559.87 7.068823e+03 2.648072e+03
3           3 1.345307e+03  3276559.87 7.068823e+03 2.648072e+03

Note that it's a data frame (i think), and not a matrix. I loaded in the data from a csv using read.csv.

So i try this: apply(data,1,f1)

But my result is this: Error in uniqueid + Perspvalue : 'Perspvalue' is missing

I expected a number instead of an error.

Upvotes: 0

Views: 1122

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60934

You'll need to use mapply for this, or even more convienient mdply from the plyr package.

Some example code:

spam_function = function(a, b) {
  return(a*b)
}

require(plyr)
input_args = data.frame(a = runif(1000), b = runif(1000))
result = mdply(input_args, spam_function)
> head(result)
           a         b         V1
1 0.46902575 0.6865863 0.32202668
2 0.56837805 0.2400993 0.13646717
3 0.07185661 0.2334754 0.01677675
4 0.15589191 0.6636891 0.10346377
5 0.98317092 0.8895609 0.87459042
6 0.46070479 0.4301685 0.19818071

If you just want the vector of results:

result_vector = result$V1

Or alternatively, a base R solution using mapply:

result_mapply = mapply(spam_function, a = input_args$a, b = input_args$b)
> head(result_mapply)
[1] 0.2757767 0.1268879 0.5851026 0.7904186
[5] 0.2186079 0.1091692

Upvotes: 7

Related Questions