Hello everybody, I have problem with a lapply command, which rather proves that I don't fully understand it. I want to extract from a list that consists of dataframes, the length of the first sequences from a given variable (its part of a simulation exercises). Below is code which does the job, but I think it should be possible to make it more compact. ### Example Data dat <-list() dat[[1]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) dat[[2]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) ####Code x<-sapply(dat,"[",3) #Extracting the vector y<-lapply(x,rle) #Counting the sequences which is returned as a list z<-sapply(y,"[", 1) #extracting the first element of the list of sequence counts final<-sapply(z,"[",1) #extracting the first number, which gives the length of the first sequence, which I want final ############### Thanks for your help, Stefan
### Example Data dat <-list() set.seed(1) dat[[1]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) dat[[2]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) ####Code x<-sapply(dat,"[",3) #Extracting the vector y<-lapply(x,rle) #Counting the sequences which is returned as a list z<-sapply(y,"[", 1) #extracting the first element of the list of sequence counts final<-sapply(z,"[",1) #extracting the first number, which gives the length of the first sequence, which I want final ############### dat # or you can try this x <- sapply(dat, "[", 3) # extract the vector y <- lapply(x, rle) sapply(y, function(.row) c(length=.row$lengths[1], value=.row$values[1])) On Sat, Aug 2, 2008 at 5:07 AM, stefan.duke at gmail.com <stefan.duke at gmail.com> wrote:> Hello everybody, > I have problem with a lapply command, which rather proves that I don't > fully understand it. > > I want to extract from a list that consists of dataframes, the length > of the first sequences from a given variable (its part of a simulation > exercises). > > Below is code which does the job, but I think it should be possible to > make it more compact. > > ### Example Data > dat <-list() > dat[[1]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) > dat[[2]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) > ####Code > x<-sapply(dat,"[",3) #Extracting the vector > y<-lapply(x,rle) #Counting the sequences which is returned as a list > z<-sapply(y,"[", 1) #extracting the first element of the list of > sequence counts > final<-sapply(z,"[",1) #extracting the first number, which gives the > length of the first sequence, which I want > final > ############### > > > > Thanks for your help, > Stefan > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
It seems to me that the reshape package should do the job, library(reshape) # note that you need to fix the method to melt lists in the current version ( passing ... to the method for data.frames, as discussed here last week) cast(data=melt(dat), fun.aggregate = function(.x) rle(.x)$length[1]) this works for me, but I have to confess a very limited understanding of reshape. Hope this helps, baptiste On 2 Aug 2008, at 10:07, stefan.duke at gmail.com wrote:> Hello everybody, > I have problem with a lapply command, which rather proves that I don't > fully understand it. > > I want to extract from a list that consists of dataframes, the length > of the first sequences from a given variable (its part of a simulation > exercises). > > Below is code which does the job, but I think it should be possible to > make it more compact. > > ### Example Data > dat <-list() > dat[[1]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) > dat[[2]] <- data.frame(matrix( rbinom(40, 1, .8),nrow=5)) > ####Code > x<-sapply(dat,"[",3) #Extracting the vector > y<-lapply(x,rle) #Counting the sequences which is returned > as a list > z<-sapply(y,"[", 1) #extracting the first element of the list of > sequence counts > final<-sapply(z,"[",1) #extracting the first number, which gives the > length of the first sequence, which I want > final > ############### > > > > Thanks for your help, > Stefan > > ______________________________________________ > 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag