[R] split / lapply over multiple columns

Ralf B ralf.bierig at gmail.com
Wed Aug 4 04:48:14 CEST 2010


Hi all,

I have a data frame with column over which I would like to run
repeated functions for data analysis. Currently I am only running
recursively over two columns where I column 1 has two states over
which I split and column two has 3 states. The function therefore runs
2 x 3 = 6 times as shown when running the following code:

mydata <- data.frame(userid = c(5, 6, 5, 6, 5, 6), taskid = c(1, 1, 2, 2, 3, 3),
      stuff = 11:16)
mydata
mydata <- mydata[with(mydata, order(userid, taskid)), ]
mydata

lapply(split(mydata, mydata[,1]), function(x){
	lapply(split(x, x[,2]), function(y){
		print(paste("result:",y))
	})
})

This traverses the tree like this:

5,1
5,2
5,3
6,1
6,2
6,3

Is there an easier way of doing that? I would like to provide the two
columns (index 1 and index 2) directly and have the ?lapply function
perform its lambda function directly on each memebr of the tree
automatically? How can I do that?

Best,
Ralf



More information about the R-help mailing list