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]]
Sorry, but i don't get the problem at all. Could you provide a bit of y by using dput()? Can you provide an example of how you want the data to look like after the transformation? On 16.01.2013, at 16:42, Andrea Goijman wrote:> 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.
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-bounces@r-project.org [mailto:r-help-bounces@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}}
Hi, May be this helps you. source("Andreadata.txt") ?head(occ.data) ?melting<- melt(occ.data,id.var=c("Point", "Site", "Rep", "A?o"),measure.var="Pres") ?y<-cast(melting,Site~Rep~Point~A?o) ?dim(y) #[1] 10? 5 25? 6 y[,,25,6] #????? Rep #Site?? 1 2 3 4 5 ?# 1021 0 0 0 0 0 ?# 1022 0 0 0 0 0 ?# 1051 0 0 0 0 0 ?# 2073 0 0 0 0 0 ?# 2194 0 0 0 0 0 ?# 2195 0 0 0 0 0 ?# 3055 0 0 0 0 7 ?# 4072 0 0 0 0 0 ?# 4073 0 0 0 0 0 ?# 6202 0 0 0 0 0 library(plyr) ?y1<-aaply(y,1,function(x) {x[x>1]<-1;x}) dim(y1) #[1] 10? 5 25? 6 y1[,,25,6] #????? Rep #Site?? 1 2 3 4 5 ?# 1021 0 0 0 0 0 ?# 1022 0 0 0 0 0 ?# 1051 0 0 0 0 0 ?# 2073 0 0 0 0 0 ?# 2194 0 0 0 0 0 ?# 2195 0 0 0 0 0 ?# 3055 0 0 0 0 1 ?# 4072 0 0 0 0 0 ?# 4073 0 0 0 0 0 ?# 6202 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.
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.