NA가있는 데이터 세트에서 요인 수준을 만드는 것은 개별 열에 대해 작동하지만 더 많은 열 (모두 'impact.'로 시작)을 반복해야하고 dplyr mutate (across) 내부에서 문제가 발생했습니다.
내가 도대체 뭘 잘못하고있는 겁니까?
아래 Reprex
library(tribble)
library(dplyr)
df <- tribble(~id, ~tumour, ~impact.chemo, ~impact.radio,
1,'lung',NA,1,
2,'lung',1,NA,
3,'lung',2,3,
4,'meso',3,4,
5,'lung',4,5)
# Factor labels
trt_labels <- c('Planned', 'Modified', 'Interrupted', 'Deferred', "Omitted")
# Such that factor levels match labels as, retaining NAs where present:
data.frame(level = 1:5,
label = trt_labels)
# Create factor works for individual columns
factor(df$impact.chemo, levels = 1:5, labels = trt_labels)
factor(df$impact.radio, levels = 1:5, labels = trt_labels)
# But fails inside mutate(across)
df %>%
mutate(across(.cols = starts_with('impact'), ~factor(levels = 1:5, labels = trt_labels)))
@ 27ϕ9의 주석을 답으로 만드는 것 : 함수가 참조해야하는 객체 인 첫 번째 인수 (이 경우에서 선택한 데이터 프레임 열 ) 가 필요하기 때문에 purrr
내부 across
에 지정한 스타일 람다 함수 가 올바르지 않습니다 across
.
문제를 해결하려면 .x
바로 가기가 아닌 람다 함수 안에 삽입해야 합니다 function(x) x
.- 스타일 람다 함수 에 대한 자세한 내용은이 페이지 를 참조하세요 purrr
.
df %>%
mutate(across(.cols = starts_with('impact'), ~factor(.x, levels = 1:5, labels = trt_labels)))
# A tibble: 5 x 4
# id tumour impact.chemo impact.radio
# <dbl> <chr> <fct> <fct>
# 1 1 lung NA Planned
# 2 2 lung Planned NA
# 3 3 lung Modified Interrupted
# 4 4 meso Interrupted Deferred
# 5 5 lung Deferred Omitted
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다