Does something like this do what you want? It returns a list of tables md <- data.frame((matrix(sample(1:5, 100, replace = TRUE),nrow= 10))) str(md) apply(md, 2, table)
John Kane Kingston ON Canada > -----Original Message----- > From: p.mulong...@namibia.pharmaccess.org > Sent: Fri, 11 Jan 2013 11:17:42 +0000 > To: r-help@r-project.org > Subject: [R] Using table to get frequencies of several factors at once > > Hi, I have a dataframe with n columns, but I am only looking at five of > them. And lots of rows, over 700. > So I would like to find frequencies for each of the numeric columns > (variables) using the table function. However, is there a fast way to > produce a frequency table where the 5 rows represent the 5 numeric > variables and the columns refer to the values (levels) of the respective > numeric variables, which in this case are 0 and 1. > The only way I have figured it out is via a for loop: > m<-seq(218,222,1) #these are columns of the variables in the larger > dataframe > tm<-m[1:5] #I need this for the for loop > l.tm<-length(tm) > B<-matrix(nrow=l.tm,ncol=2) #the matrix to hold the freqs > for (p in 1:l.tm) { > var.num<-m[p] > B[p,]<-table(DATA[,var.num]) > } > >> B > [,1] [,2] > [1,] 697 9 > [2,] 512 194 > [3,] 604 102 > [4,] 700 6 > [5,] 706 706 > So the rows represent my five variables (columns) that occupy columns 218 > through 222 in the DATA dataframe. > So the second column represents my frequencies of the value 1, which is > what I am interested in. The last row has a double entry, because there > was only one value, 0, with a freq of 706 and so R duplicated in the two > columns, but that's ok, I can just ignore it. > > So is there are better way to do this? Is there a way to use the so > called tapply function? I struggle to understand the help doc for > this.function. > > > Pancho Mulongeni > Research Assistant > PharmAccess Foundation > 1 Fouché Street > Windhoek West > Windhoek > Namibia > > Tel: +264 61 419 000 > Fax: +264 61 419 001/2 > Mob: +264 81 4456 286 > > ______________________________________________ > R-help@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. ____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop! ______________________________________________ R-help@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.