1

我最近更新到最新版本的 R、RStudio 和 tidyverse,现在从rnoaa包运行ldc函数时出现错误。此错误仅在更新后开始。

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))

当我检查 RawData 的类时,我得到以下信息:

class(dat$RawData[[1]])

"tbl_df"     "tbl"        "data.frame" "lcd"   

由于该类中包含这个奇怪的“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))

但是我很困惑为什么带有lcd函数的map2给出了一个对象 class 。tbl_df/tbl/data.frame/lcd

我真的很感激任何帮助理解这个错误!

4

1 回答 1

0

该属性lcd正在创建问题

x必须是向量,而不是tbl_df/tbl/data.frame/lcd对象。

, 可以转换为tibble它应该可以工作

library(dplyr)
library(purrr)
dat %>% 
   mutate(RawData = map(RawData, ~
         .x %>% 
           as_tibble %>% 
            mutate(across(everything(), as.character))))

如果我们检查函数lcd,它会在末尾添加 lcd 类属性

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
}
于 2020-08-05T21:25:52.337 回答