Hello!
I have a dataset indices
where each observation contains a character rating for 4 different variables. I want to assign the character rating in each column a numeric value using an ifelse
statement and then sum the result in a new column.
To clarify, each row has four indexes with a "Good/Fair/Poor/Missing" rating, and I want to assign a 1 for each Good and a 0 for everything else. Then, I want to add up the result in a column called indices$Total
This is what I have tried:
for(i in 1:ncol(indices[,5:8])){
indices$Total[i] <- sum(ifelse(indices$i=="Good", 1,0))
}
But, of course, it didn't work because I'm sure I'm not writing the appropriate syntax, or I'm not defining i
correctly or something. Could someone help me achieve this? Here is a subset of my data:
indices <- structure(list(UID = c("155230", "155231", "155232", "155233",
"155233", "155233"), SITE_ID = c("LHLEC-012", "LHLEC-015", "LHLEC-011",
"LHLEC-003", "LHLEC-003", "LHLEC-003"), LATITUDE = c(42.0855,
42.1436, 42.1203, 42.1725, 42.1725, 42.1725), LONGITUDE = c(-83.1216,
-83.1177, -83.1328, -83.1316, -83.1316, -83.1316), Water.Quality.Index = c("Fair",
"Poor", "Fair", "Good", "Good", "Good"), SedChemindex = c("Fair",
"Good", "Fair", "Good", "Good", "Good"), SEDTOX_INDEX = c("Fair",
"Good", "Fair", "Good", "Good", "Fair"), OTI.Classification = c("Missing",
"Fair", "Poor", "Fair", "Fair", "Fair")), row.names = c(NA, 6L
), class = "data.frame")
Thanks so much!