error when applying functions like replace_na() from tidyr on labelled columns of dataframe in R

huangapple go评论75阅读模式
英文:

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 &lt;- data.frame(a = LETTERS[1:5],
                     b = c(1,2,NA,4,5))
    Hmisc::label(df$a) &lt;- &quot;hi hi&quot;
    Hmisc::label(df$b) &lt;- &quot;bye bye&quot;
    
    names(df) &lt;- Hmisc::label(df)
    df %&gt;%
        mutate(`hi hi` = paste0(`hi hi`, &quot;people&quot;),
               `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&#39;t convert `replace` &lt;double&gt; to match type of `data` &lt;labelled&gt;.
Backtrace:
  1. df %&gt;% ...
 10. tidyr:::replace_na.default(`bye bye`, mean(`bye bye`, na.rm = T))
 11. vctrs::vec_assign(data, missing, replace, x_arg = &quot;data&quot;, value_arg = &quot;replace&quot;)
Error in mutate(., `hi hi` = paste0(`hi hi`, &quot;people&quot;), `bye bye` = replace_na(`bye bye`, :

Caused by error in `vec_assign()`:
! Can&#39;t convert `replace` &lt;double&gt; to match type of `data` &lt;labelled&gt;.

答案1

得分: 1

replace_nabye bye 的数据类型似乎有冲突;这里 bye bye 可能需要是数值型的:

library(dplyr)

df |&gt;
  mutate(`hi hi` = paste0(`hi hi`, &quot;people&quot;),
         `bye bye` = as.numeric(`bye bye`),
         `bye bye` = replace_na(`bye bye`, mean(`bye bye`, na.rm = T)))

不过你也可以考虑使用 base 的方法:

is.na(df$`bye bye`) &lt;- 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 |&gt;
  mutate(`hi hi` = paste0(`hi hi`, &quot;people&quot;),
         `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`) &lt;- 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

huangapple
  • 本文由 发表于 2023年3月3日 23:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定