jeudi 13 août 2020

R argument of length zero in if statement

I have a df

df <- data.frame(id = c(1, 1, 2, 2, 3, 3, 3), 
                 year = c(1997,1997,1997,1997,1998,1998,1998),
                 usable = c(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
                 main = c(TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE))

When I execute the code

df$id_year <- interaction(df$id, df$year)
for (i in levels(df$id_year)) {
  rows <- df$id_year == i & df$usable
  if (!unique(df$main[rows]) {
    print("yes")
  }
}

the error of "argument of length zero" appears for the line if (!unique(df$main[rows]). Could anyone please explain why and how to fix it? Thank you

Aucun commentaire:

Enregistrer un commentaire