im trying to subset a data frame in a for loop to create a smaller data.frame. This is my data.frame
day rain in mm temperature in °C season
1 201 20 summer
2 156 18 summer
3 56 -4 winter
4 98 15 spring
I want to extract a data.frame for each season (with all columns). Here is my code:
for (season in seasons){
a<- weather[which(weather$season %in% season[1]) , ,drop=TRUE]
...
}
Unfortunately, the sub-setting doesn' t work. When i use
a<- weather[which(weather$season %in% "summer") , ,drop=TRUE] it works perfectly. Also this does not work properly:
season <- "summer"
a<- weather[which(weather$season %in% season[1]) , ,drop=TRUE]
Does anyone see the problem with my code? Thank you.
split(weather, weather$season). It is better to keep all the data sets in a single list instead of spreading them all over the global environment.