1

I have a data frame and I tried to use a for loop to create a new column and fill it with gram negative and gram positive by matching species in column 2. I know how to do with dplyr, but I want to use loop to improve my understanding for coding. here is my code:

species_abundance<-data.frame(
ID=c(1,2,3,4,5),
Genus = c("Sphingopyxis marina","Loktanella salsilacus",
          "Paracoccus chinensis","Bacillus","Streptomyces"))
 

and output is like

species_abundance<-data.frame(
ID=c(1,2,3,4,5),
Genus = c("Sphingopyxis marina","Loktanella salsilacus",
          "Paracoccus chinensis","Bacillus","Streptomyces")),
Grams_staining=c("grams_negative, grams_negative, grams_negative,grams_positive, grams_positive)

I tried with this code, but I didnt get my expected results, kindly help me in what all way we can loop to get results, so I can improve my learning.

for(i in 1:nrow(species_abundance)) {# for-loop over columns
  if (species_abundance[i,2] == "Sphingopyxis marina"&&
      species_abundance[i,2] == "Loktanella salsilacus"&&
      species_abundance[i,2] == "Paracoccus chinensis"){
      print("grams_negative")
  }
  
  else {
    species_abundance[i,2] == "Bacillus"{
     print("grams_positive")
  }  
}
5
  • your if .. && .. && construct requires that something matches ALL of those, not one of those. I would try if(species_abundance$Genus[i] %in% c("Sphingopyxis marina", "Loktanella salsilacus", "Paracoccus chinensis")). You also need a way to allocate the result to the data frame (e.g. species_abundance$Grams_staining[i] <- ) Commented Jan 12, 2023 at 12:47
  • @PaulStaffordAllen Thank you, its working, can we use or | construct insted using %in% or ==, asking just to know how else I can work Commented Jan 12, 2023 at 13:10
  • @PaulStaffordAllen, Also, I got this warning, Warning message: Unknown or uninitialised column: Grams_staining. Kindly explain the solution Thanks Commented Jan 12, 2023 at 13:16
  • @PaulStaffordAllen, Thank you, is there any way to change the position of column of Gramstaining, while looping, I mean if I want to put in first position than how will be the loop structure. Kindly reply, Thanks in advance Commented Jan 12, 2023 at 15:53
  • The order of columns doesn't matter really until you need to display the data, but in general you can do something like: mydata <- mydata[,c(3,1,2)] to select which columns to keep and in which order. Commented Jan 13, 2023 at 7:58

1 Answer 1

1

I would suggest this approach. First, define which genus/species should be identified as gram positive and gram negative, then use %in% in your for loop to print:

Define gram positive/negative (based on your loop in your question, can be anything).

gramneg <- c("Sphingopyxis marina", "Loktanella salsilacus", "Paracoccus chinensis")
grampos <- c("Bacillus", "some_other_genus")

For loop

for(i in seq_along(species_abundance$Genus)){
  if(species_abundance$Genus[i] %in% gramneg){
    print("gram_negative")
  } else {
    if(species_abundance$Genus[i] %in% grampos){
    print("gram_positive")}
     else {
    print("Not in in gram positive or gram negative list")
     }
    }
}

Output:

# [1] "gram_negative"
# [1] "gram_negative"
# [1] "gram_negative"
# [1] "gram_positive"
# [1] "Not in in gram positive or gram negative list"

Data

species_abundance <- data.frame(ID = c(1,2,3,4,5),
                                Genus = c("Sphingopyxis marina","Loktanella salsilacus",
                                          "Paracoccus chinensis","Bacillus","Streptomyces"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.