[R] help "reshaping" dataframe

arun smartpink111 at yahoo.com
Sat Jan 5 04:44:57 CET 2013


Hi,
You could also use ?dcast()
occ.data1<-occ.data[,-c(2:3)]
library(reshape2)
res1<-dcast(occ.data1,year+Site+specie+Pres~Rep,value.var="Rep")
names(res1)[grep("[0-9]",names(res1))]<-paste("Rep",1:5,sep="")

res1[,-c(1:4)]<-sapply(res1[,-c(1:4)],function(x) as.integer(is.na(x)))

res1
#  year Site specie Pres Rep1 Rep2 Rep3 Rep4 Rep5
#1 2003 2021   MICH    1    1    1    0    0    1
#2 2003 2021   MISA    1    0    1    1    1    1
#3 2003 2021   MOBO    1    0    0    0    0    1
#4 2003 2021   SILU    1    0    1    1    0    1
#5 2003 2021   TYSA    1    1    1    1    1    0
#6 2003 2021   ZEAU    1    0    1    0    0    0
#7 2003 2021   ZOCA    1    0    1    1    0    0
#8 2003 2022   MICH    1    1    1    1    1    0
#9 2003 2022   MISA    1    1    0    0    1    1

A.K. 





----- Original Message -----
From: Andrea Goijman <agoijman at cnia.inta.gov.ar>
To: R help <r-help at r-project.org>
Cc: 
Sent: Friday, January 4, 2013 6:15 PM
Subject: [R] help "reshaping" dataframe

List,

I want to reshape my data, but I'm not sure how to do it... it might be a
simple task, but don't know which package does this.
"occ.data" (see below) is how my original data are arranged, and I know
that with melt() I can reshape it like "y" (see below). However, I just
want to build a matrix like the "y" matrix, but with only 2 dimensions.
Something like this:

    year    Site      specie  Pres  Rep1  Rep2  Rep3  Rep4  Rep5
1  2003   2021    MICH     1          0        0           1        1
0
3  2003   2021    MISA     1          1        0            0        0
  0
4  2003   2021    MOBO    1         1        1            0        0
0

where "year" and "specie" are not another dimension, they are different
columns; and Rep is the other dimension



> occ.data <- read.table("Occ_03.csv", header=TRUE,sep=",",na.strings=TRUE)
> occ.data[1:20,]
    year Ruta Point Site specie Pres Rep
1  2003      202     3 2021    MICH    1   3
2  2003      202     4 2021    MICH    1   4
3  2003      202     1 2021    MISA    1   1
4  2003      202     1 2021    MOBO    1   1
5  2003      202     2 2021    MOBO    1   2
6  2003      202     3 2021    MOBO    1   3
7  2003      202     4 2021    MOBO    1   4
8  2003      202     1 2021    SILU    1   1
9  2003      202     4 2021    SILU    1   4
10 2003      202     5 2021    TYSA    1   5
11 2003      202     1 2021    ZEAU    1   1
12 2003      202     3 2021    ZEAU    1   3
13 2003      202     4 2021    ZEAU    1   4
14 2003      202     5 2021    ZEAU    1   5
15 2003      202     1 2021    ZOCA    1   1
16 2003      202     4 2021    ZOCA    1   4
17 2003      202     5 2021    ZOCA    1   5
18 2003      202    10 2022    MICH    1   5
19 2003      202     7 2022    MISA    1   2
20 2003      202     8 2022    MISA    1   3
>
> ###Reshape the data using the R package "reshape"
> library(reshape)
>
> all.melt=melt(occ.data,id.var=c("specie", "Site", "Rep", "year"),
measure.var="Pres")
> y=cast(all.melt, Site ~ Rep ~ specie ~ year)
>
> y[is.na(y)] <- 0
>
> y[1:10,,1,]
, , year = 2003

      Rep
Site   1 2 3 4 5
  1021 0 0 0 0 0
  1022 0 0 0 0 0
  1023 0 0 0 0 0
  1024 0 0 0 0 0
  1025 0 0 0 0 0
  1026 0 0 0 0 0
  2021 0 0 0 0 0
  2022 0 0 0 0 0
  2023 0 0 0 0 0
  2024 0 0 0 0 0

, , year = 2004

      Rep
Site   1 2 3 4 5
  1021 0 0 0 0 1
  1022 1 0 0 0 0
  1023 0 0 0 0 0
  1024 0 0 0 0 0
  1025 0 0 0 0 0
  1026 0 0 0 0 0
  2021 0 0 0 0 0
  2022 0 0 0 0 0
  2023 0 0 0 0 0
  2024 0 0 0 0 0

    [[alternative HTML version deleted]]

______________________________________________
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.





More information about the R-help mailing list