首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复read.csv的建议

修复read.csv的建议
EN

Stack Overflow用户
提问于 2020-11-03 22:57:03
回答 2查看 33关注 0票数 0

我正在读取csv文件,

代码语言:javascript
复制
data <- read.csv("F:/Root/ PUMPING - data.csv", header = T, sep = ";")

它是具有8个变量的1168个观察值的数据帧。

变量和表如下所示:

代码语言:javascript
复制
  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()命令时,我看到的是:

代码语言:javascript
复制
> nrow(data)
[1] 1168

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

代码语言:javascript
复制
> nrow(data$Fiscal_Year)
NULL
> nrow(data$SiteState)
NULL

有没有人有关于如何修复这个问题并将所有数据导入行的建议?我并不是要计算矢量中的对象。这个结构后来出现了几个问题。我相信,如果值在行中,这些将被修复。

EN

回答 2

Stack Overflow用户

发布于 2020-11-03 23:02:39

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

票数 2
EN

Stack Overflow用户

发布于 2020-11-04 00:51:00

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

代码语言:javascript
复制
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[, "Sepal.Length", drop=FALSE])
#   Sepal.Length
# 1          5.1
# 2          4.9
# 3          4.7
# 4          4.6
# 5          5.0
# 6          5.4
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64665270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档