[R-sig-eco] Converting species counts into species list

Tom_Philippi at nps.gov Tom_Philippi at nps.gov
Mon Dec 6 17:48:46 CET 2010


expand.table() in package vcdExtra performs the same expansion as Peter's
rep.data.frame() function in mefa.  expand.table() works on table objects
as well as data frames.  However, If you're dealing with species by site
data, I recommend using the tools in mefa to leverage the additional object
classes.

Tom

-------------------------------------------
Tom Philippi, Ph.D.
Quantitative Ecologist
Inventory and Monitoring Program
National Park Service
1201 Oakridge Drive, Suite 150
Ft. Collins, CO   80525-5589
Tom_philippi at NPS.gov    (970) 225-3586
Fax (970) 225 3597
http://science.nature.nps.gov/im/monitor
-------------------------------------------



                                                                           
             Peter Solymos                                                 
             <solymos at ualberta                                             
             .ca>                                                       To 
             Sent by:                  zhangjl <zhangjl at ibcas.ac.cn>       
             r-sig-ecology-bou                                          cc 
             nces at r-project.or         r-sig-ecology                       
             g                         <r-sig-ecology at r-project.org>,      
                                       Ophelia Wang                        
                                       <opheliawang at mail.utexas.edu>       
             12/06/2010 07:30                                      Subject 
             AM MST                    Re: [R-sig-eco] Converting species  
                                       counts into species list            
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Ophelia and Others,

A more general solution is:

## define a function (that is part of the mefa package)
`rep.data.frame` <- function(x, ...)
    as.data.frame(lapply(x, rep, ...))
## example from ?mefa:::rep.data.frame
x <- data.frame(sample = LETTERS[c(1,1,2,2,3)],
    species = letters[c(5,5,5,6,7)],
    count = c(1,2,10,3,4), segment = letters[c(8,9,8,9,8)])
x
rep(x[,c(1,2,4)], times = x[,3])
rep(x[,c(1,2,4)], each = 2)

Cheers,

Peter


On Mon, Dec 6, 2010 at 5:07 AM, zhangjl <zhangjl at ibcas.ac.cn> wrote:
> Dear Ophelia,
>     If your dataframe is already in memory, simply write
>    as.character(rep(dat[,1],dat[,2]) ,
> to finish the task.
>
> Aegiinte        3
> Agonperu      3
> Aiouimpr       2
> Albiniop        2
> Albisubd       1
> ..................
> else, you may try the following code, if your data is saved on the disk.
>
> dat <- read.csv("dat2.csv")
> writeLines(as.character(rep(dat[,1],dat[,2])), "Lines.csv")
>
> Hope these commands will help.
>
> best,
> Jinlong
>
>
> 2010-12-06
>
>
>
> Jinlong Zhang
> Ph.D.Candidate
> State Key Laboratory for Vegetation and Environmental Change,
> Institute of Botany, The Chinese Academy of Sciences
> Nanxincun 20,Xiangshan, Beijing 100093
> E-mails:
> zhangjl at ibcas.ac.cn
> jinlongzhang01 at gmail.com
>
>
>
>
> 发件人: Ophelia Wang
> 发送时间: 2010-12-06  16:51:21
> 收件人: r-sig-ecology
> 抄送:
> 主题: [R-sig-eco] Converting species counts into species list
>
> Hello,
> I have a list of species and abundance that looks like this:
> Aegiinte        3
> Agonperu      3
> Aiouimpr       2
> Albiniop        2
> Albisubd       1
> ..................
> And I need to convert these two columns into a column in which each
> cell represents an individual of a articular species:
> Aegiinte
> Aegiinte
> Aegiinte
> Agonperu
> Agonperu
> Agonperu
> Aiouimpr
> Aiouimpr
> Albiniop
> Albiniop
> Albisubd
> .........
> Can someone help me to develop a script to do the conversion?
> Thanks a lot!
> Ophelia
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
>

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology


More information about the R-sig-ecology mailing list