[R] Changing frequency values to 1 and 0

arun smartpink111 at yahoo.com
Wed Jan 16 19:03:54 CET 2013


HI,

Saw ur post in Nabble.
occ.data<-read.table(text="
Año Punto Especie Pres Ruta_com Point Site Rep guild
1  2012    30    TYSA    1      108    30 1086   5 OTHER
2  2012    26    VACH    1      108    26 1086   1 OTHER
3  2012    27    VACH    1      108    27 1086   2 OTHER
4  2012    26    ZEAU    1      108    26 1086   1 OTHER
5  2012    27    ZEAU    1      108    27 1086   2 OTHER
6  2012    28    ZEAU    1      108    28 1086   3 OTHER
7  2012    30    ZEAU    1      108    30 1086   5 OTHER
8  2012     7    TYSA    1      111     7 1112   2 OTHER
9  2012     6    ZEAU    1      111     6 1112   1 OTHER
10 2012    10    ZEAU    1      111    10 1112   5 OTHER
11 2012    24    TYSA    1      111    24 1115   4 OTHER
12 2012    23    VACH    1      111    23 1115   3 OTHER
13 2012    21    ZEAU    1      111    21 1115   1 OTHER
14 2012    23    ZEAU    1      111    23 1115   3 OTHER
15 2012    24    ZEAU    1      111    24 1115   4 OTHER
16 2012    25    ZEAU    1      111    25 1115   5 OTHER
17 2012    28    AMHU    1      111    28 1116   3 gui4b
18 2012    29    AMHU    1      111    29 1116   4 gui4b
19 2012    30    AMHU    1      111    30 1116   5 gui4b
20 2012    27    TYSA    1      111    27 1116   2 OTHER
21 2012    26    VACH    1      111    26 1116   1 OTHER
22 2012    27    VACH    1      111    27 1116   2 OTHER
23 2012    26    ZEAU    1      111    26 1116   1 OTHER
24 2012    27    ZEAU    1      111    27 1116   2 OTHER
25 2012    29    ZEAU    1      111    29 1116   4 OTHER
26 2012    28    ZOCA    1      111    28 1116   3 gui4b
27 2012    29    ZOCA    1      111    29 1116   4 gui4b
28 2012    30    ZOCA    1      111    30 1116   5 gui4b
29 2012     5    AMHU    1      205     5 2051   5 gui4b
30 2012     3    SILU    1      205     3 2051   3 gui4b 
",sep="",header=TRUE,stringsAsFactors=FALSE)

 junk.melt<- melt(occ.data,id.var=c("guild", "Site", "Rep", "Año"),
 measure.var="Pres") 
  y<-cast(junk.melt, Site ~ Rep ~ guild ~ Año) 
 y
#, , guild = gui4b, Año = 2012
#
 #     Rep
#Site   1 2 3 4 5
 # 1086 0 0 0 0 0
 # 1112 0 0 0 0 0
 # 1115 0 0 0 0 0
 # 1116 0 0 2 2 2
 # 2051 0 0 1 0 1

#, , guild = OTHER, Año = 2012
#
 #     Rep
#Site   1 2 3 4 5
 # 1086 2 2 1 0 2
 # 1112 1 1 0 0 1
 # 1115 1 0 2 2 1
 # 1116 2 3 0 1 0
  2051 0 0 0 0 0
library(plyr)
 aaply(y,1,function(x) {x[x>1]<-1;x})
#, , guild = gui4b
#
#      Rep
#Site   1 2 3 4 5
 # 1086 0 0 0 0 0
 # 1112 0 0 0 0 0
 # 1115 0 0 0 0 0
 # 1116 0 0 1 1 1
 # 2051 0 0 1 0 1

#, , guild = OTHER
#
 #     Rep
#Site   1 2 3 4 5
 # 1086 1 1 1 0 1
 # 1112 1 1 0 0 1
 # 1115 1 0 1 1 1
 # 1116 1 1 0 1 0
 # 2051 0 0 0 0 0
A.K.






----- Original Message -----
From: Andrea Goijman <agoijman at cnia.inta.gov.ar>
To: R help <r-help at r-project.org>
Cc: 
Sent: Wednesday, January 16, 2013 10:42 AM
Subject: [R] Changing frequency values to 1 and 0

Dear list,

I'm working with a large data set, where I grouped several species in one
group (guild). Then I reshaped my data as shown below. Now, I just want to
have "Rep" only as 1 or 0.

I'm not being able to change the values of rep>=1 to 1... tried many things
and I'm not being successful!


> melting=melt(occ.data,id.var=c("guild", "Site", "Rep", "Año"),
measure.var="Pres")
> y=cast(melting, Site ~ Rep ~ guild ~ Año)
Aggregation requires fun.aggregate: length used as default

> y[1:10,,"gui4a",1:2]
, , Año = 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
*  1051 3 1 2 3 5*
*  1052 4 3 5 2 3*
*  1053 4 3 3 3 2*
*  1054 1 2 1 3 0*

, , Año = 2004

      Rep
Site   1 2 3 4 5
  1021 2 5 5 5 4
  1022 6 3 2 2 2
  1023 4 1 1 2 2
  1024 0 1 2 2 0
  1025 0 1 0 1 0
  1026 2 1 0 0 1
  1051 2 1 3 1 2
  1052 2 4 1 1 2
  1053 2 4 2 2 1
  1054 4 3 3 2 3

    [[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