[R] How to let R repeat computations over a number of variables

From: Uli Kleinwechter <ulikleinwechter_at_yahoo.com.mx>
Date: Fri, 14 Mar 2008 21:13:09 +0100


Hello,

I have written a small script to read a dataset, compute some basic descriptives and write them to a file (see below). The variable
"maizeseedcash" of which the statistics are calculated is contained in
the data frame agr_inputs. My question is whether there is a way to make R compute the statistics not only for maizeseedcash but also for other variables in the dataset. I thought about a thing like a loop which repeats the computations according to a set of variables which I would like to be able to specify before. Is something like that possible and if so, how would it look like?

All hints are appreciated.

Best regards,

Uli



sink("agr_inputs.txt", append=FALSE, type="output")

agr_inputs<-read.csv2("agric_inputs.csv")

attach(agr_inputs)

min<-min(maizeseedcash)

q25<-quantile(maizeseedcash, probs=.25)

median<-quantile(maizeseedcash, probs=.50)

mean<-mean(maizeseedcash)

q75<-quantile(maizeseedcash, probs=.75)

max<-max(maizeseedcash)

var<-var(maizeseedcash)              

sd<-sd(maizeseedcash)

varcoeff<-sd/mean*100

Measure<-c("Min","25%", "Median", "Mean", "75%", "Max", "Var", "SD",
"VarCoeff")

maizeseedcas<-c( min, q25, median, mean, q75, max, var, sd, varcoeff)

solution<-data.frame(Measure, maizeseedcas)

print (solution)

detach(agr_inputs)

sink()




R-help_at_r-project.org mailing list
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. Received on Fri 14 Mar 2008 - 21:56:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 15 Mar 2008 - 01:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive