簡體   English   中英

使用 mutate 和 case_when 更新 var 時出現奇怪的錯誤消息

[英]strange error message while using mutate and case_when to update a var

我有一個 56 行的 df,我想PAV" base on 。這很簡單,但我遇到了非常奇怪的錯誤。誰能告訴我可能是什么原因?

df<-structure(list(tt = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                        NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "Y", "Y", "Y", "Y", "Y", 
                        "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", 
                        "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", 
                        "Y", "Y", "Y", "Y", "Y"), PAV = structure(c("2", "2", "2", "2", 
                                                                    "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
                                                                    "3", "5", "5", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
                                                                    "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
                                                                    "4", "4", "4", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", 
                                                                    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
                                                                    "0", "0", "0", "0", "0", "", "", "", "", "", "", "", "", "", 
                                                                    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
                                                                    "", "", "", "", "", "", "", "", "", "", "", "", ""), .Dim = c(56L, 
                                                                                                                                  2L))), class = "data.frame", row.names = c(NA, 56L))

df<- df %>% 
mutate(PAV=case_when(tt=="Y"~PAV))

錯誤代碼是: Error: Problem with mutate() column PAV . i . i PAV = case_when(tt == "Y" ~ PAV) . x . x tt == "Y" ~ PAV must be length 56 or one, not 112.

“PAV”列是matrix列。 我們可能需要先轉換為兩個常規列,然后across這些列以更新值

library(purrr)
library(dplyr)
df %>% 
  invoke(data.frame, .) %>%
  mutate(across(starts_with('PAV'), ~ case_when(tt %in% 'Y' ~ .x)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM