You should really be asking Coursera for help with its course.
However you can figure this out by breaking down your commands
into small steps and seeing what each step gives. This is an advantage
of an interactive system like R. E.g., suppose you start with
> s1 <- data.frame(state=c("Rhode Island","Rhode
Island","Montana"),
x=c(1,2,3))
> s2 <- split(s1, s1$State)
Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
group length is 0 but data length > 0
Do not proceed to compute something that depends on s2 if there
was an error computing s2, but look at the arguments you gave to
split when trying to compute s2:
> s1
state x
1 Rhode Island 1
2 Rhode Island 2
3 Montana 3
> s1$State
NULL
> length(s1$State)
[1] 0
That length 0 object, s1$State, that you gave as the group argument
to split, would cause that error message. Fix the s1$State and you
can procede.
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Oct 2, 2015 at 4:09 PM, Greg Damico <damicogreg78 at gmail.com>
wrote:> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.