Reputation: 149
best wishes for the new year.
I have a question...I would like to create a loop to generate multiple plots from multiple dataframes for several designated factor names in r.
I have different dataset with the same fish species. There are more than 50 species (plaice, flounder etc.) For each of these species I have different datasets with different data for each set, but the species used are the same. For each of these species I want to create the same ggplots and other files. Now I made the scipt so that when I fill in a species name in:
###########################
USEDSPECIES <- "Plaice"
###########################
I can run the scripts all at once to generate the plots etc. I want. The only down side is that I have to write down each species and run the whole thing like 50 times... Is there a way for me to make a kind of loop?
In the past I created several loop constructions like:
MaxValue <- rep(FALSE, length(data$Column))
tags <- unique(data $FishID)
for(i in 1:length(tags)){
t.ss <- which(data $FishID == tags[i])
MaxValue[t.ss][which(data$Column[t.ss] == max(data $Column[t.ss], na.rm=T))] <- TRUE
}
Only this way only one dataframe is used, in stead of multiple without indicating a factor name (as species) used.
Thank you in advance!
Upvotes: 1
Views: 47
Reputation: 6911
An example of the advantages of combining the scattered dataframes and working on from there:
library(dplyr)
## example data frames for Plaice and Flounder:
df_plaice <- data.frame(FishID = rep('Plaice', 8), Weight = sample(500:800, 8))
df_flounder <- data.frame(FishID = rep('Flounder', 8), Weight = sample(500:800, 8))
## row-bind single data frames:
all_the_fish <-
list(df_plaice, df_flounder) |> Reduce(f = rbind)
> all_the_fish
FishID Weight
1 Plaice 553
2 Plaice 776
## ...
15 Flounder 580
16 Flounder 794
## species-wise aggregates:
all_the_fish |>
group_by(FishID) |>
summarize(MaxWeight = max(Weight, na.rm = TRUE),
AverageWeight = mean(Weight, na.rm = TRUE)
)
# A tibble: 2 x 3
FishID MaxWeight AverageWeight
<chr> <int> <dbl>
1 Flounder 794 674.
2 Plaice 776 620.
plotting:
all_the_fish |>
ggplot() +
geom_boxplot(aes(Weight)) +
coord_flip() +
facet_wrap(~ FishID)
Upvotes: 1