So, I have a list of data frames, named as "D1.txt", "D2.txt"................"D45.txt".
Each of the file contains
2 columns and each file has 1000's of rows`.
I am trying to add a new column to each of the data frame in the list, by the following code, but it shows the error as
incorrect number of subscripts on matrix
.
The code that I am using is,
L <- lapply(seq_along(L), function(i) {
L[[i]][, paste0('DF', i)] <- 1
L[[i]]
})
where L
is the name of the list containing data frames.
Why is this error coming? Thanks! :)
Edit: A reproducible example:
# Create dummy data
L <- replicate(5, expand.grid(1:10, 1:10)[sample(100, 10), ], simplify=FALSE)
# Add a column to each data.frame in L.
# This will indicate presence of the pair when we merge.
L <- lapply(seq_along(L), function(i) {
L[[i]][, paste0('DF', i)] <- 1
L[[i]]
})