用于修复read.csv的摘要

问题描述

我正在读取一个csv文件

data <- read.csv("F:/Root/ PUMPING - data.csv",header = T,sep = ";")

这是一个包含8个变量的1168个观测值的数据框。

变量和表格如下:

  Fiscal_Year SiteState      county  StartDate    EndDate Site_Type Count_Site Sum_gallons
1        FY 18        IA      Butler 21/11/2017 21/11/2017         F          3      554700
2        FY 18        IA Cerro Gordo 18/10/2017 19/10/2017         F          1     1124085
3        FY 18        IA Cerro Gordo 19/10/2017 19/10/2017         F          1      478240
4        FY 18        IA Cerro Gordo 20/10/2017 20/10/2017         F          1      201306
5        FY 18        IA Cerro Gordo 20/10/2017 21/10/2017         F          1      973760
6        FY 18        IA Cerro Gordo 20/10/2017 23/10/2017         F          1      784743
7        FY 18        IA Cerro Gordo 26/10/2017 26/10/2017         F          1      246462
8        FY 18        IA Cerro Gordo 27/10/2017 29/10/2017         F          1      561808
9        FY 18        IA Cerro Gordo 30/10/2017 30/10/2017         F          1      519946
10       FY 18        IA Cerro Gordo 02/11/2017 02/11/2017         F          2      816240
11       FY 18        IA Cerro Gordo 03/11/2017 08/11/2017         F          1     1260160
12       FY 18        IA Cerro Gordo 04/11/2017 04/11/2017         F          2      757145
13       FY 18        IA Cerro Gordo 05/11/2017 05/11/2017         F          3     1022532
14       FY 18        IA Cerro Gordo 06/11/2017 06/11/2017         F          1      565500
15       FY 18        IA Cerro Gordo 06/11/2017 07/11/2017         F          1      682500
16       FY 18        IA Cerro Gordo 07/11/2017 07/11/2017         F          3      905442
17       FY 18        IA Cerro Gordo 08/11/2017 08/11/2017         F          1      128880
18       FY 18        IA Cerro Gordo 09/11/2017 09/11/2017         F          2      509776
19       FY 18        IA Cerro Gordo 10/11/2017 11/11/2017         F          1      730916
20       FY 18        IA Cerro Gordo 12/11/2017 12/11/2017         F          1      440577

使用日期列时,我遇到了几个问题。 当我应用nrow()命令时,这是我看到的:

> nrow(data)
[1] 1168

但对于每个变量(例如):

> nrow(data$Fiscal_Year)
NULL
> nrow(data$SiteState)
NULL

有人建议如何解决此问题并将所有数据导入行吗?我不是要计算向量中的对象。此结构稍后会出现一些问题。我相信如果这些值在行中的位置,这些将是固定的。

谢谢

解决方法

这是因为data$Fiscal_Year的输出是一个向量,并且向量没有R要计数的行,向量是一个三维对象。您需要使用length(data$Fiscal_Year)

,

重新访问后面的代码并进行修复以使其可以将向量作为输入来处理可能是有意义的,但是如果需要,可以使用nrow(data[,"Fiscal_Year",drop=FALSE])保留数据帧列的结构。这是R随附的iris数据集的示例:

data(iris)
str(iris)
# 'data.frame': 150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... nrow(iris)
# [1] 150
nrow(iris$Sepal.Length)
# NULL
nrow(iris[,"Sepal.Length"])
# NULL
nrow(iris[,"Sepal.Length",drop=FALSE])
# [1] 150 
head(iris$Sepal.Length)
[1] 5.1 4.9 4.7 4.6 5.0 5.4
head(iris[,drop=FALSE])
#   Sepal.Length
# 1          5.1
# 2          4.9
# 3          4.7
# 4          4.6
# 5          5.0
# 6          5.4

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...