Incorrect Number of Subscripts on Matrix in R

incorrect number of subscripts on matrix in R

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.

Incorrect number of subscripts on matrix in R using read_table

Regarding the issue, it may be because we used read_table which returns a tibble and tibble doesn't drop dimensions with [. Instead, we need [[.

collector <- function(min, max){
collected <- matrix(nrow = 601, ncol = max - min + 2)
names = c()
for (i in 1:(max-min+1)){
names[i] = paste0("D:/CHY 498/UV-Vis/22822/BH4_3/12321A",(i+min-1),".RLS")
}
for (j in 1:(max-min+1)){
e <- read_table(names[j], col_names=FALSE)
collected[,j+1] = e[[2]]## change
}

}

Instead of initializing with a NULL vector, we can create a vector of certain length and then assign with [i]. Other than that the code works with a dummy data

collector <- function(min, max){
i1 <- max - min + 1
collected <- matrix(nrow = 601, ncol = max - min + 2)
names = character(i1)

for (i in 1:i1){
names[i] = paste0("D:/CHY 498/UV-Vis/22822/BH4_3/12321A",(i+min-1),".RLS")
}
for (j in 1:i1){
e <- cbind(NA, 1:601) # created dummy data
collected[,j+1] = e[,2]
}
collected


}
test <- collector(15, 23)

-testing

test <- collector(15, 23)
> head(test)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] NA 1 1 1 1 1 1 1 1 1
[2,] NA 2 2 2 2 2 2 2 2 2
[3,] NA 3 3 3 3 3 3 3 3 3
[4,] NA 4 4 4 4 4 4 4 4 4
[5,] NA 5 5 5 5 5 5 5 5 5
[6,] NA 6 6 6 6 6 6 6 6 6

NOTE: The last part of reading the data couldn't be tested. It may be that some of the links doesn't have data and thus couldn't be read. Also, paste is vectorized, so the first loop is not really needed

R error incorrect number of subscripts on matrix

You would need to preallocate your output vector. However, it's easier/cleaner to use mapply. If you pass it two vectors (including lists) it iterates simultaneously over both and applies the function to the paired elements. Thus we only need to split the matrices into lists.

A <- matrix(1:9, 3)
B <- A * 3 + 2 + 1/A

t(mapply(function(a, b) {
fit <- lm(b ~ a)
residuals(fit)
}, split(A, letters[1:3]), split(B, letters[1:3])))
# 1 2 3
#a 0.10714286 -0.21428571 0.10714286
#b 0.03750000 -0.07500000 0.03750000
#c 0.01851852 -0.03703704 0.01851852

residuals(lm(B[1,] ~ A[1,]))
# 1 2 3
#0.1071429 -0.2142857 0.1071429

Here is a for loop that does the same:

result <- matrix(NA, nrow = nrow(A), ncol = ncol(A))
for (i in seq_len(nrow(A))) {
result[i,] <- residuals(lm(B[i,] ~ A[i,]))
}
# [,1] [,2] [,3]
#[1,] 0.10714286 -0.21428571 0.10714286
#[2,] 0.03750000 -0.07500000 0.03750000
#[3,] 0.01851852 -0.03703704 0.01851852

I get error incorrect number of subscripts on matrix

The boot() function requires its statistic argument to be a function that returns a vector. Your sample_mean function returns a list of class "htest" because that is the output of perm.t.test(). Based on the function name, I assume you want the estimate of the means of the differences from that test.

If you change your function to look as follows, the code works.

sample_mean <- function(mydata, indices) {

sam=mydata[indices,1]

sam1=mydata[indices,2]


bar = perm.t.test(sam,sam1,paired=TRUE,nperm=500)

return(bar$estimate)
}

If you want a different output from perm.t.test(), then swap $estimate for something else like $statistic or $p.value.

Here is an example of boot with R=10 (to be manageable):

results = boot(mydata,statistic=sample_mean,R=10) 
print(results)

ORDINARY NONPARAMETRIC BOOTSTRAP

Call:
boot(data = mydata, statistic = sample_mean, R = 10)

Bootstrap Statistics :
original bias std. error
t1* 0.5172414 0.1206897 0.720067

Error in incorrect number of subscripts on matrix

Solved it.

Had to define the T ProbT Star matrices.

T <{ matrix(NA, nrow = MaxK + 1, ncol = 1)
ProbT <{ matrix(NA, nrow = MaxK + 1, ncol = 1)
Star <{ matrix(NA, nrow = M, ncol = B)


Related Topics



Leave a reply



Submit