我一直在尝试运行MSCMT::listFromLong命令将数据框从长格式转换为列表。我确信我的数据是长格式的,但是每当我运行代码时,我都会遇到以下错误
Error in order(rownames(res[[i]])) : argument 1 is not a vector
查看listFromLong源代码后,我意识到错误一定是在这段代码中** **之间的某处:
> 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 }
我无法确定错误的确切来源,因为据我了解,数据已准备好由此代码处理。如果有人能告诉我如何找到“参数 1”,或者解释为什么它没有转换为 ** ** 之间的行中的向量,我将不胜感激。
我的数据是使用从 Stata 导入的
data <- readstata13::read.dta13("path/data.dta")