kjm123
kjm123

Reputation: 1

Adding a string to multiple existing columns if it exists in another column

I have separated out a GTDBtk taxonomy column into separate columns ("Domain", "Phylum", "Class", "Order", "family", "genus", "species") but I want to add "unclassified" to every column when it appears. As of now, "unclassified" only appears in "Domain". I have tried to use the code below but I don't think the ifelse command can apply to a list as I get an error that the list isn't logical. I am working in R.

class_columns = c("Domain", "Phylum", "Class", "Order", "family", "genus", "species")
table %>% ifelse(table$Domain == "Unclassified", table[,class_columns] <- "Unclassified" )

Upvotes: 0

Views: 17

Answers (1)

kjm123
kjm123

Reputation: 1

Sorted it out. Column 33 is the "Domain" column for anyone wondering.

class_columns = c("Phylum", "Class", "Order", "family", "genus", "species")

for(i in 1:nrow(table2)){
  if(table2[i,33]=='Unclassified'){
    # paste into position i of vector m
    for(classcols in class_columns){
      table2[i,classcols] <- "Unclassified"
    }
  }  else {
    NULL
  }
}

Upvotes: 0

Related Questions