Hi all, I am trying to convert geometric means in a matrix to cover classes. My values are as such: perc<-c(0,0.025136418, 0.316227766, 1.414213562,3.16227766, 7.071067812, 15.8113883, 35.35533906, 61.23724357, 84.40971508, 97.46794345) cover<-c(0,1,2,3,4,5,6,7,8,9,10) This is what I am trying to accomplish veg_mean[veg_mean>0 && veg_mean < .1] <- 1 veg_mean[veg_mean>= .1 && veg_mean < 1.0] <- 2 veg_mean[veg_mean>=1.0 && veg_mean < 2.0] <- 3 veg_mean[veg_mean>=2.0 && veg_mean < 5.0] <- 4 veg_mean[veg_mean>= 5.0 && veg_mean < 10.0] <- 5 veg_mean[veg_mean>= 10.0 && veg_mean < 25] <- 6 veg_mean[veg_mean>= 25.0 && veg_mean < 50.0] <- 7 veg_mean[veg_mean>=50.0 && veg_mean < 75.0] <- 8 veg_mean[veg_mean>= 75.0 && veg_mean < 95.0 ] <- 9 veg_mean[veg_mean>= 95.0 && veg_mean <= 100] <- 10 veg_mean[veg_mean> 100] <- NA where values are assigned based on the geometric means. However, I think that my syntax for the && operator is wrong and I can't find a reference to proper syntax. I basically want to "bin" the geometric means. Any help would be greatly appreciated. Thanks, Wade [[alternative HTML version deleted]]
Use '&' for vectors and '&&' for scalars. Ditto applies to the OR operator(s). /Henrik On Mon, Jul 28, 2008 at 10:36 AM, Wade Wall <wade.wall at gmail.com> wrote:> Hi all, > > I am trying to convert geometric means in a matrix to cover classes. My > values are as such: > > perc<-c(0,0.025136418, 0.316227766, 1.414213562,3.16227766, 7.071067812, > 15.8113883, 35.35533906, 61.23724357, 84.40971508, 97.46794345) > cover<-c(0,1,2,3,4,5,6,7,8,9,10) > > This is what I am trying to accomplish > > veg_mean[veg_mean>0 && veg_mean < .1] <- 1 > veg_mean[veg_mean>= .1 && veg_mean < 1.0] <- 2 > veg_mean[veg_mean>=1.0 && veg_mean < 2.0] <- 3 > veg_mean[veg_mean>=2.0 && veg_mean < 5.0] <- 4 > veg_mean[veg_mean>= 5.0 && veg_mean < 10.0] <- 5 > veg_mean[veg_mean>= 10.0 && veg_mean < 25] <- 6 > veg_mean[veg_mean>= 25.0 && veg_mean < 50.0] <- 7 > veg_mean[veg_mean>=50.0 && veg_mean < 75.0] <- 8 > veg_mean[veg_mean>= 75.0 && veg_mean < 95.0 ] <- 9 > veg_mean[veg_mean>= 95.0 && veg_mean <= 100] <- 10 > veg_mean[veg_mean> 100] <- NA > > where values are assigned based on the geometric means. However, I think > that my syntax for the && operator is wrong and I can't find a reference to > proper syntax. I basically want to "bin" the geometric means. > > Any help would be greatly appreciated. > > Thanks, > > Wade > > [[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, maybe the following code helps to achieve what you want? It seems to me it is basically a 'recode' question. set.seed(1234) # not neccessary but this ensures we have the same #results random.values <- runif(n=30, min=0, max=100) newgrouping <- cut(x=random.values, breaks=c(0,0.1, 1, 2, 5, 10, 25, 50, 75, 95, 100), labels=1:10) cbind(random.values,newgrouping) ## should result in: random.values newgrouping [1,] 11.3703411 6 [2,] 62.2299405 8 [3,] 60.9274733 8 [4,] 62.3379442 8 [5,] 86.0915384 9 [6,] 64.0310605 8 [7,] 0.9495756 2 [8,] 23.2550506 6 [9,] 66.6083758 8 [10,] 51.4251141 8 [11,] 69.3591292 8 [12,] 54.4974836 8 [13,] 28.2733584 7 [14,] 92.3433484 9 etc. I hope this helps, Roland Wade Wall wrote:> Hi all, > > I am trying to convert geometric means in a matrix to cover classes. My > values are as such: > > perc<-c(0,0.025136418, 0.316227766, 1.414213562,3.16227766, 7.071067812, > 15.8113883, 35.35533906, 61.23724357, 84.40971508, 97.46794345) > cover<-c(0,1,2,3,4,5,6,7,8,9,10) > > This is what I am trying to accomplish > > veg_mean[veg_mean>0 && veg_mean < .1] <- 1 > veg_mean[veg_mean>= .1 && veg_mean < 1.0] <- 2 > veg_mean[veg_mean>=1.0 && veg_mean < 2.0] <- 3 > veg_mean[veg_mean>=2.0 && veg_mean < 5.0] <- 4 > veg_mean[veg_mean>= 5.0 && veg_mean < 10.0] <- 5 > veg_mean[veg_mean>= 10.0 && veg_mean < 25] <- 6 > veg_mean[veg_mean>= 25.0 && veg_mean < 50.0] <- 7 > veg_mean[veg_mean>=50.0 && veg_mean < 75.0] <- 8 > veg_mean[veg_mean>= 75.0 && veg_mean < 95.0 ] <- 9 > veg_mean[veg_mean>= 95.0 && veg_mean <= 100] <- 10 > veg_mean[veg_mean> 100] <- NA > > where values are assigned based on the geometric means. However, I think > that my syntax for the && operator is wrong and I can't find a reference to > proper syntax. I basically want to "bin" the geometric means. > > Any help would be greatly appreciated. > > Thanks, > > Wade > > [[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. >
See ?cut for creating a factor based on ranges of values. Regards, Wade Wall wrote:> > Hi all, > > I am trying to convert geometric means in a matrix to cover classes. My > values are as such: > > perc<-c(0,0.025136418, 0.316227766, 1.414213562,3.16227766, 7.071067812, > 15.8113883, 35.35533906, 61.23724357, 84.40971508, 97.46794345) > cover<-c(0,1,2,3,4,5,6,7,8,9,10) > > This is what I am trying to accomplish > > veg_mean[veg_mean>0 && veg_mean < .1] <- 1 > veg_mean[veg_mean>= .1 && veg_mean < 1.0] <- 2 > veg_mean[veg_mean>=1.0 && veg_mean < 2.0] <- 3 > veg_mean[veg_mean>=2.0 && veg_mean < 5.0] <- 4 > veg_mean[veg_mean>= 5.0 && veg_mean < 10.0] <- 5 > veg_mean[veg_mean>= 10.0 && veg_mean < 25] <- 6 > veg_mean[veg_mean>= 25.0 && veg_mean < 50.0] <- 7 > veg_mean[veg_mean>=50.0 && veg_mean < 75.0] <- 8 > veg_mean[veg_mean>= 75.0 && veg_mean < 95.0 ] <- 9 > veg_mean[veg_mean>= 95.0 && veg_mean <= 100] <- 10 > veg_mean[veg_mean> 100] <- NA > > where values are assigned based on the geometric means. However, I think > that my syntax for the && operator is wrong and I can't find a reference > to > proper syntax. I basically want to "bin" the geometric means. > > Any help would be greatly appreciated. > > Thanks, > > Wade > > [[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. > >-- View this message in context: http://www.nabble.com/Case-statements-in-R-tp18695725p18696107.html Sent from the R help mailing list archive at Nabble.com.
Maybe Matching Threads
- R 2.0.0: namespaces, S4 classes & versioned package installation: failure to resolve correct pkg version
- gamma distribution don't allow negative value in GLMs?
- quantiles for geometric distribution
- Use of geometric mean for geochemical concentrations
- metafor - code for analysing geometric means