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.
You’ll need to use
mapplyfor this, or even more convienientmdplyfrom the plyr package.Some example code:
If you just want the vector of results:
Or alternatively, a base R solution using
mapply: