Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

incorrect number of subscripts on matrix in R

So, I have a list of data frames, named as "D1.txt", "D2.txt"................"D45.txt". Each of the file contains2 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]] 
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think that when you read in your "D1.txt", "D2.txt"................"D45.txt" files they get converted to matrices and that is why your particular for loop fails. I'll use your example:

L <- replicate(5, expand.grid(1:10, 1:10)[sample(100, 10), ], simplify=FALSE)

If we use class(L[[1]]) to pick out the first element of the list it will output [1] "data.frame" if you use your for loop on this list that only contains data.frames you will see no error and it will give you what you want. If however we transform all elements in the list to matrices:

for(i in seq_along(L)){
     L[[i]] <- as.matrix(L[[i]])
}

and check with class(L[[1]]) it will output [1] "matrix". If you use your for loop now on L which now contains matrices we will get:

> L <- lapply(seq_along(L), function(i) { 
+   L[[i]][, paste0('DF', i)] <-  1
+     L[[i]] 
+     })
Error in `[<-`(`*tmp*`, , paste0("DF", i), value = 1) : 
  subscript out of bounds

Hence, you can either make sure that when you read in your files they are coerced to data.frames, use @Richards solution, or read in your files and coerce them to data.frames via

 for(i in seq_along(L)){
    L[[i]] <- as.data.frame(L[[i]])
}

and use your for loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...