Search code examples
rlistfunctionvectordimension

Unexpected error message while extracting vectors from a list


I want to extract vectors from a list of text files.

First define the correct "working directory" and then I generate a list that contains a the test files.

file.list <- list.files(pattern="*.txt", full.names=T)

Afterwards I format the data the right way.

datalist = lapply(file.list, FUN=read.table, header = F, sep = "\t", skip = 2)

And eventually I define the vectors which should be extracted.

cmbn = expand.grid(1:641, 1:977)
flen = length(datalist)
lapply(1:(nrow(cmbn)),function(t,lst,cmbn){
  return(sapply(1:flen,function(i,t1,lst1,cmbn1){
    return(lst1[[i]][cmbn1$Var1[t1],cmbn1$Var2[t1]])},t,lst,cmbn))}
  ,datalist,cmbn)

In the end I got as an output all the vectors. But how can I store them in a clever way? I want to be able to access the vectors individually.


Solution

  • The error message "incorrect number of dimensions" indicates that lst1[[i]] does not possess two dimensions. When dim returns NULL, it indicates that lst1[[i]] is not even a matrix nor array.

    Try using str(lst1[[i]]) to take a look at the structure of the element. Is it vector? Is it something else? Or is it simply missing (NULL)?

    When you know what is wrong, you can fix it. It could be reading an empty file, or that you are reading an index outside of the bounds of the list due to ??