Insert a value in a different column when checked against multiple conditions from other columns?

Viewed 36

I have 2 columns I need to run a few conditions on in order to classify samples as a status. I created an empty status column. Then ran the few conditions of the two columns (pp & ff) to try insert the corresponding value in the new column.

When I run this I get no error but only the first line works:

df$status[which(df$ff >7 & df$pp >9)] <- "type_a"

The type_a values are placed correctly in the status column but all other rows remain as NA. I have 2 or 3 odd NA values sparsely in a few rows of the two columns, but that is it. I don't think that would stop it from working?

This is what I'm currently working with:

#Adding a column called status
df$status <- NA

df$status[which(df$ff >7 & df$pp >9)] <- "type_a"
df$status[which(df$ff <4 & df$pp <4)] <- "type_b"
df$status[which(df$ff <=4 & df$ff >=7 & df$pp >=8.5 & df$pp <=9)] <- "type_c"
1 Answers

There's a logical error in your type_c, this will never be TRUE:

df$ff <=4 & df$ff >=7

A number cannot simultanously be smaller than 4 and larger than 7.

You likely meant to capture the inclusive range [4,7], and you can probably get that right on your own.

Related