gpt4 book ai didi

R-错误 : data_frames can only contain 1d atomic vectors and lists

转载 作者:行者123 更新时间:2023-12-04 10:45:13 26 4
gpt4 key购买 nike

我正在尝试应用 dplyr 包并使用以下函数计算数据集中每个卡号的条目数:

freq<- function(data){
data <- complete.dupremoved[order(-complete.dupremoved$SUMMA),]
aggregate(count ~., data=transform(complete.dupremoved,count=1), length)
complete.dupremoved$count <-complete.dupremoved[complete.dupremoved$KLIENDIKAARDINR,]
sample <- count(complete.dupremoved, vars = "KLIENDIKAARDINR")
complete.dupremoved<- merge(complete.dupremoved,sample, by ="KLIENDIKAARDINR")
return(complete.dupremoved)
}

显示的错误是错误:data_frames 只能包含一维原子向量和列表。

当我做:
lapply(complete.dupremoved,class)

一些列是 numeric 、 factor 、 character 、 integer。任何解决方案如何解决这个问题?
调试器还提供以下信息:
function (x) 
{
stopifnot(is.list(x))
if (length(x) == 0) {
x <- list()
class(x) <- c("tbl_df", "tbl", "data.frame")
attr(x, "row.names") <- .set_row_names(0)
return(x)
}
names_x <- names2(x)
if (any(is.na(names_x) | names_x == "")) {
stop("All columns must be named", call. = FALSE)
}
ok <- vapply(x, is_1d, logical(1))
**if (any(!ok)) {
stop("data_frames can only contain 1d atomic vectors and lists",
call. = FALSE)**
}
n <- unique(vapply(x, NROW, integer(1)))
if (length(n) != 1) {
stop("Columns are not all same length", call. = FALSE)
}
class(x) <- c("tbl_df", "tbl", "data.frame")
attr(x, "row.names") <- .set_row_names(n)
x
}

最佳答案

此错误的原因是该函数正在创建一个数据帧作为原始数据帧中的变量。这是执行此操作的行:

complete.dupremoved$count <-complete.dupremoved[complete.dupremoved$KLIENDIKAARDINR,]

将来,您可以使用此检查数据框以识别每个变量的类:
sapply(your_df_here, class)

撇开主要问题不谈,我希望您能够按因子计算条目。有几个现有的选项。

关于R-错误 : data_frames can only contain 1d atomic vectors and lists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34443410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com