Reputation: 113
I am trying to perform a linear interpolation of mean water level for each data frame contained in a list of data frames (1038 in total) using the apply family of functions, but I am having trouble defining the variables in the interpolation function to point to columns within each data frame.
Here is the head of my list:
head(df_list)
$`1928-01-01`
station date_measured daily_mean max min mean lon lat
8267 20250000 1928-01-01 0 453 193 272 -49.5519 -15.2753
8268 20250000 1928-01-01 0 453 191 276 -49.5519 -15.2753
$`1928-02-01`
station date_measured daily_mean max min mean lon lat
8269 20250000 1928-02-01 0 NA NA NA -49.5519 -15.2753
8270 20250000 1928-02-01 0 NA NA NA -49.5519 -15.2753
$`1928-03-01`
station date_measured daily_mean max min mean lon lat
8271 20250000 1928-03-01 0 394 219 282 -49.5519 -15.2753
8272 20250000 1928-03-01 0 382 218 281 -49.5519 -15.2753
$`1928-04-01`
station date_measured daily_mean max min mean lon lat
8273 20250000 1928-04-01 0 280 176 224 -49.5519 -15.2753
8274 20250000 1928-04-01 0 287 178 223 -49.5519 -15.2753
$`1928-05-01`
station date_measured daily_mean max min mean lon lat
8275 20250000 1928-05-01 0 199 161 172 -49.5519 -15.2753
8276 20250000 1928-05-01 0 197 162 173 -49.5519 -15.2753
$`1928-06-01`
station date_measured daily_mean max min mean lon lat
8277 20250000 1928-06-01 0 174 132 149 -49.5519 -15.2753
8278 20250000 1928-06-01 0 173 132 149 -49.5519 -15.2753
This is what I tried originally:
daily_int <- lapply(df_list, function(x) interp(x=lon,y=lat,z=mean, method="linear"))
Which resulted in the following error:
Error in interp(x = lon, y = lat, z = mean, method = "linear") :
object 'lat' not found
I realize the interp function is not finding the column that I want it to look at, and being new to the apply family, I'm not sure how to do that (or if it's even possible). Essentially, I need to interpolate daily water levels across a river, and I'd like to do it in the most efficient way possible while keeping the days separate.
Edit based on comment: I'm trying to predict the mean, min, and max for each data frame.
Upvotes: 0
Views: 143
Reputation: 451
I am unfamiliar with the interp
function you are using but in general if you want to access the column names of the data frames in an lapply
loop the following should work:
daily_int <- lapply(df_list, function(x) interp(x=x$lon,y=x$lat,z=x$mean, method="linear"))
Does that work? If not maybe this will work:
daily_int <- lapply(df_list, function(x) interp(x=x[["lon"]],y=x[["lat"]],z=x[["mean"]], method="linear"))
Upvotes: 1
Reputation: 531
Im not exactly sure which values you are trying to predict for using the interp function
As far as the lapply goes: each value in the list becomes the x in the function.
So using with()
you can assign the data frame you are working with in the interp function.
daily_int <- lapply(df_list, function(x) {
out <- with(x,interp(x=lon,y=lat,z=mean, method="linear"))
return(out)
})
i dont have R open to test this and am not familiar with interp off the top of my head, so will update later!
Upvotes: 2