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. case_data.r # 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), length(LP14b2)) # 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", "LP14b2")) -- Shiny! Let's be bad guys. Reach me @ memilanuk (at) gmail dot com
Hi use list and numeric indexing. As you did not provide reproducibe example here is possible way. dat <- data.frame(w=abs(rnorm(10)), LC09=round(runif(10)*10), LC10=round(runif(10)*10)) lll <- vector("list", 2) k=0 for(i in 2:3) { k=k+1 lll[[k]] <- rep(dat$w, dat[,i]) } names(lll)<-names(dat[,2:3]) boxplot(lll) Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > memilanuk > Sent: Monday, March 23, 2015 2:22 AM > To: r-help at stat.math.ethz.ch > Subject: [R] 1st script > > 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. > > case_data.r > > # 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), > length(LP14b2)) > > # 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", > "LP14b2")) > > > > -- > Shiny! Let's be bad guys. > > Reach me @ memilanuk (at) gmail dot com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
On 03/22/2015 10:58 PM, PIKAL Petr wrote:> use list and numeric indexing. As you did not provide reproducibe example here is possible way. > > dat <- data.frame(w=abs(rnorm(10)), LC09=round(runif(10)*10), LC10=round(runif(10)*10)) > lll <- vector("list", 2) > k=0 > for(i in 2:3) { > k=k+1 > lll[[k]] <- rep(dat$w, dat[,i]) > } > names(lll)<-names(dat[,2:3]) > boxplot(lll) >I don't suppose you have a commented version of that...? What may be blindingly obvious to you is... not so much for me. Is this what you need for a reproducible example? Sample of case_weights.csv: Weight,LC09,LC10,LP14b1,LP14b2 171.0,0,0,0,0 171.1,0,0,3,0 171.2,0,0,19,0 171.3,0,0,44,0 171.4,0,0,52,0 171.5,0,0,56,1 171.6,0,0,42,1 171.7,0,0,17,5 171.8,0,0,15,29 171.9,0,0,2,46 172.0,0,0,0,37 172.1,0,0,0,39 172.2,0,0,0,33 172.3,0,0,0,48 172.4,0,0,0,26 172.5,0,0,0,48 172.6,0,0,0,31 172.7,0,0,0,47 172.8,0,0,0,17 172.9,0,0,0,19 173.0,0,0,0,7 173.1,0,0,0,10 173.2,0,0,0,12 173.3,0,0,0,17 173.4,0,0,0,10 173.5,0,0,0,6 173.6,0,0,0,6 173.7,0,0,0,2 173.8,0,0,0,1 176.3,3,0,0,0 176.4,1,0,0,0 176.5,0,0,0,0 176.6,4,0,0,0 176.7,2,0,0,0 176.8,6,0,0,0 176.9,4,0,0,0 177.0,9,0,0,0 177.1,16,0,0,0 177.2,24,0,0,0 177.3,27,0,0,0 177.4,43,0,0,0 177.5,40,0,0,0 177.6,56,0,0,0 177.7,41,0,0,0 177.8,58,0,0,0 177.9,55,0,0,0 178.0,48,0,0,0 178.1,37,0,0,0 178.2,23,0,0,0 178.3,30,0,0,0 178.4,14,0,0,0 178.5,16,0,0,0 178.6,9,0,0,0 178.7,11,0,0,0 178.8,9,0,0,0 178.9,7,0,0,0 Thanks, Monte -- Shiny! Let's be bad guys. Reach me @ memilanuk (at) gmail dot com
> # 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)My stock advice is to add code that checks that the stuff you read from the file has the expected format. Do this right after the read.csv. The stopifnot function gives a quick way to code the checks and it helps to write helper functions when checks must be repeated. E.g. allNonNegIntegers <- function(x) is.numeric(x) && all(!is.na(x) & x >0 & x%%1==0) stopifnot( all(c("Weight", "LC09", "LC10") %in% names(case_weights)), is.numeric(case_weights$Weight), allNonNegIntegers(case_weights$LC09), allNonNegIntegers(case_weights$LC10), <... more comma-separated check expressions>) Bill Dunlap TIBCO Software wdunlap tibco.com On Sun, Mar 22, 2015 at 6:21 PM, memilanuk <memilanuk at gmail.com> wrote:> 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. > > case_data.r > > # 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), > length(LP14b2)) > > # 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", > "LP14b2")) > > > > -- > Shiny! Let's be bad guys. > > Reach me @ memilanuk (at) gmail dot com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]