【问题标题】:Trying to Create Factor Variable with labels getting this error message: Error in unique.default(x, nmax = nmax) : unique() applies only to vectors尝试使用收到此错误消息的标签创建因子变量:unique.default(x, nmax = nmax) 中的错误:unique() 仅适用于向量
【发布时间】:2020-10-20 00:08:12
【问题描述】:

我正在尝试创建一个新变量,对其进行重新编码,将其转换为具有新标签的因子变量。重新编码的第一部分没问题,但是当我尝试转换为因子变量时,它给了我这样的消息:unique.default(x, nmax = nmax) 中的错误:unique() 仅适用于向量

mutate(locale = case_when (V4024 %in% c("R/hme-own dwell",
                                  "R/hme-det bldg","R/home-vac/2nd", 
                                        "R/hme-htl/mtl") ~ 1,
         
          V4024 %in% c("N/hme-own yrd", "N/hme apt hall",
                                           "N/hme-on street") ~ 2,
                          V4024 %in% c("Frn/hme-yard etc","Frn/hme-on str",
                                       "Frn/hme-at hme", "Frn/hme-apt hall"
                                           ) ~ 3,
                              V4024 %in% c("Comm-rest/bar","Other comm bld",
                                           "Gas station", "Office", "Bank",
                                           "Factory/warehouse") ~ 4,
                                V4024 %in% c("Park-apt etc",
                                             "Park-parking etc") ~ 5,
                                 V4024 %in% c("Schl-school bldg", 
                                              "Schl-school prop") ~ 6,
                                V4024 %in% c("Park-noncomm","Open-on street",
                                             "Open-pub transp",
                                             "Open-apt yd etc") ~ 7,
                                  V4024 %in% c("Other-other") ~ 8))
mutate (locale = as.factor(locale)) %>%
  mutate(locale = fct_recode(as.factor(locale), 
                             "Victim's Home" = "1", "Near Victim's Home" = "2", 
                             "At or Near friends/acq" = "3",
                             "Commercial Place" = "4",
                             "Parking lot/Garage" = "5",
                             "School" = "6", "Open Areas/Public Transport" = "7",
                             "Other" = "8"))

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    不完全确定错误发生的原因,但我认为您需要很长的路才能实现所需的输出。为什么不直接在case_when 中分配相关的fct_recode 输出?

    library(dplyr)
    
    mutate(locale = factor(case_when (V4024 %in% c("R/hme-own dwell",
                                            "R/hme-det bldg","R/home-vac/2nd", 
                                            "R/hme-htl/mtl") ~ "Victim's Home",
                               
                               V4024 %in% c("N/hme-own yrd", "N/hme apt hall",
                                      "N/hme-on street") ~ "Near Victim's Home",
                               V4024 %in% c("Frn/hme-yard etc","Frn/hme-on str",
                                            "Frn/hme-at hme", "Frn/hme-apt hall"
                               ) ~ "At or Near friends/acq",
                               V4024 %in% c("Comm-rest/bar","Other comm bld",
                                            "Gas station", "Office", "Bank",
                                       "Factory/warehouse") ~ "Commercial Place",
                               V4024 %in% c("Park-apt etc",
                                   "Park-parking etc") ~ "Parking lot/Garage",
                               V4024 %in% c("Schl-school bldg", 
                                            "Schl-school prop") ~ "School",
                               V4024 %in% c("Park-noncomm","Open-on street",
                                           "Open-pub transp",
                                "Open-apt yd etc") ~ "Open Areas/Public Transport",
                               V4024 %in% c("Other-other") ~ "Other")))
    

    【讨论】:

    • 谢谢罗纳克!我是新手!如何结合 case_when 和 fct_recode?我必须先做 as.factor 吗?
    • @MariTereMolinet 你在这里不需要fct_recode。如果您想将locale 转为factor,您可以在case_when 之外进行。查看更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多