What could be the cause of the error below?

This program works because I made the varibles inisde lapply global by using the <<- operator. However, it does not work with the real files in the real program. These are .tsv files whith named columns. The answer I get when I run the real program is: Error: (converted from warning) Error in : (converted from warning) Error in : arguments imply differing number of rows: 3455, 4319. What might be causing this?

    lc <- list("test.txt", "test.txt", "test.txt", "test.txt")
    lc1 <- list("test.txt", "test.txt", "test.txt")
    lc2 <- list("test.txt", "test.txt")
    #list of lists.  The lists contain file names
    lc <- list(lc, lc1, lc2) 
    #new names for the three lists in the list of lists
    new_dataFns <- list("name1", "name2", "name3")
    file_paths <- NULL
    new_path <- NULL
    #add the file names to the path and read and merge the contents of each list in the list of lists
    lapply(
      lc, 
      function(lc) {
        filenames <- file.path(getwd(), lc)
        dataList <<- lapply(filenames, function (lc) read.table(file=lc, header=TRUE))
        dataList <<- lapply(dataList, function(dataList) {merge(as.data.frame(dataList),as.data.frame(dataList))})
        
      }
    )  
    
    #add the new name of the file to the path total will be 3 paths/fille_newname.tsv.  
    lapply(new_dataFns, function(new_dataFns) {new_path <<- file.path(getwd(), new_dataFns)})
    
    finalFiles <- merge(as.data.frame(dataList), as.data.frame(new_path))
    print(finalFiles)

The error message "arguments imply differing number of rows" is often the result trying to create a data frame that requires all columns/variables have the same number of rows/observations, but the variables provided don't have the same number of observations.

For example, the reprex below:

a <- c("foo", "bar")
b <- c(0, 0, 0)
df <- data.frame(a, b)
#> Error in data.frame(a, b): arguments imply differing number of rows: 2, 3

Created on 2018-08-22 by the reprex package (v0.2.0.9000).


But it's really hard to say given the information provided.
We obviously can't replicate your error with the info provided, since this isn't a reprex and I don't have access to a test.txt example.

I would convert your question into a reprex that replicates the error. That will be a good starting point for folks that would like to help.

1 Like

Thank you very much for your answer. I will send the represx