Search code examples
rfunctionsyntax-erroruser-defined

Errors creating user defined function specific to my code


The below chunk of code is used for an analysis of water chemical composition for all the districts in a particular state of india through 2009:2012 train is the parent data Quality.Parameter gives info on chemicals

state_1 <- subset(train,train$State.Name=="ANDHRA PRADESH")
ANDHRA <-   as.data.frame(table(state_1$District.Name,state_1$Quality.Parameter,state_1$Year),stringsAsFactors = FALSE)
str(ANDHRA)
names(ANDHRA) <- c("District.Name","CHEMICAL","YEAR","Freq")
an <- ggplot(ANDHRA,aes(ANDHRA$CHEMICAL,ANDHRA$Freq,fill=ANDHRA$District.Name))
an+geom_bar(stat="identity",position = "dodge")+facet_grid(.~ANDHRA$YEAR)+
labs(title="TREND of Chemical Compostion in Andhra Pradesh Villages",x="Chemicals",y="Number Of Cases",fill="Districts in Andhra Pradesh")

i have about 27 Indian states and want to analyze the chemicals in the water above code plot this.

table(train$State.Name)

   ANDHRA PRADESH ARUNACHAL PRADESH             ASSAM             BIHAR       CHATTISGARH      CHHATTISGARH 
         2888               612             79910             92336             25062              8815 
      GUJARAT           HARYANA  HIMACHAL PRADESH JAMMU AND KASHMIR         JHARKHAND         KARNATAKA 
         2092               262                88                67              3913             30824 
       KERALA    MADHYA PRADESH       MAHARASHTRA           MANIPUR         MEGHALAYA          NAGALAND 
         4800             14449             12480                14               427               618 
       ORISSA        PUDUCHERRY            PUNJAB         RAJASTHAN        TAMIL NADU           TRIPURA 
        68620                17              1056            131417              3164             26235 
UTTAR PRADESH       UTTARAKHAND       WEST BENGAL 
         9918                57             30101 

Instead of writing the code again and again for all the different states i created a user defined function

district_analysis<-function(a,b,d) {
b<- subset(train,train$State.Name=="a")
d<- as.data.frame(table(b$District.Name,b$Quality.Parameter,b$Year),stringsAsFactors = FALSE)
names(d) <- c("District.Name","CHEMICAL","YEAR","Freq")
an <- ggplot(d,aes(d$CHEMICAL,d$Freq,fill=d$District.Name))
an+geom_bar(stat="identity",position = "dodge")+facet_grid(.~d$YEAR)+labs(title="TREND of Chemical Compostion in" a "Villages",x="Chemicals",y="Number Of Cases",fill="Districts in" a) 
return(an) }

But while trying to run the function i am getting the error as this

> district_analysis<-function(a,b,d) {
+   b<- subset(train,train$State.Name=="a")
+     d<- as.data.frame(table(b$District.Name,b$Quality.Parameter,b$Year),stringsAsFactors = FALSE)
+     names(d) <- c("District.Name","CHEMICAL","YEAR","Freq")
+     an <- ggplot(d,aes(d$CHEMICAL,d$Freq,fill=d$District.Name))
+     an+geom_bar(stat="identity",position = "dodge")+facet_grid(.~d$YEAR)+labs(title="TREND of Chemical Compostion in" a "Villages",x="Chemicals",y="Number Of Cases",fill="Districts in" a) 
Error: unexpected symbol in:
"  an <- ggplot(d,aes(d$CHEMICAL,d$Freq,fill=d$District.Name))
an+geom_bar(stat="identity",position =   "dodge")+facet_grid(.~d$YEAR)+labs(title="TREND of Chemical Compostion in" a"
>   return(an) }
Error: unexpected '}' in "  return(an) }"

I am not able to figure out the reason please suggest me the code edit.


Solution

  • You forget to paste the strings.

    +labs(title="TREND of Chemical Compostion in" a "Villages"
    

    does not work, that should be:

    +labs(title=paste0("TREND of Chemical Compostion in ", a, " Villages")
    

    to make it a single string. The same holds for the fill parameter, I am actually not even sure if you can use the parameter that way, and it should not be in your labs() function.

    Hope this helps.