[R] How to let R repeat computations over a number of variables
Uli Kleinwechter
ulikleinwechter at yahoo.com.mx
Fri Mar 14 21:13:09 CET 2008
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()
******
More information about the R-help
mailing list