I would like to calculate a ratio (A / B = C). Then I want to assign a label for ratio. If the ratio equals 0 label gets "0". If value less than 1 label gets "1". Lastly ratio is greater than 1 label gets "2".
Here is the example data:
A B C
1 0 171.06 0.0000000
2 1 2 0.5
3 0 120.84 360.00000
4 0 308.07 0.0000000
5 0 243.06 0.0000000
6 0 876.015 0.0000000
Here is the function with loop:
targets <- function(data)
{
label_df <- data[,c("A", "B")]
label_df[is.na(label_df)] <- 0
label_df["C"] <- (label_df["A"] / label_df["B"])
for(i in 1:nrow(label_df))
{
if(label_df$C == 0){label_df[i,"C"] <- 0}
else if(label_df$C < 1){label_df[i,"C"] <- 1}
else {label_df[i,"C"] <- 2}
}
return(as.data.frame(label_df))
}
And here is the result. It shows all labels 0.
lab <- target(data)
head(lab)
A B C label
1 0 171.06 0.0000000 0
2 1 2 0.5 0
3 0 120.84 360.00000 0
4 0 308.07 0.0000000 0
5 0 243.06 0.0000000 0
6 0 876.015 0.0000000 0
I check the result with table() function and it just shows all data for label 0. There are no 1 or 2 labels. I couldn't figure out where the problem is. Any idea?