![](/img/trans.png)
[英]Change levels and labels using the `as_factor` R haven function for table1 output
[英]R Problems using haven::as_factor
對於 FP,數據的答案為 1、2 和 NA。 我正在嘗試為 ggplot 框 plot 設置數據,並且需要這些因子,以便它可以將 FP 用於 plot 中的 x 值以將它們拆分為單獨的圖。 然后我想將 NA 重命名為 OM,理想情況下也將 1 和 2 重命名為字符值。
c_plot <- dataset %>%
select(XCD, FP) %>%
filter(XCD > 0) %>%
haven::as_factor(FP, levels = "labels") %>%
mutate(FP = ifelse(is.na(FP, OM, FP)) %>%
錯誤是:
Error in force(ordered) : object 'FTPT' not found
Error:
! Arguments in `...` must be used.
x Problematic argument:
* ..1 = FP
如何讓它正確生成我需要的數據?
如果沒有看到您的數據,很難准確判斷出了什么問題,但您可以嘗試類似mutate(abc = haven::as_factor(col_name))
的方法,例如:
iris %>%
mutate(abc = haven::as_factor(Sepal.Length))
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species abc
# 1 5.1 3.5 1.4 0.2 setosa 5.1
# 2 4.9 3.0 1.4 0.2 setosa 4.9
# 3 4.7 3.2 1.3 0.2 setosa 4.7
# 4 4.6 3.1 1.5 0.2 setosa 4.6
# 5 5.0 3.6 1.4 0.2 setosa 5
此外,最后一行應該是這樣的(注意括號的位置)
mutate(FP = ifelse(is.na(FP), OM, FP))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.