Skip to content
Prev 277111 / 398506 Next

Help

Hi Francesca,

Try something like this:

x <- c(1, 3, 7)
dati <- lapply(1:4, function(i) {datiP[datiP$city == i, x]})

dati[[1]] # datiP1
dati[[2]] # datiP2
dati[[3]] # datiP3
dati[[4]] # datiP4

if the *only* groups are 1, 2, 3, 4 (i.e., 1:4 is exhaustive), this
can be simplified:


dati <- by(datiP, datiP$city, `[`, x)

For documentation, see:

?lapply
?by
?"[" # to see how I use the extraction operator as a function

Hope this helps,

Josh

On Fri, Nov 11, 2011 at 12:24 PM, Francesca
<francesca.pancotto at gmail.com> wrote: