r/R_Programming • • Jun 19 '17

Efficient/shorter R code

Hi all, I am grabbing and formatting a bunch of similar api calls in JSON and doing some formatting and then dumping into CSV. I am a newbie to coding and R and I see now that my code is 70% repeating of the same things... Which I guess is breaking some kind of principle of good coding, right?

Example of code that is repeated about 8-16 times:

Analytics_export<-jsonlite::fromJSON(r,flatten=TRUE)$rows #Gets the data from the analytics
Analytics_headers<-jsonlite::fromJSON(r,flatten=TRUE)$headers #Gets the column names.
colnames(Analytics_export) <- c(Analytics_headers[,"column"]) #Writes column names from json
Analytics_export<- replace(Analytics_export, Analytics_export == 'EDUCATION', 'Education')
Analytics_export<- replace(Analytics_export, Analytics_export == 'FOOD SECURITY', 'Food Security')
Analytics_export<- replace(Analytics_export, Analytics_export == 'Democratic Republic of Congo', 'DRC')
Analytics_export <- replace(Analytics_export, Analytics_export == 'SHELTER', 'Shelter')
Analytics_export <- cbind(Analytics_export, "Project" = '')
Analytics_export <- cbind(Analytics_export, "Year" = right(Analytics_export[,2],4))
Analytics_export[,2] <- gsub('Oct to Dec .*', 'Q4',Analytics_export[,2])
Analytics_export[,2] <- gsub('Jul to Sep .*', 'Q3',Analytics_export[,2])
Analytics_export[,2] <- gsub('Apr to Jun .*', 'Q2',Analytics_export[,2])
Analytics_export[,2] <- gsub('Jan to Mar .*', 'Q1',Analytics_export[,2])

I have two questions on this:

  1. Is there a simple way of just adding all this to a function or just call it "Analytics formating" and then call that for each of the formating times?
  2. Do you have any simple tips for how to make this better or more condensed?

Thanks!

3 Upvotes

2 comments sorted by

View all comments

Show parent comment

3

u/runopinionated Jun 19 '17

That is great! I wasn't aware of either stringr or pipes. Thanks a bunch! :-)