[R-sig-eco] problem using reshape package
Michael Denslow
michael.denslow at gmail.com
Thu Jul 9 15:38:08 CEST 2009
Hi Dulce,
On Thu, Jul 9, 2009 at 7:37 AM, Maria Dulce
Subida<mdsubida at icman.csic.es> wrote:
> Hello everyone!
>
> I'm having a problem in casting a data frame with the reshape package. I
> have an original data set of species abundances in replicate samples at
> certain sites, with the following form:
>
>
>
> SITE SPECIES Replicate1 Replicate2 Replicate3
>
> I A 1 3 0
>
> I B 2 5 1
>
> I C 1 0 2
>
> II A 4 2 0
>
> II C 1 0 0
>
> II D 6 3 3
>
>
>
> Please notice that site II does not have species B and has a new species D,
> the remaining two are shared with site I.
>
>
>
> I need to get these data in the form of a matrix like:
>
>
>
> SITE.REPLICATE A B C D
>
> I.1 1 2 1 NA
>
> I.2 3 5 0 NA
>
> I.3 0 1 2 NA
>
> II.1 4 NA 1 6
>
> II.2 2 NA 0 3
>
> II.3 0 NA 0 3
>
>
>
> Using the above "toy data" in R, everything works fine using melt and recast
> as follows (lets call test to may initial matrix):
>
>> testm <- melt (test, id.var=c("SITE","SPECIES"))
>
>> testc <- cast(testm, ...~SPECIES)
>
>> testc
>
> SITE variable A B C D
>
> 1 I Replicate1 1 2 1 NA
>
> 2 I Replicate2 3 5 0 NA
>
> 3 I Replicate3 0 1 2 NA
>
> 4 II Replicate1 4 NA 1 6
>
> 5 II Replicate2 2 NA 0 3
>
> 6 II Replicate3 0 NA 0 3
>
>
>
> However, when I use the same code in my real data set which is considerably
> larger (73 sites and 7 replicates for site, resulting in a molten matrix of
> 14469 x 4), the cast function does some kind of aggregation (in fact it
> advices of an aggregation using the default fun.aggregate) that I was not
> able to understand. I also tried to split my original data frame in order to
> get molten matrices smaller than 5500x4, but I got the same problem.
>
> Could anyone help me with this?
>
>
>
> (I use R 2.8.1 for Windows)
>
>
>
> Thank you very very much in advance!
>
>
>
>
>
> Cheers,
>
>
>
> Dulce
Sorry I can't answer your question but have another suggestion to try.
I am a bit confused by the format of your original data frame.
However, I do this sort of matrification often with the matrify()
function in the labdsv package.
see
library(labdsv)
?matrify
hope this helps,
Michael
--
Michael Denslow
Graduate Student
I.W. Carpenter Jr. Herbarium [BOON]
Department of Biology
Appalachian State University
Boone, North Carolina U.S.A.
-- AND --
Communications Manager
Southeast Regional Network of Expertise and Collections
sernec.org
More information about the R-sig-ecology
mailing list