I’m trying to apply a function to all the 334 rows of data frame M, which contains time and position data and get one value for each row. Instead, I get a list of 334 values for each row. How can I simply get one value per row calculated from the values of the variables of that same row?
These are the head and the tail of data frame M:
d mo y lat long
5 6 2007 NA NA
6 6 2007 NA NA
7 6 2007 NA NA
8 6 2007 26.89 15.53
9 6 2007 28.00 15.73
10 6 2007 22.41 14.93
...
26 4 2008 23.86 14.05
27 4 2008 24.12 14.34
28 4 2008 27.75 12.87
29 4 2008 27.28 10.91
30 4 2008 24.17 14.44
1 5 2008 NA NA
My code:
f1 = function(x){
options(latitude= M$lat, longitude= M$long);
as.lt(moon.rst(jday = jd(M$y,M$mo,M$d)))
}
M$rs <- apply(M, 1, f1)
There is a warning from the function
jd()since we are passing vectors (in this caseyear) as an argument, but despite this I hope that is the result that you need.Edit: Another version without any warnings, using
apply, but using indices and it seemed for me thatdo.callis faster.