我正在尝试应用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只能包含1d原子矢量和列表。
当我这样做时: lapply(complete.dupremoved,类)
有些列是数字,因子,字符,整数。任何解决方案如何解决? 调试器还提供以下内容:
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
}
答案 0 :(得分:7)
此错误的原因是该函数正在将数据帧创建为原始数据框中的变量。这就是这样做的行:
complete.dupremoved$count <-complete.dupremoved[complete.dupremoved$KLIENDIKAARDINR,]
将来您可以使用此方法检查数据框,以确定每个变量的类别:
sapply(your_df_here, class)
除了主要问题,我希望你能够按因子计算参赛作品。现有几种选择。