는 IF/다른 기능을 내가 노력하고의 mutate() 나는 예 dataframe에게이
df <- data.frame(cust = sample(1:100, 1000, TRUE),
channel = sample(c("WEB", "POS"), 1000, TRUE))
는
이 문제없이 작은 dataframes에서 작동get_channels <- function(data) {
d <- data
if(unique(d) %>% length() == 2){
d <- "Both"
} else {
if(unique(d) %>% length() < 2 && unique(d) == "WEB") {
d <- "Web"
} else {
d <- "POS"
}
}
return(d)
}
, 그것은 더 소요 변이 없습니다 시간. 데이터 프레임> 1000000 이상 cust
의 순서에, 말하자면, 오히려 커질 0.34602 초
,의
start.time <- Sys.time()
df %>%
group_by(cust) %>%
mutate(chan = get_channels(channel)) %>%
group_by(cust) %>%
slice(1) %>%
group_by(chan) %>%
summarize(count = n()) %>%
mutate(perc = count/sum(count))
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
시간 차이는 내 기본
if/else
FX는 많이 소요 많이입니다.어떻게하면이 기능을 간소화하여 더 빨리 실행할 수 있습니까?
감사합니다. 이것이'dplyr' 프레임 워크에서 가속화 될 수 있다면 어떨까요? – Steven
@Steven 죄송합니다, 저는 dplyr에 대해 많은 경험이 없습니다. data.table은 일반적으로 더 빠릅니다. – Kristofersen