我正在读取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有没有人有关于如何修复这个问题并将所有数据导入行的建议?我并不是要计算矢量中的对象。这个结构后来出现了几个问题。我相信,如果值在行中,这些将被修复。
发布于 2020-11-03 23:02:39
这是因为data$Fiscal_Year的输出是一个向量,并且向量没有行供R计算,向量是一维对象。您需要使用length(data$Fiscal_Year)
发布于 2020-11-04 00:51:00
重新访问后面的代码并对其进行修复可能是有意义的,这样它就可以处理向量作为输入,但是如果需要的话,您可以使用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[, "Sepal.Length", drop=FALSE])
# Sepal.Length
# 1 5.1
# 2 4.9
# 3 4.7
# 4 4.6
# 5 5.0
# 6 5.4https://stackoverflow.com/questions/64665270
复制相似问题