Categorizing a numerical value, Depth.


At each depth, there are a recorded number of infected individuals. I want to make depth categories. And I'm trying to organize the number of infected that fall under each category. Hence why I want to make depth categories, ranges for example, of 0-7 meters, 8-10 meters, 11-15 meters, and 16-26 meters.

CDD_A <- read.csv("Coral Disease Data")
Infected <- subset(CDD_A,select=Sum.Infected)
Depth<-subset(CDD_A, select = Depth)
Lat <- subset(CDD_A, select = LAT)
Long <- subset(CDD_A, select = LONG)
ID_A = cbind(Lat, Long, Depth, Infected)
View(ID_A)

Here is one way to bind numeric data using the cut() function.

DF <- data.frame(Depth = sample(0:26))
DF
#>    Depth
#> 1     17
#> 2     13
#> 3     14
#> 4     22
#> 5     16
#> 6     10
#> 7     11
#> 8     15
#> 9     18
#> 10     3
#> 11     6
#> 12    26
#> 13    20
#> 14     2
#> 15    21
#> 16     8
#> 17     1
#> 18    19
#> 19     9
#> 20    25
#> 21     4
#> 22    23
#> 23     7
#> 24     5
#> 25     0
#> 26    24
#> 27    12
DF$Bins <- cut(DF$Depth, breaks = c(0, 7, 10, 15, 26), include.lowest = TRUE)
DF
#>    Depth    Bins
#> 1     17 (15,26]
#> 2     13 (10,15]
#> 3     14 (10,15]
#> 4     22 (15,26]
#> 5     16 (15,26]
#> 6     10  (7,10]
#> 7     11 (10,15]
#> 8     15 (10,15]
#> 9     18 (15,26]
#> 10     3   [0,7]
#> 11     6   [0,7]
#> 12    26 (15,26]
#> 13    20 (15,26]
#> 14     2   [0,7]
#> 15    21 (15,26]
#> 16     8  (7,10]
#> 17     1   [0,7]
#> 18    19 (15,26]
#> 19     9  (7,10]
#> 20    25 (15,26]
#> 21     4   [0,7]
#> 22    23 (15,26]
#> 23     7   [0,7]
#> 24     5   [0,7]
#> 25     0   [0,7]
#> 26    24 (15,26]
#> 27    12 (10,15]

Created on 2022-04-26 by the reprex package (v0.2.1)

I'm puzzled by the code you showed. You seem to be simply rebuilding your data set with modified column names. Is there a goal other than that?

(The reason I am making a new data frame is that the other is filled with other variables. This is just for me to be more organized and make less mistakes.)

CDD_A <- read.csv("Coral Disease Data by R.V.Woesik&ChelseaV2.csv") # importing a csv file and assigning it a name.
Infected <- subset(CDD_A,select=Sum.Infected) #Subsetting sum infected values from data frame.
Depth<-subset(CDD_A, select = Depth) #subsetting depth values from data frame
Lat <- subset(CDD_A, select = LAT) #subsetting latitude values from data frame
Long <- subset(CDD_A, select = LONG) #Subsetting longitude from data frame.
ID_A = cbind(Lat, Long, Depth, Infected) #configuring all variables that were subsetted into a new data frame.

I see the confusion now. At each depth, there are a recorded number of infected individuals. I want to make depth categories. And im trying to organize the number of infected that fall under each category. Hence why I want to make depth categories, ranges for example, of 0-7 meters, 8-10 meters, 11-15 meters, and 16-26 meters.

So this code gets you what you need, right?

ID_A$Bins <- cut(ID_A$Depth, breaks = c(0, 7, 10, 15, 26), include.lowest = TRUE)

I am doing this so I can run a glm and see which depth is most influential in the number of infected.

Please ask an explicit question. The last code I provided adds a column to your data that assigns every depth to a category. What more do you need?

THe code resolved the issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.