使用 rnoaa 包中的 lcd 函数时出现奇怪的问题 class
Strange tibble class when using lcd function from rnoaa package
我最近更新到最新版本的 R、RStudio 和 tidyverse,现在当 运行 来自 ldc 函数时出现错误=]rnoaa 包。此错误仅在更新后出现。
R version 4.0.2 (2020-06-22)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.1 purrr_0.3.4 readr_1.3.1 tidyr_1.1.1 tibble_3.0.3 ggplot2_3.3.2 tidyverse_1.3.0 rnoaa_1.1.0
loaded via a namespace (and not attached):
[1] tinytex_0.25 tidyselect_1.1.0 xfun_0.16 haven_2.3.1 colorspace_1.4-1 vctrs_0.3.2 generics_0.0.2 utf8_1.1.4 blob_1.2.1
[10] XML_3.99-0.5 rlang_0.4.7 pillar_1.4.6 withr_2.2.0 httpcode_0.3.0 glue_1.4.1 DBI_1.1.0 rappdirs_0.3.1 dbplyr_1.4.4
[19] modelr_0.1.8 readxl_1.3.1 lifecycle_0.2.0 munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.6 curl_4.3 fansi_0.4.1
[28] hoardr_0.5.2 triebeard_0.3.0 urltools_1.7.3 broom_0.7.0 Rcpp_1.0.5 scales_1.1.1 backports_1.1.8 jsonlite_1.7.0 fs_1.5.0
[37] gridExtra_2.3 hms_0.5.3 digest_0.6.25 stringi_1.4.6 grid_4.0.2 cli_2.0.2 tools_4.0.2 magrittr_1.5 crul_1.0.0
[46] crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.1 xml2_1.3.2 reprex_0.3.0 lubridate_1.7.9 assertthat_0.2.1 httr_1.4.2 rstudioapi_0.11
[55] R6_2.4.1 compiler_4.0.2
这是一个示例数据集:
Year <- seq(from = 2000, to = 2003)
Station <- c(72658014922)
dat <- crossing(Station, Year)
我用map获取数据中每年NOAA的LCD记录
dat <- dat %>%
mutate(RawData = map2(Station, Year, lcd))
当我检查 class 的 RawData 时,我得到以下信息:
class(dat$RawData[[1]])
"tbl_df" "tbl" "data.frame" "lcd"
由于 class 中包含这个奇怪的“lcd”,任何对 RawData 的调用都会出现此错误:
dat <- dat %>%
mutate(RawData = map(RawData, ~mutate_all(., as.character)))
Error: Problem with `mutate()` input `RawData`.
x `x` must be a vector, not a `tbl_df/tbl/data.frame/lcd` object.
i Input `RawData` is `map(RawData, ~mutate_all(., as.character))`.
我可以通过 运行 这个代码来绕过这个错误:
dat <- dat %>%
mutate(RawData = lapply(RawData, as_tibble))
但是我很困惑为什么map2用lcd函数给出了一个对象classtbl_df/tbl/data.frame/lcd
.
如果能帮助我理解这个错误,我将不胜感激!
属性 lcd
造成了问题
✖ x
must be a vector, not a tbl_df/tbl/data.frame/lcd
object.
,可以转换为 tibble
,它应该可以工作
library(dplyr)
library(purrr)
dat %>%
mutate(RawData = map(RawData, ~
.x %>%
as_tibble %>%
mutate(across(everything(), as.character))))
如果我们检查函数lcd
,它会在末尾添加lcd class属性
function (station, year, ...)
{
assert(station, c("character", "numeric", "integer"))
assert(year, c("character", "numeric", "integer"))
assert_range(year, 1901:format(Sys.Date(), "%Y"))
path <- lcd_get(station = station, year = year, ...)
tmp <- safe_read_csv(path)
names(tmp) <- tolower(names(tmp))
df <- tibble::as_tibble(tmp)
structure(df, class = c(class(df), "lcd")) # // adding the class attribute
}
我最近更新到最新版本的 R、RStudio 和 tidyverse,现在当 运行 来自 ldc 函数时出现错误=]rnoaa 包。此错误仅在更新后出现。
R version 4.0.2 (2020-06-22)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.1 purrr_0.3.4 readr_1.3.1 tidyr_1.1.1 tibble_3.0.3 ggplot2_3.3.2 tidyverse_1.3.0 rnoaa_1.1.0
loaded via a namespace (and not attached):
[1] tinytex_0.25 tidyselect_1.1.0 xfun_0.16 haven_2.3.1 colorspace_1.4-1 vctrs_0.3.2 generics_0.0.2 utf8_1.1.4 blob_1.2.1
[10] XML_3.99-0.5 rlang_0.4.7 pillar_1.4.6 withr_2.2.0 httpcode_0.3.0 glue_1.4.1 DBI_1.1.0 rappdirs_0.3.1 dbplyr_1.4.4
[19] modelr_0.1.8 readxl_1.3.1 lifecycle_0.2.0 munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.6 curl_4.3 fansi_0.4.1
[28] hoardr_0.5.2 triebeard_0.3.0 urltools_1.7.3 broom_0.7.0 Rcpp_1.0.5 scales_1.1.1 backports_1.1.8 jsonlite_1.7.0 fs_1.5.0
[37] gridExtra_2.3 hms_0.5.3 digest_0.6.25 stringi_1.4.6 grid_4.0.2 cli_2.0.2 tools_4.0.2 magrittr_1.5 crul_1.0.0
[46] crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.1 xml2_1.3.2 reprex_0.3.0 lubridate_1.7.9 assertthat_0.2.1 httr_1.4.2 rstudioapi_0.11
[55] R6_2.4.1 compiler_4.0.2
这是一个示例数据集:
Year <- seq(from = 2000, to = 2003)
Station <- c(72658014922)
dat <- crossing(Station, Year)
我用map获取数据中每年NOAA的LCD记录
dat <- dat %>%
mutate(RawData = map2(Station, Year, lcd))
当我检查 class 的 RawData 时,我得到以下信息:
class(dat$RawData[[1]])
"tbl_df" "tbl" "data.frame" "lcd"
由于 class 中包含这个奇怪的“lcd”,任何对 RawData 的调用都会出现此错误:
dat <- dat %>%
mutate(RawData = map(RawData, ~mutate_all(., as.character)))
Error: Problem with `mutate()` input `RawData`.
x `x` must be a vector, not a `tbl_df/tbl/data.frame/lcd` object.
i Input `RawData` is `map(RawData, ~mutate_all(., as.character))`.
我可以通过 运行 这个代码来绕过这个错误:
dat <- dat %>%
mutate(RawData = lapply(RawData, as_tibble))
但是我很困惑为什么map2用lcd函数给出了一个对象classtbl_df/tbl/data.frame/lcd
.
如果能帮助我理解这个错误,我将不胜感激!
属性 lcd
造成了问题
✖
x
must be a vector, not atbl_df/tbl/data.frame/lcd
object.
,可以转换为 tibble
,它应该可以工作
library(dplyr)
library(purrr)
dat %>%
mutate(RawData = map(RawData, ~
.x %>%
as_tibble %>%
mutate(across(everything(), as.character))))
如果我们检查函数lcd
,它会在末尾添加lcd class属性
function (station, year, ...)
{
assert(station, c("character", "numeric", "integer"))
assert(year, c("character", "numeric", "integer"))
assert_range(year, 1901:format(Sys.Date(), "%Y"))
path <- lcd_get(station = station, year = year, ...)
tmp <- safe_read_csv(path)
names(tmp) <- tolower(names(tmp))
df <- tibble::as_tibble(tmp)
structure(df, class = c(class(df), "lcd")) # // adding the class attribute
}