0

I am trying to separate a data frame, based on month and year, into several smaller data frames. So I used a loop to extract data frame which fulfills the month and year conditions (see code below).

However, in some situations, data from certain months are not available in a specific year, and by using the code below, it creates empty data frames. Is there a way to avoid it?

Weather.df = read.table(file = "D:/Program Files/R Projects/Weather_Pattern/Weather.txt", header = TRUE, sep = ",")

for (YEAR in min(Weather.df$year):max(Weather.df$year)){
  for (MONTH in month.abb){
      temp.df = subset(Weather.df, month == MONHT & year == YEAR)
      assign(paste(YEAR,MONTH,"luna",sep="."), temp.df)
  }
}
Scarabee
  • 5,437
  • 5
  • 29
  • 55
Melon
  • 5
  • 1
  • Could you provide a reproducible example?http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – lizzie Feb 11 '17 at 06:06

1 Answers1

0

You could test if the data frame has any rows:

if(nrow(temp.df) > 0) {
  # do assignment
}
Neal Fultz
  • 9,282
  • 1
  • 39
  • 60