[R] Conditional data frame manipulation

Johannes Graumann johannes_graumann at web.de
Sat Feb 17 17:34:20 CET 2007


Hi all,

My current project brought forth the snippet below, which modifies in each
row of a data frame a certain field depending on another field in the same
row. Dealing with data of some 30000 entries this works, but is horribly
slow. Can anyone show this newbie how to do this properly (faster ;0)?

for (i in 1:nrow(dataframe)){
  if (any(grep('^yes$',dataframe[i,][['Field1']]))){
    dataframe[i,]['Field1'] <- dataframe[i,]['Field2']
  } else {
    dataframe[i,]['Field1'] <- NA
  }
}

Thanks for your insights, Joh



More information about the R-help mailing list