I am pretty new to R and really finding it powerful. I am trying to create a dataframe with multiple levels. I have 3 groups (of subjects) called "group1", "group2" and "group3" who have each 19 tests (names do not matter). In each of these 19 tests there are 3 components called b1, b2 and b3 out of 7 components that I am interested in.
What I tried so far: For each patient, the 19 tests form 19 columns with 7 rows (components) hence, the components I am interested in are 5,6,7 for each of the 19 tests:
sapply(x, '[', XXX, j)
My final code is:
for (j in 1:19) {
x = lapply(c('group1', 'group2', 'group3'), function(i) {
filenames = dir(file.path('c:/19/7', i),
pattern = 'sRL.txt', full.names = T)
x = lapply(filenames, read.table, sep = '')
bb1 = sapply(x, '[', 5, j)
bb2 = sapply(x, '[', 6, j)
gg = sapply(x, '[', 7, j)
pcode = as.numeric(gsub('_.*', '', basename(filenames)))
data.frame(group = i, tests= j, pcode, b1= bb1, b2= bb2, g = gg)
})
}
Could you please advise how to create a nice dataframe that combine the components 5,6,7 of the 19 tests for each subject of each group?