R read_excel错误读取数值数据
我试图从Robert Shiller教授的主页下载并解析文件ie_Data.xls中的数据工作表。我从下载文件,然后运行以下脚本:R read_excel错误读取数值数据,r,excel,tidyverse,R,Excel,Tidyverse,我试图从Robert Shiller教授的主页下载并解析文件ie_Data.xls中的数据工作表。我从下载文件,然后运行以下脚本: library(tidyverse) ie_data <- read_excel("ie_data.xls", sheet = "Data", col_names = TRUE, col_types = "numeric", na = "&qu
library(tidyverse)
ie_data <- read_excel("ie_data.xls", sheet = "Data", col_names = TRUE,
col_types = "numeric", na = "", skip = 7) %>%
select(Date,E) %>%
drop_na()
生成了一系列警告,但更麻烦的是输出
> names(ie_data)
[1] "Date" "E"
> ie_data
# A tibble: 1,791 x 2
Date E
<dbl> <dbl>
1 1871. 0.4
2 1871. 0.4
3 1871. 0.4
4 1871. 0.4
5 1871. 0.4
6 1871. 0.4
7 1871. 0.4
8 1871. 0.4
9 1871. 0.4
10 1871. 0.4
# ... with 1,781 more rows
Warning message:
`...` is not empty.
We detected these problematic arguments:
* `needs_dots`
These dots only exist to allow future extensions and should be empty.
Did you misspecify an argument?
这两列的内容都应该有两个小数位1871.01代表1871年1月,1871.02代表1871年2月,依此类推,第二列是每股收益,四舍五入到最接近的便士,但小数点后的所有内容都会在数据框开头的第一列中消失!更神秘的是它的尾巴:
> tail(ie_data)
# A tibble: 6 x 2
Date E
<dbl> <dbl>
1 2019. 135.
2 2019. 137.
3 2019. 139.
4 2020. 132.
5 2020. 124.
6 2020. 116.
Warning message:
`...` is not empty.
We detected these problematic arguments:
* `needs_dots`
These dots only exist to allow future extensions and should be empty.
Did you misspecify an argument?
现在两列都失去了分数部分!为了正确读取这些列,我需要对代码进行哪些更改
谨此致以诚挚的谢意
Thomas Philips在使用ie_数据打印数据时,您可以执行以下操作,以便在控制台中看到更多有效数字。这不会影响数据,只会影响打印到控制台时数据的显示方式
options(pillar.sigfig = 10)
ie_data
这将显示:
Date E
<dbl> <dbl>
1 1871.01 0.4
2 1871.02 0.4
3 1871.03 0.4
4 1871.04 0.4
5 1871.05 0.4
6 1871.06 0.4
7 1871.07 0.4
8 1871.08 0.4
9 1871.09 0.4
10 1871.1 0.4
# ... with 1,781 more rows
您将获得:
# A tibble: 1,791 x 2
Date E
<dbl> <dbl>
1 1871. 0.4
2 1871. 0.4
3 1871. 0.4
4 1871. 0.4
5 1871. 0.4
6 1871. 0.4
7 1871. 0.4
8 1871. 0.4
9 1871. 0.4
10 1871. 0.4
# ... with 1,781 more rows
当使用ie_数据打印数据时,您可以执行以下操作以在控制台中查看更多有效数字。这不会影响数据,只会影响打印到控制台时数据的显示方式
options(pillar.sigfig = 10)
ie_data
这将显示:
Date E
<dbl> <dbl>
1 1871.01 0.4
2 1871.02 0.4
3 1871.03 0.4
4 1871.04 0.4
5 1871.05 0.4
6 1871.06 0.4
7 1871.07 0.4
8 1871.08 0.4
9 1871.09 0.4
10 1871.1 0.4
# ... with 1,781 more rows
您将获得:
# A tibble: 1,791 x 2
Date E
<dbl> <dbl>
1 1871. 0.4
2 1871. 0.4
3 1871. 0.4
4 1871. 0.4
5 1871. 0.4
6 1871. 0.4
7 1871. 0.4
8 1871. 0.4
9 1871. 0.4
10 1871. 0.4
# ... with 1,781 more rows
尝试使用col_types=text
我真的不知道为什么numeric会让你修剪数字,但我似乎可以让它与文本一起工作,只要你以后转换成一个整数试试col_types=text
我真的不知道为什么numeric会让你得到修剪过的数字,但我似乎可以让它与文本一起工作,如果你以后转换成一个舍入的数字我使用了与你上面列出的代码相同的代码,如果你键入printie_data$Date,你会看到数据中存在小数位。您也可以单击全局环境中的ie_数据或控制台中的Viewie_数据,您将看到小数点。我使用的代码与上面列出的代码相同,如果您键入printie_data$Date,您将看到小数点存在于数据中。您也可以单击全局环境中的ie_数据或控制台中的Viewie_数据,您将看到小数点位置。谢谢大家-这非常有用谢谢大家-这非常有用