【发布时间】:2019-12-13 16:19:19
【问题描述】:
我有一个相当宽的数据集要读取,顶部有 1000 多个缺失值,但所有变量名称都遵循相同的模式。有没有办法使用starts_with() 来强制正确解析某些变量?
MWE:
library(tidyverse)
library(readr)
mwe.csv <- data.frame(id = c("a", "b"), #not where I actually get the data from
amount1 = c(NA, 20),
currency1 = c(NA, "USD")
)
mwe <- readr::read_csv("mwe.csv", guess_max = 1) #guess_max() for example purposes
我希望能够做到
mwe<- read_csv("mwe.csv", guess.max = 1
col_types = cols(starts_with("amount") = "d",
starts_with("currency") = "c"))
)
> mwe
# A tibble: 2 x 3
id amount currency
<chr> <dbl> <chr>
1 a NA NA
2 b 20 USD
但我收到错误“意外的 '=' in: read_csv”。有什么想法吗?我无法对其进行硬编码,因为列数会定期更改,但模式 (amountN) 将保持不变。还会有其他列不是 id 或金额/货币。为了提高速度,我不希望增加 guess.max() 选项。
【问题讨论】: