Hi R listers, I am trying to group distances of nests to the vegetation into classes that are define by (0-5m, 6-10m, 11-15m, 16-20m, 21-25m, 26-30m, 31-35m, 36-40m, 41-45m, 46-50m, 51-55m, 56-60m). Each row is a nest and all the distances to the vegetation is in a column. In plyr, I have tried - below script but I think I am going about this the wrong way and am not successful. #Veg index star = resp Veg.index <- function(values, weights=c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70)) { star <- values*weights return(apply(star, 1, sum) / apply(values, 1, sum)) } data.to.analyze$VegIndex <- Veg.index(data.to.analyze[,c("0", "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70")]) write.csv(data.to.analyze, "3turtlehatch.csv", row.names=FALSE) Please advise, Jean -- View this message in context: http://r.789695.n4.nabble.com/Creating-vegetation-distance-groups-from-one-column-tp4644970.html Sent from the R help mailing list archive at Nabble.com.
Jean V Adams
2012-Oct-04 12:21 UTC
[R] Creating vegetation distance groups from one column
Jean, Take a look at the cut() function, ?cut For example ... mydf <- data.frame(nest=1:100, d2veg=runif(100, 0, 60)) mydf$dgroup <- cut(mydf$d2veg, breaks=seq(0, 70, 5), include.lowest=TRUE) head(mydf) (another) Jean Jhope <jeanwaijang@gmail.com> wrote on 10/04/2012 02:27:38 AM:> > Hi R listers, > > I am trying to group distances of nests to the vegetation into classesthat> are define by (0-5m, 6-10m, 11-15m, 16-20m, 21-25m, 26-30m, 31-35m,36-40m,> 41-45m, 46-50m, 51-55m, 56-60m). Each row is a nest and all thedistances to> the vegetation is in a column. > > In plyr, I have tried - below script but I think I am going about thisthe> wrong way and am not successful. > > #Veg index star = resp > Veg.index <- function(values, weights=c(0, 5, 10, 15, 20, 25, 30, 35,40,> 45, 50, 55, 60, 65, 70)) { > star <- values*weights > return(apply(star, 1, sum) / apply(values, 1, sum)) > } > data.to.analyze$VegIndex <- Veg.index(data.to.analyze[,c("0", "5", "10", > "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65","70")])> write.csv(data.to.analyze, "3turtlehatch.csv", row.names=FALSE) > > Please advise, Jean[[alternative HTML version deleted]]
Rui Barradas
2012-Oct-04 12:27 UTC
[R] Creating vegetation distance groups from one column
Hello, Without sample data it's difficult to give an answer but see ?cut. To give a data example, the best way is to use ?dput(). dput( head(mydata, 30) ) # Paste the output of this in a post Hope this helps, Rui Barradas Em 04-10-2012 08:27, Jhope escreveu:> Hi R listers, > > I am trying to group distances of nests to the vegetation into classes that > are define by (0-5m, 6-10m, 11-15m, 16-20m, 21-25m, 26-30m, 31-35m, 36-40m, > 41-45m, 46-50m, 51-55m, 56-60m). Each row is a nest and all the distances to > the vegetation is in a column. > > In plyr, I have tried - below script but I think I am going about this the > wrong way and am not successful. > > #Veg index star = resp > Veg.index <- function(values, weights=c(0, 5, 10, 15, 20, 25, 30, 35, 40, > 45, 50, 55, 60, 65, 70)) { > star <- values*weights > return(apply(star, 1, sum) / apply(values, 1, sum)) > } > data.to.analyze$VegIndex <- Veg.index(data.to.analyze[,c("0", "5", "10", > "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70")]) > write.csv(data.to.analyze, "3turtlehatch.csv", row.names=FALSE) > > Please advise, Jean > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Creating-vegetation-distance-groups-from-one-column-tp4644970.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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, Try this: data.to.analyze<-read.table(text=" Area Veg 456?????? 0 3400????? 10 79????????? 25 56?????????? 18 467???????? 4 67????????? 7 839??????? 30 1120????? 16 3482????? 32 ",sep="",header=TRUE) data.to.analyze$VegIndex=cut(data.to.analyze ? $Veg,breaks=c(-5,0,5,10,15,20,25,30,35), ??? labels=c("(-5-0)", "(1-5)", "(6-10)", "(11-15)", "(16-20)","(21-25)","(26-30)","(31-35)")) VegIndex <- data.to.analyze$VegIndex plot(VegIndex) A.K. ----- Original Message ----- From: Jhope <jeanwaijang at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, October 5, 2012 3:38 AM Subject: Re: [R] Creating vegetation distance groups from one column Thank you! That has worked for me when creating graphs. In plyr I used the script: # Veg Index data.to.analyze$VegIndex <- cut(data.to.analyze$Veg, ? ? ? breaks=seq(0, 35, 5), include.lowest=TRUE) VegIndex <- data.to.analyze$VegIndex plot(VegIndex) But the vegetation distances on the x-axis in the graph are showing up as: [-5,0] (0,5] (5,10] (10,15] (15,20] (20,25] (25,30] I am concerned these vegetation classes are not grouped probably and there is overlap between the classes. It should read, preferably without brackets or only one kind (): (-5-0) (1-5) (6-10) (11-15) (16-20) (21-25) (26-30) How do I fix this? Please advise, Jean -- View this message in context: http://r.789695.n4.nabble.com/Creating-vegetation-distance-groups-from-one-column-tp4644970p4645127.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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 Thanks to all for posting! In plyr I did try something similar, a combination of what I learned from all who replied. It seemed to work. If anyone knows of any criticism about the grouping structure please let me know. I need this to be accurate. Grouping defined by (0-5m) (6-10m) (11-15m) (16-20m) (21-25m) (26-30m) (31-35m) without overlap of measurements. See script below: data.to.analyze$VegIndex = cut (data.to.analyze$Veg,breaks=c(0,5,10,15,20,25,30,35), labels=c("(0-5)", "(6-10)", "(11-15)", "(16-20)", "(21-25)", "(26-30)", "(31-35)")) VegIndex <- data.to.analyze$VegIndex plot(VegIndex) Saludos, Jean -- View this message in context: http://r.789695.n4.nabble.com/Creating-vegetation-distance-groups-from-one-column-tp4644970p4645235.html Sent from the R help mailing list archive at Nabble.com.