#r #function #for-loop #datatable #across
Вопрос:
У меня есть таблица данных с информацией за 21 год (2000-2020), в которой у меня есть 1 столбец для каждого ежемесячного наблюдения (январь-декабрь), например: таблица данных
Мне нужно повторить функцию через все 20 лет (строки) и 12 месяцев (столбцы). Это мой текущий код:
jan_2020 lt;-subset(jan_2020, ano == 2020) jan_2020 lt;- jan_2020[,c("Latitude","Longitude","jan")] jan_2020 lt;- jan_2020[, Latitude:=round(Latitude,2)] jan_2020 lt;- jan_2020[, Longitude:=round(Longitude,2)] # Triangular Irregular Surface jan_2020 fit_jan_2020 lt;- interp::interp( # using {interp} x = jan_2020$Latitude, # the function actually accepts coordinate vectors y = jan_2020$Longitude, z = jan_2020$jan, xo = grd_template$X, # here we already define the target grid yo = grd_template$Y, output = "points" ) %gt;% bind_cols() # data table jan_2020 jan_2020_interp lt;- fit_jan_2020 %gt;% as.data.table() jan_2020_interp lt;- jan_2020_interp[complete.cases(jan_2020_interp),] names(jan_2020_interp) lt;- c('Latitude','Longitude','jan') jan_2020_interp lt;- jan_2020_interp[, Ano :=2020]
Поэтому я повторяю это для каждого месяца («январь», «февраль», «март», …) и для каждого года / «ано» (2000, 2001, …).
В конце концов, я должен все это объединить, вот так:
interpolado_2020 lt;- merge(interpolado_2020, mar_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, abr_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, mai_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, jun_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, jul_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, ago_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, set_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, out_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, nov_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE ) interpolado_2020 lt;- merge(interpolado_2020, dez_2020_interp, by= c("Latitude","Longitude","Ano"),all.x=TRUE )
Я хотел бы использовать функцию, чтобы повторить этот процесс во всех столбцах за 12 месяцев и 21 год (строки фильтра).
Я кое-что начал, но в конце концов не могу обобщить результаты…
Вот что я попробовал:
for(i in anos_interp_1){ painel_interp lt;- painel_temp[,c("Latitude","Longitude","jan","fev","mar","abr", "mai","jun","jul","ago","set","out","nov","dez","ano")] painel_interp lt;- painel_interp %m% mutate(across(c("jan","fev","mar","abr", "mai","jun","jul","ago", "set","out","nov","dez")),.fns=function(x) subset(painel_interp, ano == i), names(painel_interp)lt;-c("Latitude","Longitude","Mes","ano"), painel_interp[, Latitude:=round(Latitude,2)], painel_interp[, Longitude:=round(Longitude,2)], fit lt;- interp::interp( # using {interp} x = painel_interp$Latitude, # the function actually accepts coordinate vectors y = painel_interp$Longitude, z = painel_interp$Mes, xo = grd_template$X, # here we already define the target grid yo = grd_template$Y, output = "points" ) %gt;% bind_cols(), mes_interp lt;- fit %gt;% as.data.table(), mes_interp[complete.cases(jan_interp),], names(mes_interp) lt;- c('Latitude','Longitude','Mes') )}
Кто-нибудь, пожалуйста, может мне помочь?