I have a problem with organizing some nested loops I am running.
I have a list with 10 dfs (one for each crop), each df has 7 columns, the first three columns contain factors, the last 4 contain numbers (with some NAs).
What I want to do is to attach another column to each df with numbers. The values in this column are dependent on some values in a df called "lower".
I made an ifelse function that takes as input a number from two different columns.
low <- function(x,y) {
ifelse(x < lower[i,j], (y * 1.01^12), y)
}
lower is a dataframe with 6 columns (biomes) and 10 rows (crops). x and y are columns in each dataframe.
Now I want to apply this function to each dataframe (i.e. crop), depending on a factor in another column (biome, 6 factors in this column).
Applying the function is easy:
mapply(low, column1, column2) # applies function low and gives result
The indices I can use are i = crop and j = biome and these equates to a specific numbers in the df lower.
for (i in 1:10) {
for (j in levels(biome)){
New_list[[i]] <- mapply(low,
trial[[i]][trial[[i]]$Biome %in% j,][,5],
trial[[i]][trial[[i]]$Biome %in% j,][,7]) # applies function low and gives result, trial is the list with the dataframes
}}
But this only gives me results for the last j and not the other 5.
So how do I get 10 vectors, one for each crop that I can attach to my list with 10 df.
Aucun commentaire:
Enregistrer un commentaire