I have been trying to run the MSCMT::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 listFromLong
'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 }
I can't pinpoint the exact source of the error as, to my understanding, the data is ready to be processed by this code. 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 <- readstata13::read.dta13("path/data.dta")