Skip to content

Creating vegetation distance groups from one column

11 messages · Jean V Adams, Jhope, arun +1 more

#
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.
#
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,

Allow me to recap my question. In plyr I am trying to group distances of
nests to the vegetation into classes that are defined by (0-5m, 6-10m,
11-15m, 16-20m, 21-25m, 26-30m, 31-35m). Each row is a nest and all the
distances to the vegetation is in a column. 

As well I would like to create classes from the HTL starting at (-5-0m,
1-5m, 6-10m, 11-15m, 16-20m, 21-25m, 26m-30m). 
 
I have included a sample of the data through dput() - see below. If that is
too tiresome, I've also included head(data.to.analyze) below that. 

Best, Jean
structure(list(QuadratEvent = c("8.1A", "10.1A", "14.1A", "19.1A", 
"23.1A", "24.1A", "25.1A", "29.1A", "30.1A", "31.1A", "32.1A", 
"33.1A", "38.1A", "39.1A", "41.1A", "47.1A", "48.1A", "50.1A", 
"52.1A", "53.1A", "58.1A", "63.1A", "68.1A", "73.1A", "75.1A", 
"79.1A", "29BA", "90.1B", "90.2B", "82.1B"), ExDate = c("Nov. 26, 2010", 
"Nov. 26, 2010", "Nov. 18, 2010", "Nov. 22, 2010", "Nov. 20, 2010", 
"Nov. 28, 2010", "Nov. 28, 2010", "Nov. 27, 2010", "Nov. 20, 2010", 
"Nov. 20, 2010", "Nov. 26, 2010", "Nov. 26, 2010", "Nov. 20, 2010", 
"Nov. 26, 2010", "Nov. 27, 2010", "Nov. 27, 2010", "Nov. 26, 2010", 
"Nov. 27, 2010", "Nov. 27, 2010", "Nov. 27, 2010", "Nov. 27, 2010", 
"Nov. 26, 2010", "Nov. 26, 2010", "Nov. 28, 2010", "Nov. 28, 2010", 
"Nov. 26, 2010", "Nov. 28, 2010", "Jan. 1, 2011", "Jan. 1, 2011", 
"Dec. 23, 2010"), Sector = c("14", "13", "20", "79", "34", "34", 
"34", "36", "67", "66", "67", "67", "67", "66", "70", "70", "68", 
"57", "55", "56", "40", "27", "26", "27", "28", "19", "36", "70", 
"70", "47"), Quadrat = c("8", "10", "14", "19", "23", "24", "25", 
"29", "30", "31", "32", "33", "38", "39", "41", "47", "48", "50", 
"52", "53", "58", "63", "68", "73", "75", "79", "29B", "90", 
"90", "82"), Aeventexhumed = c("A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B"), ST0 = c(6L, 
4L, 6L, 0L, 135L, 660L, 321L, 608L, 50L, 5L, 88L, 48L, 7L, 60L, 
0L, 69L, 195L, 68L, 162L, 47L, 595L, 144L, 111L, 478L, 636L, 
136L, 515L, 213L, 0L, 175L), ST1 = c(0L, 0L, 0L, 0L, 23L, 0L, 
0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 39L, 2L, 1L, 4L, 
9L, 1L, 5L, 25L, 4L, 139L, 0L, 0L, 0L), ST2 = c(1L, 0L, 2L, 0L, 
1L, 2L, 2L, 0L, 153L, 0L, 0L, 0L, 117L, 93L, 3L, 2L, 0L, 0L, 
6L, 2L, 0L, 17L, 1L, 11L, 67L, 0L, 31L, 68L, 0L, 0L), ST3 = c(98L, 
109L, 26L, 0L, 6L, 0L, 0L, 0L, 1L, 3L, 11L, 88L, 3L, 342L, 9L, 
3L, 4L, 72L, 7L, 81L, 0L, 33L, 103L, 0L, 0L, 218L, 162L, 0L, 
74L, 0L), ST4 = c(1L, 1L, 11L, 0L, 0L, 0L, 0L, 0L, 27L, 1L, 0L, 
0L, 7L, 0L, 8L, 6L, 0L, 0L, 5L, 4L, 0L, 38L, 150L, 0L, 0L, 19L, 
5L, 0L, 0L, 0L), Shells = c(72L, 74L, 106L, 102L, 125L, 4L, 15L, 
0L, 31L, 111L, 0L, 0L, 86L, 48L, 21L, 35L, 0L, 0L, 12L, 13L, 
0L, 23L, 20L, 3L, 4L, 115L, 5L, 2L, 0L, 0L), Hatchlings = c(3L, 
4L, 1L, 1L, 48L, 0L, 0L, 0L, 45L, 99L, 0L, 0L, 93L, 9L, 0L, 0L, 
0L, 6L, 2L, 6L, 0L, 9L, 13L, 0L, 0L, 9L, 0L, 0L, 0L, 0L), MaxHatch = c(72L, 
74L, 106L, 102L, 125L, 4L, 15L, 0L, 45L, 111L, 0L, 0L, 93L, 48L, 
21L, 35L, 0L, 6L, 12L, 13L, 0L, 23L, 20L, 3L, 4L, 118L, 5L, 2L, 
0L, 0L), DeadHatch = c(2L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 
0L, 0L, 1L, 5L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 6L, 
0L, 0L, 0L, 0L), Oldeggs = c(0L, 21L, 165L, 0L, 0L, 36L, 0L, 
0L, 96L, 45L, 5L, 142L, 10L, 81L, 103L, 92L, 0L, 0L, 0L, 0L, 
108L, 0L, 0L, 0L, 93L, 0L, 0L, 0L, 0L, 0L), TotalEggs = c(178L, 
188L, 151L, 102L, 290L, 666L, 338L, 608L, 280L, 120L, 99L, 136L, 
227L, 543L, 42L, 117L, 199L, 185L, 194L, 148L, 599L, 264L, 386L, 
497L, 732L, 495L, 857L, 283L, 74L, 175L), QuadratID = structure(c(107L, 
4L, 22L, 38L, 43L, 44L, 46L, 54L, 57L, 58L, 60L, 61L, 67L, 69L, 
72L, 79L, 80L, 81L, 83L, 85L, 91L, 94L, 97L, 103L, 105L, 106L, 
56L, 109L, NA, 108L), .Label = c("", "1.1", "1.2", "10.1", "10.2", 
"100.1", "100.2", "108.1", "108.2", "110.1", "110.2", "114.1", 
"114.2", "121.1", "121.2", "127.1", "127.2", "128.1", "128.2", 
"135.1", "135.2", "14.1", "15.1", "15.2", "151.1", "151.2", "152.1", 
"152.2", "153.1", "153.2", "154.1", "154.2", "155.1", "155.2", 
"16.1", "16.2", "18.1", "19.1", "21.1", "21.2", "22.1", "22.2", 
"23.1", "24.1", "24.2", "25.1", "25.2", "26.1", "26.2", "27.1", 
"27.2", "28.1", "28.2", "29.1", "29.2", "29B", "30.1", "31.1", 
"31.2", "32.1", "33.1", "33.2", "35.1", "35.2", "37.1", "37.2", 
"38.1", "38.2", "39.1", "40.1", "40.2", "41.1", "42.1", "42.2", 
"43.1", "43.2", "46.1", "46.2", "47.1", "48.1", "50.1", "50.2", 
"52.1", "52.2", "53.1", "53.2", "54.1", "54.2", "56.1", "56.2", 
"58.1", "62.1", "62.2", "63.1", "65.1", "65.2", "68.1", "68.2", 
"68.3", "68.4", "69.1", "70.1", "73.1", "74.1", "75.1", "79.1", 
"8.1", "82.1", "90.1", "94.1", "94.2"), class = "factor"), Sector = c(14L, 
13L, 20L, 79L, 34L, 34L, 34L, 36L, 67L, 66L, 67L, 67L, 67L, 66L, 
70L, 70L, 68L, 57L, 55L, 56L, 40L, 27L, 26L, 27L, 28L, 19L, 36L, 
70L, NA, 47L), Aeventexhumed = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, NA, 3L), .Label = c("", "A", "B", 
"C"), class = "factor"), Veg = c(9.75, 16.34, 3.15, 5.7, 12.2, 
4.82, 3.3, 1.85, 17.75, 27.13, 0.84, 8.45, 16.42, 20.4, 22.4, 
25, 25.4, 15.1, 13.7, 17.1, NA, 17.3, 24.3, 9.35, 5.4, 15.5, 
1.85, 11, NA, 3), Berm = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 10, NA, 11), HTL = c(4.66, -2.47, 16, NA, 2.35, 22.85, 
24, 15.7, 9.55, 4.2, 9.65, 19.8, 13.85, 8.7, 7.4, 2.9, 2.9, 4.26, 
1.2, 1.05, NA, 3.2, 3.8, 2.55, 6.55, 2, 15.7, 20, NA, 20), Estuary = c(NA, 
NA, NA, 11.8, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 3.5, NA, NA, NA, 8.05, NA, NA, NA, NA, NA), Rayos =
c("Rayos.1", 
"Rayos.1", "Rayos.2", "Rayos.2", "Rayos.3", "Rayos.3", "Rayos.3", 
"Rayos.3", "Rayos.5", "Rayos.5", "Rayos.5", "Rayos.5", "Rayos.5", 
"Rayos.5", "Rayos.5", "Rayos.5", "Rayos.5", "Rayos.4", "Rayos.4", 
"Rayos.4", "Rayos.3", "Rayos.2", "Rayos.2", "Rayos.2", "Rayos.2", 
"Rayos.2", "Rayos.3", "Rayos.5", "Rayos.NA", "Rayos.4"), NotHatched =
c(106L, 
114L, 45L, 0L, 165L, 662L, 323L, 608L, 249L, 9L, 99L, 136L, 141L, 
495L, 21L, 82L, 199L, 185L, 182L, 135L, 599L, 241L, 366L, 494L, 
728L, 380L, 852L, 281L, 74L, 175L), DevelopIndex = c(3.86516853932584, 
4.92021276595745, 5.62251655629139, 1, 3.40689655172414, 5.98198198198198, 
1.13313609467456, 2, 3.53571428571429, 1.15, 4.66666666666667, 
4.05882352941176, 2.26872246696035, 4.15469613259669, 5.21428571428571, 
2.97435897435897, 4.93969849246231, 4.1027027027027, 1.32474226804124, 
3.92567567567568, 3.02003338898164, 3.26136363636364, 4.03626943005181, 
5.95171026156942, 1.11885245901639, 4.06060606060606, 4.07117852975496, 
3.97879858657244, 2, 6)), .Names = c("QuadratEvent", "ExDate", 
"Sector", "Quadrat", "Aeventexhumed", "ST0", "ST1", "ST2", "ST3", 
"ST4", "Shells", "Hatchlings", "MaxHatch", "DeadHatch", "Oldeggs", 
"TotalEggs", "QuadratID", "Sector", "Aeventexhumed", "Veg", "Berm", 
"HTL", "Estuary", "Rayos", "NotHatched", "DevelopIndex"), row.names = c(NA, 
30L), class = "data.frame")


