加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

drop \u0026\u0026!has.j出错:使用sum(complete.cas

发布时间:2020-12-14 05:44:53 所属栏目:Windows 来源:网络整理
导读:在R和一般情况下,我对编程都很陌生. 这是我编写此脚本的目标: 我有332个csv文件.我想,“编写一个函数来读取一个充满文件的目录,并报告每个数据文件中完全观察到的案例的数量.该函数应该返回一个数据框,其中第一列是文件的名称,第二列是完整案例的数量.“ 功
在R和一般情况下,我对编程都很陌生.

这是我编写此脚本的目标:

>我有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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读