下载和放大中的错误!has.j:invalid'x'type in'x&& y'当使用sum(complete.cases)Windows7 R3.2.1 [英] Error in drop && !has.j : invalid 'x' type in 'x && y’ when using sum(complete.cases) Windows7 R3.2.1

查看:938
本文介绍了下载和放大中的错误!has.j:invalid'x'type in'x&& y'当使用sum(complete.cases)Windows7 R3.2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R和一般的编程都很新。



这是我写这个脚本的目的:


  1. 有332个csv文件。我想,写一个函数,读取一个目录充满了文件,并报告在每个数据文件中完全观察到的案例数。函数应该返回一个数据框,其中第一列是文件的名称,第二列是完整个案的数目。


  2. 该函数如下:



    complete< - function(directory,id = 1:332){
    ##'directory'长度1表示
    ## CSV文件的位置

      ##'id'是一个整数向量,监视器ID号
    ##要使用

    ##返回以下格式的数据框:
    ## id nobs
    ## 1 117
    ## 2 1041
    ## ...
    ##其中'id'是监视器ID号,'nobs'是
    ##完成案例数

    }


示例输出如下所示:

  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




  1. 我的脚本到目前为止: li>

setwd(C:/ users / beachlb / Desktop / R_Programming / specdata)#this是我计算机上的本地目录, csv文件已存储

 > complete<  -  function(directory,id = 1:332){

> files_list< - list.files(directory,full.names = TRUE)#creates一个指定目录中的文件列表

> dat< - data.frame )#创建一个空数据框,我们可以使用它来添加数据到

> for(i in id){

> dat< - rbind(dat, read.csv(files_list [i]))#loops通过332个csv文件,将它们结合成一个数据框,名为dat
}

> dat $ nobs&完成情况的行数填充数据帧中的完整情况的行数

> dat_subset< - dat [which(dat [,ID ]%in%id),] #subsets dat,以便只在输出中包含所需的情况

> dat_subset [,ID,nobs] #prints所有行的所需数据框的命名列}




  1. 我运行我的功能,我得到这个错误,错误在下降&& !has.j:invalid'x'type in'x&& y'。我不知道什么是扔我的错误。我会感激任何建议,什么可能是导致此错误,以及我如何能够解决它。指向我的文学,我可以阅读研究这和/或教程,这将有助于我强化所需的编码技能,以避免这个错误,也将不胜感激。


  2. 前言:我不确定是否应该在单独的主题中提出此问题。填充所有行的完整个案总数(对于所有332个文件),而不是特定计算给定监视器标识的完整个案数,并将其放入仅限该标识的列nobs。 (请注意,每个文件以监视器id命名,并且只包含来自该监视器的案例,例如001.csv =监视器1的输出,002.csv =监视器2的输出。因此,我希望有人帮助我指向一个资源,如何子集dat,使得当nobs列填充时,nobs列中的每一行给出每个id号的完整个案的数量。



解决方案

  complete < function(directory,id = 1:332){
files_list< - list.files(directory,full.names = TRUE)
nobs< - c()
for id){
dat < - read.csv(files_list [i])
nobs <-c(nobs,sum(completecases(dat)))
}
data.frame(id,nobs)
}

但是你不应该一次读入所有的文件,然后找到完整的案例。它不会按id分隔结果。

  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


I am very new to programming, both in R and in general.

Here is my goal for writing this script:

  1. I have 332 csv files. I want to, "Write a function that reads a directory full of files and reports the number of completely observed cases in each data file. The function should return a data frame where the first column is the name of the file and the second column is the number of complete cases."

  2. The outline of the function is as follows:

    complete <- function(directory, id = 1:332) { ## 'directory' is a character vector of length 1 indicating ## the location of the CSV files

        ## '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
    

    }

Example output would look like this:

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

  1. My script so far looks like this:

setwd("C:/users/beachlb/Desktop/R_Programming/specdata") #this is the local directory on my computer where all 332 csv files are stored

>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}

  1. When I run my function as is, I get this error, "Error in drop && !has.j : invalid 'x' type in 'x && y’. I am not sure what is throwing me that error. I would appreciate any advice on what could be causing this error and how I can work to resolve it. Pointing me to literature I could read to study this and/or tutorials that would help me strengthen the coding skills needed to avoid this error would also be appreciated.

  2. Preface: I am not sure if I should ask this question on a separate thread. Right now, my function is written to populate the total number of complete cases for all rows (for all 332 files), instead of specifically calculating the number of complete cases for a given monitor id and putting that into the column nobs for that ID only. (Note that each file is named after the monitor id and contains only cases from that monitor, such that 001.csv = output from monitor 1, 002.csv = output from monitor 2). Therefore, I am hoping for someone to help point me to a resource for how to subset dat so that when the nobs column populates, each row in the nobs column gives the number of complete cases for each id number.

解决方案

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)
}

You were close. But you shouldn't read in all of the files at once and then find the complete cases. It will not separate the results by id for you. Instead I just edited your code a little bit.

Test

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

这篇关于下载和放大中的错误!has.j:invalid'x'type in'x&amp;&amp; y'当使用sum(complete.cases)Windows7 R3.2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