Why are the if and else statements in my function to create bargraphs not working?

Hi RStudioCommunity,

Beginner to R here. I am currently working on a project to create two bar graphs using one R script. Each bar graph however, uses a different data source.

I assign a dataframe named "PerData" to one of the two data spreadsheets: PerData <- read_excel("ab.xlsx").

It is intended for the user to type whatever spreadsheet they want the graph to be produced from in the argument of read_excel: PerData <- read_excel(" ").

I have designed the function to recognise which appropriate code to run, depending on the spreadsheet entered. The distinguishing factor is one of the spreadsheets has "NA" values.

I used a function (bargraph_creator) to implement this. The process behind the function works as such:

  1. Assign dataframe "PerData" to whichever of the two spreadsheets the user desires to use to produce the bar graph.
  2. Feed that dataframe into the function bargraph_creator
  3. IF that spreadsheet has NA values, it immediately recognises that spreadsheet corresponds to the code under the if statement for producing the bar graph. It ignores the code under the 'else' statement.
  4. However, if the spreadsheet has no NA values (else statement), it uses the code under the 'else' statement. The code under the if statement is ignored.

However, I am noticing the if and else statements are not working as desired. It seems to always be skipping the 'if' statement and skipping straight to the 'else' statement. I am not sure what is the problem.

Any help would be appreciated. I've attached the code below, as well as the two datasets. I am also a beginner to R and asking questions. If this code does not meet the criteria of a reprex, please let me know and I will try to fix it.

Datasets: Dropbox - Data - Simplify your life


####Load Packages####.
library(reshape2) #melt
library(tidyr)
library(readxl)
library(dplyr) #mutate
library(ggplot2)
library(labeling)
library(zoo) #yearmon
library(utf8) #functions

##########Replicate Bar Graph#################

PerData <- read_excel("ab.xlsx") 

bargraph_creator <- function(PerData) {
  a <- is.na(PerData)
  if (a[1,1] == TRUE) {
    PerData <- na.omit(PerData) #na.omit removes all rows with NAs
    
    names(PerData)[names(PerData) == "...1"] <- "Time"
    
    PerData <- mutate(PerData, Date = as.yearmon(Time, format = '%b-%y')) #Modify the time column into an R date class
    Per.m <- melt(PerData,id = c("Time","Date")) #Modify the structure of the dataframe into an R compatible way
    
    #produce bar graph
    ggplot(Per.m, aes(x=Date, y=value)) + geom_bar(aes(fill = variable), position = "dodge", width=0.15, stat="identity")
    
  } else{
    PerData <- read_excel("abc.xlsx")
 
    names(PerData)[names(PerData) == "...1"] <- "Time"
    
    PerData <- PerData[-c(1,2,3,4,5),] #remove all rows up to MAR 18
    PerData <- mutate(PerData, Date = as.yearmon(Time, format = '%b-%y')) #Modify the time column into an R date class
    Pera.m <- melt(PerData,id = c("Time","Date")) #Modify the structure of the dataframe into an R compatible way
    
    #produce bar graph
 ggplot(Pera.m, aes(x=Date, y=value)) + geom_bar(aes(fill = variable),
                                                               position = "dodge", width=0.15, stat="identity") } }

bargraph_creator(PerData) #Test out the function




You arent checking if a data.frame has NAs in it. You are checking if your variable that you intended to be a dataframe is not a dataframe but rather is NA

Hi, thanks for responding! I'm a bit confused though as I've checked through my code and can't find it; where is the part that I messed up and I am instead checking if my whole dataframe is NA?

It happens here in this part

Thanks! That wasn't quite the issue itself but there was something else I found wrong in that line of code so it definitely helped.

I'll post the solution below for anyone interested in the future:

So it turns out using

if (a[1,1] == TRUE)

was the source of the issue. This wasn't because of nigrahamuk's suggestion that "You arent checking if a data.frame has NAs in it. You are checking if your variable that you intended to be a dataframe is not a dataframe but rather is NA" but rather it was checking a single cell in the dataframe (1st row in the 1st column) that did not align with my code.

That's because both my data sources all had the first row in the first cell to be a non NA value, so it would naturally always just skip to the 'else' statement. A solution I found was to implement this instead:

a <- is.na(PerData)
if (any(is.na(PerData)) == TRUE)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.