Dear List, I have a data frame called trait with roughly 800 species in, each species have 15 columns of information: Species 1 2 3 etc.. a t y h b f j u c r y u etc.. I then have another data frame called com with the composition of species in each region, there are 506 different communities: community species NA1102 a NA1102 c NA0402 b NA0402 c AT1302 a AT1302 b etc.. What i want to do is extract the information held in the first data frame for each community and save this as a new data frame. Resulting in : - community_NA1102 a t y h c r y u community_NA0402 b f j u c r y u Thanks in advance for any suggestions / code.
Extract data
4 messages · David Winsemius, Chris Mcowen, jim holtman
On Jan 6, 2011, at 6:36 AM, Chris Mcowen wrote:
Dear List, I have a data frame called trait with roughly 800 species in, each species have 15 columns of information: Species 1 2 3 etc.. a t y h b f j u c r y u etc.. I then have another data frame called com with the composition of species in each region, there are 506 different communities: community species NA1102 a NA1102 c NA0402 b NA0402 c AT1302 a AT1302 b etc.. What i want to do is extract the information held in the first data frame for each community and save this as a new data frame.
> tapply(comm.info$species, comm.info$community, c)
$AT1302
[1] 1 2
$NA0402
[1] 2 3
$NA1102
[1] 1 3
> lapply( tapply(comm.info$species, comm.info$community, c),
function(x){ sp.info[x, ]} )
$AT1302
Species X1 X2 X3
1 a t y h
2 b f j u
$NA0402
Species X1 X2 X3
2 b f j u
3 c r y u
$NA1102
Species X1 X2 X3
1 a t y h
3 c r y u
Might have looked more compact if I had assigned the output of tapply
to an intermediate list:
comm.sp <- tapply(comm.info$species, comm.info$community, c)
lapply( comm.sp , function(x){ sp.info[x, ]} )
Resulting in : - community_NA1102 a t y h c r y u community_NA0402 b f j u c r y u Thanks in advance for any suggestions / code.
David Winsemius, MD West Hartford, CT
Dear David, Thats great, thanks very much for the help, much appreciated.
On 6 Jan 2011, at 15:53, David Winsemius wrote:
On Jan 6, 2011, at 6:36 AM, Chris Mcowen wrote:
Dear List, I have a data frame called trait with roughly 800 species in, each species have 15 columns of information: Species 1 2 3 etc.. a t y h b f j u c r y u etc.. I then have another data frame called com with the composition of species in each region, there are 506 different communities: community species NA1102 a NA1102 c NA0402 b NA0402 c AT1302 a AT1302 b etc.. What i want to do is extract the information held in the first data frame for each community and save this as a new data frame.
tapply(comm.info$species, comm.info$community, c)
$AT1302 [1] 1 2 $NA0402 [1] 2 3 $NA1102 [1] 1 3
lapply( tapply(comm.info$species, comm.info$community, c), function(x){ sp.info[x, ]} )
$AT1302
Species X1 X2 X3
1 a t y h
2 b f j u
$NA0402
Species X1 X2 X3
2 b f j u
3 c r y u
$NA1102
Species X1 X2 X3
1 a t y h
3 c r y u
Might have looked more compact if I had assigned the output of tapply to an intermediate list:
comm.sp <- tapply(comm.info$species, comm.info$community, c)
lapply( comm.sp , function(x){ sp.info[x, ]} )
Resulting in : - community_NA1102 a t y h c r y u community_NA0402 b f j u c r y u Thanks in advance for any suggestions / code.
David Winsemius, MD West Hartford, CT
'merge' comes in handy:
spec <- read.table(textConnection("Species 1 2 3
+ a t y h + b f j u + c r y u"), header=TRUE)
comm <- read.table(textConnection("community species
+ NA1102 a + NA1102 c + NA0402 b + NA0402 c + AT1302 a + AT1302 b"), header = TRUE)
closeAllConnections() # use merge x <- merge(spec, comm, by.x="Species", by.y='species') x
Species X1 X2 X3 community 1 a t y h NA1102 2 a t y h AT1302 3 b f j u NA0402 4 b f j u AT1302 5 c r y u NA1102 6 c r y u NA0402
split(x, x$community)
$AT1302 Species X1 X2 X3 community 2 a t y h AT1302 4 b f j u AT1302 $NA0402 Species X1 X2 X3 community 3 b f j u NA0402 6 c r y u NA0402 $NA1102 Species X1 X2 X3 community 1 a t y h NA1102 5 c r y u NA1102
On Thu, Jan 6, 2011 at 6:36 AM, Chris Mcowen <cm744 at st-andrews.ac.uk> wrote:
Dear List, I have a data frame called trait with roughly 800 species in, each species have 15 columns of information: Species ? ? ? ? 1 ? ? ? 2 ? ? ? 3 ? ? ? etc.. a ? ? ? ? ? ? ? ? ? ? ? t ? ? ? y ? ? ? h b ? ? ? ? ? ? ? ? ? ? ? f ? ? ? j ? ? ? u c ? ? ? ? ? ? ? ? ? ? ? r ? ? ? y ? ? ? u etc.. I then have another data frame called com with the composition of species in each region, there are 506 different communities: community ? ? ? species NA1102 ? ? ? ? ?a NA1102 ? ? ? ? ?c NA0402 ? ? ? ? ?b NA0402 ? ? ? ? ?c AT1302 ? ? ? ? ?a AT1302 ? ? ? ? ?b etc.. What i want to do is extract the information held in the first data frame for each community and save this as a new data frame. Resulting in : - community_NA1102 a ? ? ? ? ? ? ? ? ? ? ? t ? ? ? y ? ? ? h c ? ? ? ? ? ? ? ? ? ? ? r ? ? ? y ? ? ? u community_NA0402 b ? ? ? ? ? ? ? ? ? ? ? f ? ? ? j ? ? ? u c ? ? ? ? ? ? ? ? ? ? ? r ? ? ? y ? ? ? u Thanks in advance for any suggestions / code.
______________________________________________ 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 Data Munger Guru What is the problem that you are trying to solve?