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:
- Assign dataframe "PerData" to whichever of the two spreadsheets the user desires to use to produce the bar graph.
- Feed that dataframe into the function bargraph_creator
- 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.
- 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