[R] evaluating and walking in names
Thomas W Blackwell
tblackw at umich.edu
Thu Aug 7 00:35:43 CEST 2003
Cezar -
The functions sapply() and lapply() will loop over all
of the columns in a dataframe. A very common syntax is:
column.means <- sapply(my.data, mean)
But for this particular example, you could use the function
colMeans() instead. See the help for all three.
- tom blackwell - u michigan medical school - ann arbor -
> Cezar Augusto de Freitas Anselmo wrote:
> >
> > Hi, all.
> > Suppose I have an object with names (like a data.frame) and I want to walk
> > in a loop with your names. How can I do this? The idea is like this:
> >
> > my.data<-data.frame(matrix(runif(6),ncol=2))
> > names(my.data)
> > [1] "X1" "X2"
> >
> > for(i in names(my.data)){
> > my.variable <- cat(paste("my.data$", i, "\n", sep=""))
> > print(mean(my.variable))
> > }
> >
> > #it doesn't work.
> >
> > Thnaks for all,
> > C.
> > ========================================
> > Cezar Freitas (ICQ 109128967)
> > IMECC - UNICAMP
> > Campinas, SP - Brasil
More information about the R-help
mailing list