英文:
error when applying functions like replace_na() from tidyr on labelled columns of dataframe in R
问题
当我尝试在带标签的列上应用函数时,出现以下错误。这里以应用mean()函数为例。
library(tidyr)
library(dplyr)
library(Hmisc)
df <- data.frame(a = LETTERS[1:5],
b = c(1,2,NA,4,5))
Hmisc::label(df$a) <- "hi hi"
Hmisc::label(df$b) <- "bye bye"
names(df) <- Hmisc::label(df)
df %>%
mutate(`hi hi` = paste0(`hi hi`, "people"),
`bye bye` = replace_na(`bye bye`, mean(`bye bye`, na.rm = T)))
#mean(df$`bye bye`, na.rm = T)
错误信息为:
Error in `mutate()`:
ℹ In argument: `bye bye = replace_na(`bye bye`, mean(`bye bye`, na.rm = T))`.
Caused by error in `vec_assign()`:
! Can't convert `replace` <double> to match type of `data` <labelled>.
Backtrace:
1. df %>% ...
10. tidyr:::replace_na.default(`bye bye`, mean(`bye bye`, na.rm = T))
11. vctrs::vec_assign(data, missing, replace, x_arg = "data", value_arg = "replace")
Error in mutate(., `hi hi` = paste0(`hi hi`, "people"), `bye bye` = replace_na(`bye bye`, :
Caused by error in `vec_assign()`:
! Can't convert `replace` <double> to match type of `data` <labelled>.
英文:
I get error below when I want to apply a function on the labelled columns. here as an example I applied mean() function.
library(tidyr)
library(dplyr)
library(Hmisc)
df <- data.frame(a = LETTERS[1:5],
b = c(1,2,NA,4,5))
Hmisc::label(df$a) <- "hi hi"
Hmisc::label(df$b) <- "bye bye"
names(df) <- Hmisc::label(df)
df %>%
mutate(`hi hi` = paste0(`hi hi`, "people"),
`bye bye` = replace_na(`bye bye`, mean(`bye bye`, na.rm = T)))
#mean(df$`bye bye`, na.rm = T)
The error is:
Error in `mutate()`:
ℹ In argument: `bye bye = replace_na(`bye bye`, mean(`bye bye`, na.rm = T))`.
Caused by error in `vec_assign()`:
! Can't convert `replace` <double> to match type of `data` <labelled>.
Backtrace:
1. df %>% ...
10. tidyr:::replace_na.default(`bye bye`, mean(`bye bye`, na.rm = T))
11. vctrs::vec_assign(data, missing, replace, x_arg = "data", value_arg = "replace")
Error in mutate(., `hi hi` = paste0(`hi hi`, "people"), `bye bye` = replace_na(`bye bye`, :
Caused by error in `vec_assign()`:
! Can't convert `replace` <double> to match type of `data` <labelled>.
答案1
得分: 1
replace_na
和 bye bye
的数据类型似乎有冲突;这里 bye bye
可能需要是数值型的:
library(dplyr)
df |>
mutate(`hi hi` = paste0(`hi hi`, "people"),
`bye bye` = as.numeric(`bye bye`),
`bye bye` = replace_na(`bye bye`, mean(`bye bye`, na.rm = T)))
不过你也可以考虑使用 base
的方法:
is.na(df$`bye bye`) <- mean(df$`bye bye`, na.rm = TRUE)
输出:
hi hi bye bye
1 Apeople 1
2 Bpeople 2
3 Cpeople 3
4 Dpeople 4
5 Epeople 5
英文:
The type seems to conflict with replace_na
; here bye bye
probably need to be numeric:
library(dplyr)
df |>
mutate(`hi hi` = paste0(`hi hi`, "people"),
`bye bye` = as.numeric(`bye bye`),
`bye bye` = replace_na(`bye bye`, mean(`bye bye`, na.rm = T)))
However you might also consider going the base
way:
is.na(df$`bye bye`) <- mean(df$`bye bye`, na.rm = TRUE)
Output:
hi hi bye bye
1 Apeople 1
2 Bpeople 2
3 Cpeople 3
4 Dpeople 4
5 Epeople 5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论