----------------------------------------
QuadratEvent        ExDate Sector Quadrat Aeventexhumed ST0 ST1 ST2 ST3
ST4 Shells
1         8.1A Nov. 26, 2010     14       8             A   6   0   1  98  
1     72
2        10.1A Nov. 26, 2010     13      10             A   4   0   0 109  
1     74
3        14.1A Nov. 18, 2010     20      14             A   6   0   2  26 
11    106
4        19.1A Nov. 22, 2010     79      19             A   0   0   0   0  
0    102
5        23.1A Nov. 20, 2010     34      23             A 135  23   1   6  
0    125
6        24.1A Nov. 28, 2010     34      24             A 660   0   2   0  
0      4
  Hatchlings MaxHatch DeadHatch Oldeggs TotalEggs QuadratID Sector
Aeventexhumed   Veg Berm
1          3       72         2       0       178       8.1     14            
A  9.75   NA
2          4       74         0      21       188      10.1     13            
A 16.34   NA
3          1      106         1     165       151      14.1     20            
A  3.15   NA
4          1      102         0       0       102      19.1     79            
A  5.70   NA
5         48      125         0       0       290      23.1     34            
A 12.20   NA
6          0        4         0      36       666      24.1     34            
A  4.82   NA
    HTL Estuary   Rayos NotHatched DevelopIndex
