I'm attempting to use a vector or a dataframe to classify the size of fish above or below a threshold size. If I want to use a standard threshold size, say "30" for all fish, I have no problems.
sp_data <- data.frame(
X1 = c("fish1","fish1","fish2","fish2","fish3"),
X2 = c(20,30,32,21,50)
)
sp_data$X3 <-ifelse(sp_data$X2>=30 , "above", "below")
The above code works as intended however what I really want is to use a unique threshold for each fish. So I created this dataframe where I can list the fish and it's corresponding size threshold.
size_data <-data.frame(
S1 = c("fish1", "fish2", "fish3"),
S2 = c(25, 26, 30)
)
spdata$X4 <- ifelse(spdata$X1 == sizedata$S1 &
sizedata$S2 >= sp_data$X2, above, below)
This doesn't work, I think because its looking at all of spdata$X1 sizedata$S1 instead of row by row. Perhaps ifelse is not the best way to solve this problem but its the closest I've found so far. I'm thinking I need a loop or an apply to make this work but I'm not sure where to go from here.