[R] repeating rows in R

arun smartpink111 at yahoo.com
Fri Feb 7 17:54:29 CET 2014


Hi,
Use ?rep()
df1 <- read.table(text="Site      Scientific.name      value       size
'Flat Cay'            'S. iserti'             3         2.5
'Flat Cay'            'S. iserti'             2         8
'Flat Cay'            'S. iserti'             1         25
'Flat Cay'           'S. taeniopterus'     2           15",sep="",header=TRUE,stringsAsFactors=FALSE) 

df2 <- df1[rep(1:nrow(df1),df1$value),-3]
row.names(df2) <- 1:nrow(df2)
A.K.

Hello! 
  

I have a data frame that looks something like this: 

      Site      Scientific.name      value       size 
Flat Cay            S. iserti             3         2.5 
Flat Cay            S. iserti             2         8 
Flat Cay            S. iserti             1         25 
Flat Cay           S. taeniopterus     2           15 


I want to know how I can repeat the rows in this df by the 
value in the value column so I can get species abundance and size 
frequency information.  The desired result would look like this 

Site                     Scientific.name              size 
Flat Cay                  S. iserti                     2.5 
Flat Cay                 S. iserti                      2.5 
Flat Cay                   S. iserti                    2.5 
Flat Cay                  S. iserti                       8 
Flat Cay                  S. iserti                       8 
Flat Cay                  S. iserti                       25 
Flat Cay                 S. taeniopterus              15 
Flat Cay                 S. taeniopterus              15 



I am able to use the cast function to get a species matrix e.g.: 

Site    S. iserti    S. taeniopterus 


but i can't use it to analyze frequency of size class etc. 

Thanks so much for your help!



More information about the R-help mailing list