r - Kaplan Meier iteratively -


i have data frame (df) looks this:

  col1        class1   class2    class3   t_rfs(days)  e_rfs sample_name1             b                750            1 sample_name2      b        b                458            0 sample_name3      b        b                1820           0 sample_name4      b                b        1023           0 sample_name5                     b        803            0 sample_name6             b                1857           1 sample_name7                     b        850            1 

t_rfs_years = time relapse free survival
e_rfs = event relapse free survival
nb: table example respect real case.

i apply kaplan meier each class. code wrote following:

library(survival) df <- read.delim("df.txt", header = t) pdf("all_km_plotted_together.pdf", paper = "usr") par(mfrow=c(2,2)) surd <- survdiff(surv(df$t_rfs, df$e_rfs == 1) ~ df$class1)  plot(survfit(surv(df$t_rfs, df$e_rfs == 1) ~ df$class1), col = c("red", "blue")) surd <- survdiff(surv(df$t_rfs, df$e_rfs == 1) ~ df$class2)  plot(survfit(surv(df$t_rfs, df$e_rfs == 1) ~ df$class2), col = c("red", "blue")) surd <- survdiff(surv(df$t_rfs, df$e_rfs == 1) ~ df$class3)  plot(survfit(surv(df$t_rfs, df$e_rfs == 1) ~ df$class3), col = c("red", "blue")) dev.off() 

i write loop takes iteratively each "class" @ time , run script instead of write every time pieces of repeated code each "class". can me please?

best f.

there 2 ways extract column data frame: $ , [[. below few examples same thing:

  • df$class1
  • df[["class1"]]
  • df[[1]]

so using last method above in combination for loop accomplishes want.

for(i in 1:3){     plot(survfit(surv(df$t_rfs, df$e_rfs == 1) ~ df[[i]]), col = c("red", "blue")) } 

this pretty basic recommend reading introductory r book going. save lot frustration , quicker asking on so.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -