dimanche 8 décembre 2019

how to put a vector as a condition of a function?

I made this function to plot some graphics, and it works well except for the if condition of the third line

my_dsaurus_function <- function(datasets){

  df1 <- filter(datasaurus_dozen, dataset == datasets)

  if(datasets == c("away", "h_lines", "bullseye")){

    ggplot(df1) +
      geom_point(aes(x, y)) +
      facet_grid(. ~ dataset)

  }else{

    ggplot(df1) +
      geom_point(aes(x, y, colour = dataset, fill = dataset))

}}

these errors always comes up:

Warning messages:
1: In dataset == datasets :
  longitud de objeto mayor no es múltiplo de la longitud de uno menor
2: In datasets == c("away", "h_lines", "bullseye") :
  longitud de objeto mayor no es múltiplo de la longitud de uno menor
3: In if (datasets == c("away", "h_lines", "bullseye")) { :
  la condición tiene longitud > 1 y sólo el primer elemento será usado

it says: the condition has length > 1 and only the first element will be used the length of the bigger object isn´t a multiple for the length of a smaller one

how do I eliminate these errors?

Aucun commentaire:

Enregistrer un commentaire