【问题标题】:No applicable method for 'extract_' applied to an object of class "character"没有适用于“提取_”的方法应用于“字符”类的对象
【发布时间】:2020-10-13 08:53:07
【问题描述】:

我前段时间,我发布了this问题。

使用的数据如下:

library(data.table)
library(tidyverse)
library(dplyr)
library(magrittr)
DT <- structure(list(Abbreviation = c("AK", "AK", "AK", "AK", "AK", 
"AK", "AK", "AK", "AK", "AK"), date = c("1/31/2011", "10/31/2011", 
"11/30/2011", "12/31/2010", "4/30/2005", "2/28/2011", "3/31/2011", 
"4/30/2011", "5/31/2011", "6/30/2011"), year = c("2011", "2011", 
"2011", "2010", "2005", "2011", "2011", "2011", "2011", "2011"
), c1 = c("P", "P", "P", "P", "P", "P", "P", "P", "P", "P"), 
    State = c("Alaska", "Alaska", "Alaska", "Alaska", "Alaska", 
    "Alaska", "Alaska", "Alaska", "Alaska", "Alaska"), month = c("01", 
    "10", "11", "12", "04", "02", "03", "04", "05", "06"), total = c(18395, 
    10654, 14113, 16248, 14029, 17915, 17152, 15543, 13325, 11637
    ), variable = structure(c(1L, 2L, 4L, 5L, 13L, 17L, 18L, 20L, 
    1L, 1L), .Label = c("male", "female", "c4", "upto22", "from22to24", 
    "from25to34", "from35to44", "from45to54", "from55to59", "from60to64", 
    "over65", "c20", "hispanic", "non_nispanic", "c42", "native", 
    "asian", "black", "hawaii", "white", "c48", "c49", "c50", 
    "c87", "c88", "c89", "c90", "c91", "c92", "c93"), class = "factor"), 
    value = c(12288, 5863, 8500, 10508, 8860, 12060, 11594, 9997, 
    8158, 6294)), row.names = c(NA, -10L), class = c("data.table", 
"data.frame"))

所涉及的答案,这段代码:

lut <- data.table(value = names(DT))[
  , variable := value %>% 
    shift() %>% 
    like("c\\d{1,2}") %>% 
    cumsum() %>% 
    add(1L) %>% 
    extract(c("id", "sex", "age", "race", "ethn"),. )][]

应该创建以下数据:

           value variable
 1: Abbreviation       id
 2:         date       id
 3:        month       id
 4:         year       id
 5:           c1       id
 6:         male      sex
 7:       female      sex
 8:           c4      sex
 9:       upto22      age
10:   from22to24      age
11:   from25to34      age
12:   from35to44      age
13:   from45to54      age
14:   from55to59      age
15:   from60to64      age
16:       over65      age
17:          c20      age
18:     hispanic     race
19: non_hispanic     race
20:          c42     race
21:       native     ethn
22:        asian     ethn
23:        black     ethn
24:       hawaii     ethn
25:        white     ethn
26:          c48     ethn
           value variable

在此之前完美运行,现在我收到错误:

Error in UseMethod("extract_") : 
  no applicable method for 'extract_' applied to an object of class "character"

我一直在思考可能发生的变化以及为什么会出现这个错误。

如果有人有想法,那就太好了。

【问题讨论】:

  • could not find function "add"。你忘记加载magrittr了吗?
  • 我会在帖子中添加library(magrittr),但我没有忘记加载它。仔细检查,仍然得到同样的错误。谢谢你指出:)
  • 您的代码使用来自tidyrextract 而不是magrittr。尝试改用magrittr::extract
  • 可能自己添加答案并保留帖子。
  • 这就是当你装载一卡车体面而你可以用一个包裹做所有事情时发生的情况(+ magrittr 如果真的需要管道)。

标签: r dplyr data.table


【解决方案1】:

在这种情况下,问题是,正如 cmets 中所指出的,执行代码时使用了错误的库。因此解决方案是在代码中指定库:

lut <- data.table(value = names(DT))[
  , variable := value %>% 
    shift() %>% 
    like("c\\d{1,2}") %>% 
    cumsum() %>% 
    magrittr::add(1L) %>% 
    magrittr::extract(c("id", "sex", "age", "race", "ethn"),. )][]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-06
    • 2018-01-12
    • 2023-03-13
    • 1970-01-01
    • 2022-09-24
    • 2021-10-09
    • 1970-01-01
    相关资源
    最近更新 更多