我正在嘗試在 R 中重新編碼一些調查資料。以下是一些與我實際擁有的資料類似的資料。
df <- data.frame(
A = rep("Y",5),
B=seq(as.POSIXct("2014-01-13"), as.POSIXct("2014-01-17"), by="days"),
C = c("Neither agree nor disagree",
"Somewhat agree",
"Somewhat disagree",
"Strongly agree",
"Strongly disagree"),
D=c("Neither agree nor disagree",
"Somewhat agree",
"Somewhat disagree",
"Strongly agree",
"Strongly disagree")
)
我查了一些其他的帖子,寫了下面的代碼:
init2<-df %>%
mutate_at(vars(c(1:4)), function(x) case_when( x == "Neither agree nor disagree" ~ 3,
x == "Somewhat agree" ~ 4,
x == "Somewhat disagree"~ 2,
x== "Strongly agree"~ 5,
x== "Strongly disaagree"~ 1
))
但這會引發錯誤
Error: Problem with `mutate()` column `B`.
i `B = (function (x) ...`.
x character string is not in a standard unambiguous format
Run `rlang::last_error()` to see where the error occurred.
我的輸入日期是 POSIXct。我應該改變他們的格式嗎?這個問題的解決方法是什么?謝謝。
uj5u.com熱心網友回復:
嘗試將POSIXt列重新編碼為您的李克特量表是沒有意義的;嘗試重新編碼該"Y"列對我來說也沒有意義,盡管至少您沒有收到關于此的錯誤。
我建議你:
明確
mutate你想要的列,df %>% mutate(across(c(C, D), ~ case_when( . == "Neither agree nor disagree" ~ 3, . == "Somewhat agree" ~ 4, . == "Somewhat disagree" ~ 2, . == "Strongly agree" ~ 5, . == "Strongly disagree" ~ 1 ))) # A B C D # 1 Y 2014-01-13 3 3 # 2 Y 2014-01-14 4 4 # 3 Y 2014-01-15 2 2 # 4 Y 2014-01-16 5 5 # 5 Y 2014-01-17 1 1明確排除您不想要的列,
df %>% mutate(across(-c(A, B), ~ case_when( . == "Neither agree nor disagree" ~ 3, . == "Somewhat agree" ~ 4, . == "Somewhat disagree" ~ 2, . == "Strongly agree" ~ 5, . == "Strongly disagree" ~ 1 )))通過一些過濾器有條件地處理它們(盡管這并非萬無一失):
df %>% mutate(across(where(~ all(grepl("agree", .))), ~ case_when( . == "Neither agree nor disagree" ~ 3, . == "Somewhat agree" ~ 4, . == "Somewhat disagree" ~ 2, . == "Strongly agree" ~ 5, . == "Strongly disagree" ~ 1 )))
僅供參考,根據https://dplyr.tidyverse.org/reference/mutate_all.html(2021年 11 月 7 日):
范圍動詞 (
_if,_at,_all) 已被across()現有動詞中的使用取代。詳情請參閱vignette("colwise")。
它與where, 由tidyselect包(秘密地)提供很好地配對。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352463.html
上一篇:僅對負數將最接近的數字子集為零
下一篇:R:將列拆分為兩部分
