-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_spread.R
More file actions
40 lines (29 loc) · 1.63 KB
/
Copy pathfunction_spread.R
File metadata and controls
40 lines (29 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function_spread<- function(TAB){
multi_join <- function(list_of_loaded_data, join_func, ...){
require("dplyr")
output <- Reduce(function(x, y) {join_func(x, y, ...)}, list_of_loaded_data)
return(output)
}
X <- split(TAB, paste(TAB$clim_var)) # create a list of dataframe by climatic variable
for (i in 1: length(X)) { # The folowing code use data.table (faster than dplyr)
if(!is.na(match("month", names(TAB)))){
A<- data.table::melt(data.table(X[[i]]), # Change format of the data
id.vars = c("departement", "clim_var", "year_harvest", "method", "month"),
measure.vars = c("Var_mean_gs",".fitted", ".resid","y58", "y65", "y70" , "y75" , "y78" ,"y80" ,"y82"))
A<-A[, variable := paste0(variable, "_",month)]
A<-A[, !"month"]
} else{
A<- data.table::melt(data.table(X[[i]]), # Change format of the data
id.vars = c("departement", "clim_var", "year_harvest", "method"),
measure.vars = c("Var_mean_gs",".fitted", ".resid","y58", "y65", "y70" , "y75" , "y78" ,"y80" ,"y82"))
}
A<-dcast(A, departement+clim_var+year_harvest+method ~ variable, value.var = "value")
print(paste(A$sp[1], A$clim_var[1]))
NAMES<- c(names(A)[1:4], # Change the column's name of each dataframe
paste(unique(A$clim_var), names(A)[5:length(names(A))],sep="_"))
names(A)<- NAMES
X[[i]] <- A %>% select(-clim_var) #A[, !"clim_var"]
}
TAB<-multi_join(X,full_join)
return(TAB)
}