vendredi 30 mars 2018

R:How to repeat for loop from row 1 to last row in dataframe?

    N<-nrow(MedFIX)
      for(i in 1:N)
      {
          med<-MedFIX[i,]

          #These series of code does not do what I need the loop to do

          medidstrings0<-substring(med$MED_ID, 4,10) #extract digits after 
          "MED" for first medication in row
          medidstrings1<-substring(med$MED_IDj, 4,10) #extract digits after 
          "MED" for second medication in row

          if (medidstrings0 < medidstrings1)
          {dftest<-paste(medidstrings0,medidstrings1,sep = "-") #combine 
          strings with "-" in between
          }
          else (medidstrings0 > medidstrings1)
          {dftest<-paste(medidstrings1,medidstrings0,sep = "-")}

          combomed<-rbind(dftest,combomed) #combine all rows in dftest 
          combomed<-na.omit(combomed) #remove NAs
    }

combosample$CT_ID<-combomed #paste CT_IDs in data in respective column

I am assigning number codes to a specific code, but I would like to do this repeatedly for each row of my data set to the end. Is there a short way similar to this to do this? I also would like to paste this column of codes back into the main data frame but the code I have does not do that either.

Aucun commentaire:

Enregistrer un commentaire