Home > OS >  IF statements inside function do not recognize conditions
IF statements inside function do not recognize conditions

Time:08-17

I want to adjust my function so that my if and else if statements recognize the name of the dataframe used and execute the correct plotting function. These are some mock data structured the same as mine:

df1<-data.frame(A=c(1,2,2,3,4,5,1,1,2,3),
                B=c(4,4,2,3,4,2,1,5,2,2),
                C=c(3,3,3,3,4,2,5,1,2,3),
                D=c(1,2,5,5,5,4,5,5,2,3),
                E=c(1,4,2,3,4,2,5,1,2,3),
                dummy1=c("yes","yes","no","no","no","no","yes","no","yes","yes"),
                dummy2=c("high","low","low","low","high","high","high","low","low","high"))

df1[colnames(df1)] <- lapply(df1[colnames(df1)], factor)


vals <- colnames(df1)[1:5]
dummies <- colnames(df1)[-(1:5)]
step1 <- lapply(dummies, function(x) df1[, c(vals, x)])
step2 <- lapply(step1, function(x) split(x, x[, 6]))
names(step2) <- dummies
tbls <- unlist(step2, recursive=FALSE)
tbls<-lapply(tbls, function(x) x[(names(x) %in% names(df1[c(1:5)]))])

A<-lapply(tbls,"[", c(1,2))
B<-lapply(tbls,"[", c(3,4))
C<-lapply(tbls,"[", c(3,4))
list<-list(A,B,C)
names(list)<-c("A","B","C")

And this is my function:

plot_1<-function (section, subsample) {
  data<-list[grep(section, names(list))]
  data<-data[[1]]
  name=as.character(names(data))
  
  if(section=="A" && subsample=="None"){plot_likert_general_section(df1[c(1:2)],"A")}
  
  else if (section==name && subsample=="dummy1"){plot_likert(data$dummy1.yes, title=paste("How do the",name,"topics rank?"));plot_likert(data$Ldummy1.no, title = paste("How do the",name,"topics rank?"))}
}

Basically what I want it to do is plot a certain graph by specifying section and subsample I'm interested in if, for example, I want to plot section C and subsample dummy.1, I just write:

plot_1(section="C", subsample="dummy1)

I want to avoid writing this:

else if (section=="A" && subsample=="dummy1"){plot_likert(data$dummy1.yes, title=paste("How do the A topics rank?"));plot_likert(data$Ldummy1.no, title = paste("How do the A topics rank?"))}
  else if (section=="B" && subsample=="dummy1"){plot_likert(data$dummy1.yes, title=paste("How do the B topics rank?"));plot_likert(data$Ldummy1.no, title = paste("How do the B topics rank?"))}
  else if (section=="C" && subsample=="dummy1"){plot_likert(data$dummy1.yes, title=paste("How do the c topics rank?"));plot_likert(data$Ldummy1.no, title = paste("How do the C topics rank?"))}
  else if (section=="C" && subsample=="dummy2")...
  .
  .
  }

So I tried to extract the dataframe used from the list so that it matches the string of the section typed in the function (data<-list[grep(section, names(list))]) and store its name as a character (name=as.character(names(data))), because I thought that in this way the function would have recognized the string "A", "B" or "C" by itself, without the need for me to specify each condition.

However, if I run it, I get this error: Warning message: In section == name && subsample == "dummy1" : 'length(x) = 4 > 1' in coercion to 'logical(1)', that, from what I understand, is due to the presence of a vector in the statement. But I have no idea how to correct for this (I'm still quite new to R).

How can I fix the function so that it does what I want? Thanks in advance!

CodePudding user response:

Well, I can't really test your code without the plot_likert_general_section function or the plot_likert function, but I've done a bit of simplifying and best practices--passing list in as an argument, consistent spaces and assignment operators, etc.--and this is my best guess as to what you want:

plot_1 = function(list, section, subsample) { ## added `list` as an argument
  data = list[[grep(section, names(list))]] # use [[ to extract a single item
  name = as.character(names(data))
  
  if(subsample == "None"){
    plot_likert_general_section(df1[c(1:2)], section)
  } else  {
    yesno = paste(subsample, c("yes", "no"), sep = ".")
    plot_likert(data[[yesno[1]]], title = paste("How do the", name, "topics rank?"))
    plot_likert(data[[yesno[2]]], title = paste("How do the", name, "topics rank?"))
  }
}

plot_1(list, section = "C", subsample = "dummy1) 

I'm not sure if your plot_likert functions use base or grid graphics--but either way you'll need to handle the multiple plots. With base, probably use mfrow() to display both of them, if grid I'd suggest putting them in a list to return them both, and then maybe using gridExtra::grid.arrange() (or similar) to plot both of them.

CodePudding user response:

You're right that the error is due to passing a vector where a single value is expected. Try inserting print statements before the equality test to diagnose why this is.

Also, be careful with choosing variable names like name which are baseR functions (e.g. ?name). I'd also recommend following the tidyverse style guide here: https://style.tidyverse.org/.

  • Related