Sample data:
df <- data.frame(apples = c(1, 5, 3),
oranges = c(5, 3, 5))
Problem:
for(i in names(df)){
if(sum(df$i == 5) == 1){
print(paste("There is only 1 occurance of 5 fruit for", i))
} else {
print(paste("There is more than 1 occurance of 5 fruit for", i))
}
}
this gives me
[1] "There is more than 1 occurance of 5 fruit for apples"
[1] "There is more than 1 occurance of 5 fruit for oranges"
however…
> sum(df$apples == 5)
[1] 1
> sum(df$oranges == 5)
[1] 2
My expected output:
[1] "There is only 1 occurance of 5 fruit for apples"
[1] "There is more than 1 occurance of 5 fruit for oranges"
I suspect it’s some sort of syntax issue, or am I missing something more obvious?
>Solution :
You need to use df[[i]] not df$i in your loop, otherwise it is finding variable i in the dataframe. df$i is NULL. sum(NULL == 5) is 0. You always do that else bit.