Search code examples
rloopsassign

Loop on colum names with R


I am working with a dataframe (called "dataEPM"), with several columns among which some do have incremented columns names (ke_1, ke_2, .., ke_8). For these columns, I want to obtain the number of rows complying with a condition (==3), and the result in a dataframe output. So I write a loop on the column names. Here the code:

output_nb <- matrix(ncol=2, nrow=8)

for (i in 1:8){
  text <- paste("ke_",i, sep="")
  nb_i <- nrow(dataEPM[dataEPM$text == "3",])
  print(nrow(dataEPM[dataEPM$text == "3",]))
  output_nb[i,1] <- i
  output_nb[i,2] <- nb_i
}

output_nb <- data.frame(output_nb)

With the print command, I can see that nrow(dataEPM[dataEPM$text == "3",]) is always equal to 0, nut when I replace the column name (e.g. nrow(dataEPM[dataEPM$ke_1 == "3",])) it is not. So I assume that this way of writing the column name is not accepted here.

Coudl you please tell me how to fix that? Thanks in advance.


Solution

  • The following should work :

    for (i in 1:8){
      text_ <- paste("ke_",i, sep="")
      nb_i <- nrow(dataEPM[dataEPM[,text_] == "3",])
      print(nrow(dataEPM[dataEPM[,text_] == "3",]))
    }
    

    Just use plain boolean filters. Please tell me if it works!