大佬教程收集整理的这篇文章主要介绍了drop \u0026\u0026!has.j出错:使用sum(complete.cases)时,’x \u0026\u0026 y’中的’x’类型无效Windows7 R3.2.1,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我编写此脚本的目标:
>我有332个csv文件.我想,“编写一个函数来读取一个充满文件的目录,并报告每个数据文件中完全观察到的案例的数量.该函数应该返回一个数据框,其中第一列是文件的名称,第二列是完整案例的数量.“
>功能概要如下:
完成< - function(directory,id = 1:332){
##’directory’是长度为1的字符向量,表示
## CSV文件的位置
## 'id' is an Integer vector inDicaTing the monitor id numbers ## to be used ## Return a data frame of the form: ## id nobs ## 1 117 ## 2 1041 ## ... ## where 'id' is the monitor id number and 'nobs' is the ## number of complete cases
}
示例输出如下所示:
source("complete.R") complete("specdata",1) ## id nobs ## 1 1 117 complete("specdata",c(2,4,8,10,12)) ## id nobs ## 1 2 1041 ## 2 4 474 ## 3 8 192 ## 4 10 148 ## 5 12 96
>到目前为止,我的脚本如下所示:
setwd(“C:/ users / beachlb / Desktop / R_ProgrAMMing / specdata”)#这是我计算机上存储所有332个csv文件的本地目录
>complete <- function(directory,id = 1:332) { >files_list <- list.files(directory,full.names=TRUE) #creates a list of files from within the specified directory >dat <- data.frame() #creates an empty data frame that we can use to add data to >for (i in id) { >dat <- rbind(dat,read.csv(files_list[i])) #loops through the 332 csv files,rbinding them together into one data frame called dat } >dat$nobs <- sum(complete.cases(dat)) #add the column nobs to dat,populated with number of rows of complete cases in the dataframe >dat_subset <- dat[which(dat[,"ID"] %in% id),] #subsets dat so that only the desired cases are included in output when function is run >dat_subset[,"ID","nobs"] #prints all rows of the desired data frame for the named columns}
>当我按原样运行我的功能时,我收到此错误,“丢弃错误&& !has.j:’x&&中的无效’x’类型Y”.我不确定是什么让我这个错误.我将不胜感激任何可能导致此错误的建议以及我如何解决它.指向我的文学我可以阅读研究这个和/或教程,这将有助于我加强避免这种错误所需的编码技能也将不胜感激.
>前言:我不确定是否应该在另一个帖子中提出这个问题.现在,我的函数被编写为填充所有行的完整案例总数(对于所有332个文件),而不是专门计算给定监视器ID的完整案例数,并将其放入仅用于该ID的列nobs中. (请注意,每个文件都以监视器ID命名,并且仅包含来自该监视器的个案,例如001.csv =来自监视器1的输出,002.csv =来自监视器2的输出).因此,我希望有人帮我指出如何分配数据的资源,这样当nobs列填充时,nobs列中的每一行都会给出每个id号的完整个案数.
complete <- function(directory,id = 1:332) { files_list <- list.files(directory,full.names=TRUE) nobs <- c() for (i in id) { dat <- read.csv(files_list[i]) nobs <- c(nobs,sum(complete.cases(dat))) } data.frame(id,nobs) }
你很亲密但是你不应该立即读入所有文件,然后找到完整的案例.它不会以id为您分隔结果.相反,我只是稍微编辑了你的代码.
测试
complete("specdata",12)) id nobs 1 2 1041 2 4 474 3 8 192 4 10 148 5 12 96
以上是大佬教程为你收集整理的drop \u0026\u0026!has.j出错:使用sum(complete.cases)时,’x \u0026\u0026 y’中的’x’类型无效Windows7 R3.2.1全部内容,希望文章能够帮你解决drop \u0026\u0026!has.j出错:使用sum(complete.cases)时,’x \u0026\u0026 y’中的’x’类型无效Windows7 R3.2.1所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。