samedi 8 septembre 2018

R - Conditional labels in data frames based on index values, without loops

R question! I am trying to label rows in a data frame based on their indices. For example, I want to label all rows with an index < x "A", all rows with an index >= x and < y "B", etc. These index values are NOT constant, but will change from file to file that I am processing.

I have a huge data frame with thousands of rows, so I am trying to avoid lots of loops... I made a column of index values in my data frame, and I want to compare those values to a vector of values in order to strategically label rows in my data frame. This is the structure I want to avoid (I tried it and it works, it just takes forever):

for (i in 1:nrow(dataframe)){
  for (j in 1:length(index_values)){
    if (dataframe$Index[i]>index_values[j] & dataframe$Index[i]<index_values[j+1]{
      dataframe$Label[i]='blah'
    }
  }
}

I tried using "ifelse", but it doesn't seem to work in a loop? This is what I tried, but the "Label" field was not populated with anything.

for (i in 1:length(index_values)){
dataframe$Label <- ifelse((dataframe$Index>index_value[i] & dataframe$Index<index_value[i+1]),'blah',0) 
}

Does anyone have any suggestions for how to get "ifelse" to work in this context, or any other more efficient ways to approach this? Thanks!!

Aucun commentaire:

Enregistrer un commentaire