mutate_at()在to_label()作为R函数时抛出错误 [英] mutate_at() throwing an error with to_label() as its function in R

查看:81
本文介绍了mutate_at()在to_label()作为R函数时抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循此数据清除说明,但截至这行也如下所示),我收到以下错误消息:错误:mutate()输入l5cathol 出现问题。

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.

我错过了什么吗?

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


推荐答案

使用使用 dplyr 1.0.0 code>,它正在工作

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

这篇关于mutate_at()在to_label()作为R函数时抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