问题
I'm following this data cleaning instruction, but as of this line (also shown below), I get the following error: Error: Problem with mutate() input l5cathol
.
Am I missing something?
library(tidyverse)
library(haven)
library(sjmisc)
library(googledrive)
googledrive::drive_download('https://drive.google.com/file/d/124WOY4iBXxv_9eBXsoHJVUzX98x2sxYy/view?usp=sharing','test.por',overwrite=T)
dta <- haven::read_por('test.por')
names(dta) <- tolower(names(dta))
# Convert variables of interest to character/numeric
vars_chrs <- c("childid","l5cathol","l5public","r5race","w3povrty","w3daded","w3momed","w3inccat","p5fstamp")
vars_nums <- c("w3momscr", "w3dadscr","p5numpla","p5hmage","p5hdage","c5r2mtsc")
dta <- dta %>%
mutate_at(vars(one_of(vars_chrs)), funs(as.character(to_label(.)))) %>% ##** @@ HERE throws error !!!
mutate_at(vars(one_of(vars_nums)), funs(as.numeric(as.character(to_label(.)))))
回答1:
With dplyr
1.0.0, using across
, it is working
library(dplyr) # 1.0.0
library(sjmisc)
library(haven)
dta %>%
mutate(across(vars_chrs, ~ as.character(to_label(.))),
across(vars_nums, ~ as.numeric(as.character(to_label(.)))))
# A tibble: 15,305 x 3,225
# CHILDID PARENTID S5_ST_ID S5_ID T5_ID D5T_ID A5_T_ID B5_T_ID T5_T_ID L5_S_ID S5_S_ID K5_S_ID D5_T_ID E5_T_ID U5_S_ID F5NOTEND
# <chr> <chr> <chr> <chr> <chr> <chr+lb> <chr> <chr> <chr> <chr> <chr> <chr> <chr+lb> <chr+lb> <chr+lb> <dbl+lb>
# 1 000100… 0001002P 6005 6005 "600… -2 [SUP… "6005T… "6005T… "6005T… "6005" "6005" "6005" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 2 000100… 0001003P 9997 9997 "" -2 [SUP… "" "" "" "" "" "" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 3 000100… 0001004P 6009 6009 "600… -2 [SUP… "6009T… "6009T… "6009T… "6009" "6009" "6009" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 4 000100… 0001005P 7197 7197 "719… -2 [SUP… "7197T… "7197T… "7197T… "7197" "7197" "7197" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 5 000100… 0001006P 6026 6026 "602… -2 [SUP… "6026T… "6026T… "6026T… "6026" "6026" "6026" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 6 000100… 0001007P 6027 6027 "" -2 [SUP… "" "" "" "" "" "" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 7 000100… 0001008P 9997 9997 "" -2 [SUP… "" "" "" "" "" "" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 8 000100… 0001009P 5569 5569 "556… -2 [SUP… "5569T… "5569T… "5569T… "5569" "" "5569" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
# 9 000101… 0001010P 0002 0002 "000… -2 [SUP… "0002T… "0002T… "0002T… "0002" "0002" "0002" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
#10 000101… 0001011P 0007 0007 "000… -2 [SUP… "0007T… "0007T… "0007T… "0007" "0007" "0007" -2 [SUP… -2 [SUP… -2 [SUP… 0 [FALS…
data
googledrive::drive_download('https://drive.google.com/file/d/124WOY4iBXxv_9eBXsoHJVUzX98x2sxYy/view?usp=sharing','test.por',overwrite=T)
dta <- haven::read_por('test.por')
names(dta) <- tolower(names(dta))
# Convert variables of interest to character/numeric
vars_chrs <- c("childid","l5cathol","l5public","r5race","w3povrty","w3daded","w3momed","w3inccat","p5fstamp")
vars_nums <- c("w3momscr", "w3dadscr","p5numpla","p5hmage","p5hdage","c5r2mtsc")
来源:https://stackoverflow.com/questions/63182877/mutate-at-throwing-an-error-with-to-label-as-its-function-in-r