mercredi 21 mars 2018

False from dataframe evaluation in R

Here is my example

my_df <- data.frame(col_1 = c(1,2,4), col_2 = c('A', 'B', 'C'))

I would like to create if statement based on the following expression

my_df[my_df$col_1==5,'col_2']

This value currently returns:

factor(0)
Levels: A B C

How do I compare it

my_df[my_df$col_1==5,'col_2'] == 0?
is.na(my_df[my_df$col_1==5,'col_2']) ?

nothing works

This works, but a bit clumsy

NROW(my_df[my_df$col_1==5,'col_2'])==0

Aucun commentaire:

Enregistrer un commentaire