1  4.66      NA Rayos.1        106     3.865169
2 -2.47      NA Rayos.1        114     4.920213
3 16.00      NA Rayos.2         45     5.622517
4    NA    11.8 Rayos.2          0     1.000000
5  2.35      NA Rayos.3        165     3.406897
6 22.85      NA Rayos.3        662     5.981982



--
View this message in context: http://r.789695.n4.nabble.com/Creating-vegetation-distance-groups-from-one-column-tp4644970p4645066.html
Sent from the R help mailing list archive at Nabble.com.
#
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.
#
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.
#
Hello,

That now seems to be a presentation problem. See this example:


x <- 0:30 + runif(124)
y <- cut(x, breaks = seq(0, 35, 5))

l <- levels(y)
l1 <- sub("\\]", ")", l[1])
l2 <- as.numeric(sub("\\(([[:digit:]]+),.*", "\\1", l[-1])) + 1
l3 <- sub(".*,([[:digit:]]+).*", "\\1", l[-1])
l.new <- c(l1, paste0("(", l2, ",", l3, ")"))
levels(y) <- l.new

str(y)
barplot(table(y))


Instead of 'y' use data.to.analyze$VegIndex and it should give what you 
want.

Hope this helps,

Rui Barradas
Em 05-10-2012 08:38, Jhope escreveu:
#
Hi, 

I have tried the script posted but received the following errors.  I hope I
copied it correctly. I'm sorry but I don't know how to alter the script
myself.
Please advise, Jean
Error in `$<-.data.frame`(`*tmp*`, "VegIndex", value = c(1L, 1L, 1L, 1L,  : 
  replacement has 124 rows, data has 123
Error: could not find function "paste0"
Error: object 'l.new' not found
NULL
Error in plot.window(xlim, ylim, log = log, ...) : 
  need finite 'xlim' values
In addition: Warning messages:
1: In min(w.l) : no non-missing arguments to min; returning Inf
2: In max(w.r) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf



--
View this message in context: http://r.789695.n4.nabble.com/Creating-vegetation-distance-groups-from-one-column-tp4644970p4645230.html
Sent from the R help mailing list archive at Nabble.com.
#
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.
#
Hello,

My example with 'x' was just that, an example. Inline.

Em 06-10-2012 00:03, Jhope escreveu:
Use

cut(data.to.analize$VegIndex, breaks = seq(0, 35, 5))
paste0 was introduced with R 2.15.0, update your version of R and in the 
mean time use

paste(...etc..., sep = "")


Rui Barradas