【发布时间】: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"))
【问题讨论】: