[R] "split" and loop functions

Greg Damico damicogreg78 at gmail.com
Sat Oct 3 01:09:26 CEST 2015


Hello,

I wonder if you might be able to help me.  I'm enrolled in an R programming
course through Coursera.  I've done well so far--though it's been
challenging!--but I'm having trouble understanding exactly how "split" and
the loop functions (like "lapply") work.

I keep getting an error that says:  "group length is 0 but data length >0",
and I'm not sure what that means.  "Group" refers, I suppose, to the
factor-individuated bits I create through "split", but then I don't see why
that should be 0....

I'm trying to split a data frame into pieces and then isolate the nth
(user-inputted) row of each bit.  So I've been trying code like:

s2<-split(s1, s1$State, drop = TRUE)
ans<-as.data.frame(lapply(s2, function(elt) elt[as.numeric(num),c(1,2)])),

where s1 is the pre-arranged data frame (and the "State" column is
inherited from the unarranged data frame).  Maybe there's something wrong
(or illicit) in my anonymous function?

I'd appreciate any help you can give me.

Greg Damico

	[[alternative HTML version deleted]]



More information about the R-help mailing list