loops - Iterative grep with r -
ok thank guy, start again question:
this df
df = read.table(text = ' replicate size fh ms03a_t0_r1 397.51 1099 ms03a_t0_r1 695.46 8 ms03a_t0_r1 708.76 1409 ms03a_t0_r1 1203.98 102 ms03a_t0_r2 397.52 749 ms03a_t0_r2 493.97 23 ms03a_t0_r2 538.43 12 ms03a_t0_r3 397.49 638 ms03a_t0_r3 399.84 9 ms03a_t0_r3 404.95 33 ms03a_t0_r3 406.85 40 ', header = t) rn <- as.numeric(length(levels(ol$replicate))) # calculate number of samples
from have 3 new dataset each 1 contain rows *_r1 value of "replicate" variable, rows *_r2 , rows *_r3.
i thought did whit these commands:
for (i in 1:rn){ x <- df[as.character(sub('.*_r', '', as.character(replicate))) %in% i]; outfile <- paste("rep_",i,"_edited.txt",sep="") write.table(x,quote=false,sep=", ",outfile) }
but able .txt outputs , not df objects in r. in way have import them again in r move on next step of "script", , have no idea how set r import them automatically
my guess want this:
df = read.table(text = ' replicate size fh ms03a_t0_r1 397.51 1099 ms03a_t0_r1 695.46 8 ms03a_t0_r1 708.76 1409 ms03a_t0_r1 1203.98 102 ms03a_t0_r2 397.52 749 ms03a_t0_r2 493.97 23 ms03a_t0_r2 538.43 12 ms03a_t0_r3 397.49 638 ms03a_t0_r3 399.84 9 ms03a_t0_r3 404.95 33 ms03a_t0_r3 406.85 40 ', header = t) library(data.table) dt = data.table(df) special.ids = c(1,3) dt[as.numeric(sub('.*_r', '', as.character(replicate))) %in% special.ids] # replicate size fh #1: ms03a_t0_r1 397.51 1099 #2: ms03a_t0_r1 695.46 8 #3: ms03a_t0_r1 708.76 1409 #4: ms03a_t0_r1 1203.98 102 #5: ms03a_t0_r3 397.49 638 #6: ms03a_t0_r3 399.84 9 #7: ms03a_t0_r3 404.95 33 #8: ms03a_t0_r3 406.85 40
note, as.character
needed because read.table
converts strings factors default. may not need actual data if it's strings.
upon second reading, maybe want this?
split(df, sub('.*_r', '', as.character(df$replicate)))
op - need fix question.
Comments
Post a Comment