Argument 1 is not a Vector

Hi, I am new to R. I have been trying to run the listFromLong command to transform a data frame from long format to list. I am sure that my data is in long format, however whenever I run the code I run into the following error "Error in order(rownames(res[[i]])) : argument 1 is not a vector"

After looking at the lisFromLong's source code I realized that the error must be somewhere along the lines between ** ** in this code:

listFromLong <- function(foo, unit.variable, time.variable, 
                         unit.names.variable=NULL,exclude.columns=NULL) {
  if(!is.data.frame(foo)) stop("foo must be a data.frame")

  # main helper function
  DFtoList <- function(input,rowcol,colcol,colnamecol=NULL,exclude=NULL) {
    stopifnot(length(dim(input))==2)
    datcols    <- setdiff(seq_len(ncol(input)), 
                          c(rowcol,colcol,colnamecol,exclude))
    *res        <- vector("list",length(datcols))*
    names(res) <- if (!is.null(colnames(input))) colnames(input)[datcols] else 
                                                 as.character(datcols)
    if (!is.null(colnamecol)) {
      c2n        <- na.omit(unique(input[,colnamecol]))
      names(c2n) <- na.omit(unique(input[,colcol]))
    }  
    for (i in seq_along(res)) {
      idx  <- !is.na(input[,datcols[i]])
      rown <- unique(input[idx,rowcol])
      coln <- unique(input[idx,colcol])
      res[[i]] <- matrix(NA,nrow=length(rown),ncol=length(coln))
      rownames(res[[i]]) <- rown
      colnames(res[[i]]) <- coln
      for (j in which(idx)) 
        res[[i]][as.character(input[j,rowcol]),as.character(input[j,colcol])] <- 
          input[j,datcols[i]]
      **if (!is.null(colnamecol)) colnames(res[[i]]) <- c2n[as.character(coln)]**
**      res[[i]] <- res[[i]][order(rownames(res[[i]])),,drop=FALSE]**
    }
    res
  }

My inexperience with R prevents me from pinpointing the exact source of the error as, to my understanding, the data is ready to be processed by this code. In other words, I would appreciate it if someone could tell me how to locate Argument 1, or give me an explanation as to why it was not converted to a vector in the line between * *.

My data was imported from Stata using

data <- read.dta13("path/data.dta") 

What does your data look like? Can you provide a reproducible example?

While creating the data frame I realized that the code worked correctly for a subset of the data. I realized that when excluding those observations with a vector of uniquely missing values in important variables the code ran properly. Perhaps the problem was rotted in the fact the some empty vectors were being created. Is that feasible?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.