[R] 1st script

memilanuk memilanuk at gmail.com
Mon Mar 23 02:21:55 CET 2015

So... wrote my first script, rather than just using the interactive 
console.  I think I got everything working more or less the way I want, 
but I'm sure there's a ton of room for improvement.  Specifically in the 
way of automation - but thats where I kind of ran out of steam.  Any 
suggestions would be much appreciated.


# Import CSV file into a data frame.
case_weights <- read.csv(file = "case_weights.csv")

# For each row, take the number in the Weight column and replicate it
# as many times as there are in each count column.
LC09 <- rep(case_weights$Weight, case_weights$LC09)
LC10 <- rep(case_weights$Weight, case_weights$LC10)
LP14b1 <- rep(case_weights$Weight, case_weights$LP14b1)
LP14b2 <- rep(case_weights$Weight, case_weights$LP14b2)

# Determine the longest vector, to help with the next step.
max.len <- max(length(LC09), length(LC10), length(LP14b1),

# Pad each vector with NA so they are all the same length and will
# go in a data frame.
LC09 <- c(LC09, rep(NA, max.len - length(LC09)))
LC10 <- c(LC10, rep(NA, max.len - length(LC10)))
LP14b1 <- c(LP14b1, rep(NA, max.len - length(LP14b1)))
LP14b2 <- c(LP14b2, rep(NA, max.len - length(LP14b2)))

# Stick everything back into one data frame.
case_dat <- data.frame(LC09)
case_dat$LC10 <- LC10
case_dat$LP14b1 <- LP14b1
case_dat$LP14b2 <- LP14b2

# Stuff said data frame back into a CSV for use elsewhere (plot.ly).
write.csv(case_dat, file = "expanded_case_weights.csv")

# Boxplot it
boxplot(case_dat, varwidth = TRUE, notch = TRUE, horizontal = TRUE,
         main = "Case Weights", xlab = "Weight (grains)",
         ylab = "Batch", las = 1, names = c("LC09", "LC10", "LP14b1",

Shiny!  Let's be bad guys.

Reach me @ memilanuk (at) gmail dot com

More information about the R-help mailing list