drop \u0026\u0026!has.j出错:使用sum(complete.cas
在R和一般情况下,我对编程都很陌生.
这是我编写此脚本的目标: >我有332个csv文件.我想,“编写一个函数来读取一个充满文件的目录,并报告每个数据文件中完全观察到的案例的数量.该函数应该返回一个数据框,其中第一列是文件的名称,第二列是完整案例的数量.“ 完成< - function(directory,id = 1:332){ ## '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”.我不确定是什么让我这个错误.我将不胜感激任何可能导致此错误的建议以及我如何解决它.指向我的文学我可以阅读研究这个和/或教程,这将有助于我加强避免这种错误所需的编码技能也将不胜感激. 解决方法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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Windows下搭建Redis服务器
- 如何将公共证书导入Windows Azure?
- 将本地化资源.DLL嵌入到C#中的可执行文件中?
- VS打包程序制作的快捷方式指向错误的位置(指向安装包文件)
- WINDOWS下php5.2.4+mysql6.0+apache2.2.4+ZendOptimizer-3.
- win2008 R2 WEB环境配置之Mssql Server 2008 R2 安装图文教
- .net – Windows.System.Threading.ThreadPool和System.Thr
- Windows批处理字符串替换在变量给出搜索字符串时不起作用
- Windows – 如何阻止Microsoft DNS报告版本号?
- .net – 为dll配置单独的配置文件是不错的设计?