Leon-Alph
Leon-Alph

Reputation: 87

extract data.frame() from group_by()

I'd like extract a data.frame produce by group_by() function of the dplyr package on R.

My original data looks like this:

   nb   site  wincod    od     cg
1 7073  ama      1 0.005351899  0
2 7129  ama      1 0.080931646  0 
3 7130  ama      1 0.446781435  0
4 7131  ama      1 0.451162869  0
5 7132  ama      1 0.992270042  0
... ... ...     ...  ...       ...

What I'd like is to have every group by "site" and "wincod". I tried this:

as.data.frame(group_by(tab_OD,site,wincod))

But it doesn't return the data frame I want. I could do it by:

groupe1 <- filter(tab_OD,site=="ama",wincod==1)
groupe2 <- filter(tab_OD,site=="ama",wincod==2)
...
groupeN <- filter(tab_OD,site=="che",wincod==1)

thanks for your help.

Upvotes: 0

Views: 1569

Answers (1)

David Arenburg
David Arenburg

Reputation: 92292

You could alternatively use split in order to create a list of data.frames based on condition, such as

with(tab_OD, split(od, paste(site, wincod, sep = "_")))

Or (per Hadleys comment)

with(tab_OD, split(od, list(site, wincod)))

Upvotes: 1

Related Questions