0

I have a dataframe like this:

df1 <- data.frame(
  E11 = c(2, 4, 6, 8), 
  E14 = c(3, 5, 7, 9), 
  E18 = c(3, 3, 3, 3), 
  Adult = c(9, 7, 5, 3), 
  E14vsE11 = c(1, 1, 1, 1), 
  E18vsE14 = c(0, -2, -4, -6), 
  AdultvsE18 = c(6, 4, 2, 0)), 
  row.names = c("geneA", "geneB", "geneC", "geneD")
)

I want to add cluster factors to them based on the comparison values and my code is:

filtera <- df1$E14vsE11 >0 & df1$E18vsE14 >0 & df1$AdultvsE18 >0
filterb <- df1$E14vsE11 <0 & df1$E18vsE14 <0 & df1$AdultvsE18 <0
filterc <- df1$E14vsE11 ==0 & df1$E18vsE14 ==0 & df1$AdultvsE18 ==0
for (i in (1:nrow(df1))){
  if (isTRUE(filtera)){
    df1$cluster <- "cluster1"
  } else if (isTRUE(filterb)){ 
    df1$cluster <- "cluster2"
  } else if (isTRUE(filterc)){
    df1$cluster <- "cluster3"
  } else {
    df1$cluster <- "cluster4"
  }}

But in the output all were filled with "cluster4". How should I modify the code to get it correct

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Kyle
  • 49
  • 7
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Use a reproducible format like `dput()` rather than `str()` – MrFlick Jul 09 '20 at 22:26
  • Thank you. I have change the dataframe to a reproducible one. – Kyle Jul 09 '20 at 23:39
  • So in your sample data, every row is cluster 4? – MrFlick Jul 09 '20 at 23:52
  • right. I tried df1$cluster[i] < "cluster1", but still all is cluster4. – Kyle Jul 09 '20 at 23:55

1 Answers1

0

Note that if() is not a vectorized function. The isTrue is kind of hiding the fact that you are passing in a vector. When you do an assignment like df1$cluster <- "cluster4", R has no idea what row you are trying to update so it does them all.

A more modern approach would be to use a bit fo dplyr and a case_when statement to do the reclassification.

library(dplyr)
df1 %>% 
  mutate(cluster = case_when(
    E14vsE11 >0 & E18vsE14 >0 & AdultvsE18 >0 ~ "cluster1",
    E14vsE11 <0 & E18vsE14 <0 & AdultvsE18 <0 ~ "cluster2",
    E14vsE11 ==0 & E18vsE14 ==0 & AdultvsE18 ==0 ~ "cluster3",
    TRUE ~ "cluster4"
  ))
MrFlick
  • 195,160
  • 17
  • 277
  • 295