Dear Andrea


I may have not understood the question properly, but I guess it has to do with 
replacing values before and after casting.

The molten object ('melting') is a data frame, whilst the cast object ('y') is 
an array.



Crucially, the values of the variable "Rep" in the 'melting' data frame become 
the names of the columns in each component within the cast array object. So, if 
you want to change the values where rep>=1 to 1, you have to do that before 
casting the molten object.



If you look at ?cast, you get this example:



> names(airquality) <- tolower(names(airquality))

> aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)

> cast(aqm, day ~ month ~ variable)



Let's assign the cast result to an object:



> y=cast(aqm, day ~ month ~ variable)





Now let's have a look at the three objects:



> head(airquality)

  ozone solar.r wind temp month day

1    41     190  7.4   67     5   1

2    36     118  8.0   72     5   2

3    12     149 12.6   74     5   3

4    18     313 11.5   62     5   4

5    NA      NA 14.3   56     5   5

6    28      NA 14.9   66     5   6



> head(aqm)

  month day variable value

1     5   1    ozone    41

2     5   2    ozone    36

3     5   3    ozone    12

4     5   4    ozone    18

5     5   6    ozone    28

6     5   7    ozone    23



> y[1:10,,"ozone"]

    month

day   5  6   7   8  9

  1  41 NA 135  39 96

  2  36 NA  49   9 78

  3  12 NA  32  16 73

  4  18 NA  NA  78 91

  5  NA NA  64  35 47

  6  28 NA  40  66 32

  7  23 29  77 122 20

  8  19 NA  97  89 23

  9   8 71  97 110 21

  10 NA 39  85  NA 24



> y[1:10,,"wind"]

    month

day     5    6    7    8    9

  1   7.4  8.6  4.1  6.9  6.9

  2   8.0  9.7  9.2 13.8  5.1

  3  12.6 16.1  9.2  7.4  2.8

  4  11.5  9.2 10.9  6.9  4.6

  5  14.3  8.6  4.6  7.4  7.4

  6  14.9 14.3 10.9  4.6 15.5

  7   8.6  9.7  5.1  4.0 10.9

  8  13.8  6.9  6.3 10.3 10.3

  9  20.1 13.8  5.7  8.0 10.9

  10  8.6 11.5  7.4  8.6  9.7



Etc.



So, if you want to change month>5 to 5, you have to do that before casting the 
aqm dataframe. Once cast, what you get is an array with (in this example) the 
values for the variable month as column names within each component of the 
array.



Hope this helps,





José




PS: As an aside, from what I see, there are five Reps (1 to 5), so changing 
rep>=1 to 1 will leave you with only 1 category -namely a vector in the arrays. 
That's fine but perhaps you meant rep>1 rather than rep>=1?









-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Andrea Goijman
Sent: 16 January 2013 15:42
To: R help
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]]

Wrap Up and Run 10k is back! 

Also, new for 2013 – 2km intergenerational walks at selected venues. So recruit 
a buddy, dust off the trainers and beat the winter blues by 
signing up now:

http://www.ageuk.org.uk/10k

                 Milton Keynes | Oxford | Sheffield | Crystal Palace | Exeter | 
Harewood House, Leeds | 
                                 Tatton Park, Cheshire | Southampton | Coventry



Age UK Improving later life

http://www.ageuk.org.uk


 

-------------------------------
Age UK is a registered charity and company limited by guarantee, (registered 
charity number 1128267, registered company number 6825798). 
Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA.

For the purposes of promoting Age UK Insurance, Age UK is an Appointed 
Representative of Age UK Enterprises Limited, Age UK is an Introducer 
Appointed Representative of JLT Benefit Solutions Limited and Simplyhealth 
Access for the purposes of introducing potential annuity and health 
cash plans customers respectively.  Age UK Enterprises Limited, JLT Benefit 
Solutions Limited and Simplyhealth Access are all authorised and 
regulated by the Financial Services Authority. 
------------------------------

This email and any files transmitted with it are confide...{{dropped:31}}

______________________________________________
R-help@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.

Reply via email to