I have some data that I read in and modify in R. For a minimal, reproducible example (reprex) I want to give the data as an "in R" representation to also communicate the data structure:
The code to read in the data:
paths <- sprintf("filenames%02d.out", 1:26)
interim <- lapply(paths, read.table, header=FALSE, sep="\t", dec=".", na.strings="NA")
new_col_name <- c("Pos", "LRTD")
out <- lapply(interim, setNames, nm = new_col_name)
Now, lapply(out, head) allows us to see R's internal representation of the data:
[[1]]
Pos LRTD
1 0 0
2 70557 0
3 104076 0
4 163349 0
5 258229 0
6 356613 0
[[2]]
Pos LRTD
1 0 0
2 171603 0
3 268756 0
4 456513 0
5 594904 0
6 663581 0
[[3]]
Pos LRTD
1 0 0.000
2 171960 0.370
3 217096 0.358
4 254484 0.338
5 320866 0.366
6 432642 0.382
{...}
[[26]]
Pos LRTD
1 0 0
2 185161 0
3 234971 0
4 273218 0
5 319689 0
6 379800 0
So it is a list of data.frames with 26 elements. Here, I want to call the numbers that we can see above in square brackets, so the numbers [[1]], [[2]], [[3]] and so forth till [[26]], as "element descriptors".
Now what I would like to do is append a third column to all the data.frames in the list where the column contains computationally read out structural information of the data.frames.
In detail, I would like to add the element descriptors of the given data.frames to their respective data.frame. That in mind, the result should look like this:
[[1]]
Pos LRTD Chr
1 0 0 1
2 70557 0 1
[[2]]
Pos LRTD Chr
1 0 0 2
2 171603 0 2
[[3]]
Pos LRTD Chr
1 0 0.000 3
2 171960 0.370 3
{...}
[[26]]
Pos LRTD Chr
1 0 0 26
2 185161 0 26
Since I am well aware of this question, my current solution is pseudocode:
lapply(out, function(x) { x$Chr <- rep("element descriptor","lenght of list");return(x)})
I know that I can get the length of the respective data.frame with rapply(out, length), but so far I don't get rapply to work within my lapply command from above.
Also, how to reference the element descriptor in code?