'creating a for loop to determine habitat quality in different allotments

DataFrame - Example

enter image description here

Basically, I am trying to figure out the percentage of land that can contain sage (sageStat = yes or no). The tricky part is there are various values for a single allotment (name = ironcanyon-079, ironcanyon-081, ect ) and many allotments (lowerhorsefly-075, adobe-001,ect). What I need to do is find the % of land containing sage per value of the allotment (for example - ironcanyon-079 has 28% sage, ironcanyon-080 has 14%) then get the summary stats across all the values of the individual allotment ( the mean % cover of sage in a given allotment is blank)

The tricky part is I want to do this for many allotments contained in the same df. I assume that a for loop can be implemented but I am still struggling with creating this loop.

I am a noob at Rstudio and hope I asked the question in a way that is understandable and any help is greatly appreciated. The code below is how I set up my data frame.

fold <- "S:\\Biological_Resources\\Vegetation\\AIM\\UFO_RMP_Compliance_project\\2021\\Starting_to_finalize/Iron_Canyon"

setwd(fold)


file_names <- dir(fold) 
df <- do.call(rbind, lapply(file_names, function(x) cbind(read.csv(x), name=strsplit(x,'\\.') [[1]][1]))) 

as.factor(df$VALUE)


test <- df %>% 
 select("EVT_NAME", "EVT_LF", "EVT_PHYS", "name", "COUNT", "VALUE") %>% 
mutate(acres = COUNT*900/4047) %>% 
mutate(acres = round(acres, 2)) 



test1 <- test


SageVal <- c("7051", "7064")

test1$SageStat <- ifelse(test1$VALUE %in% SageVal, "Yes", "no")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source