이 질문은 데이터 프레임의 행을 부분 집합 화하는 더 빠른 방법을 묻습니다. 가장 빠른 방법은 data.table입니다. 이 간단한 경우 data.table에
set.seed(1) # for reproducible example
# 1 million rows - big enough?
df <- data.frame(age=sample(1:65,1e6,replace=TRUE),x=rnorm(1e6),y=rpois(1e6,25))
library(microbenchmark)
microbenchmark(result<-df[which(df$age>5),],
result<-subset(df, age>5),
result<-df[df$age>5,],
times=10)
# Unit: milliseconds
# expr min lq median uq max neval
# result <- df[which(df$age > 5), ] 77.01055 80.62678 81.43786 133.7753 145.4756 10
# result <- subset(df, age > 5) 190.89829 193.04221 197.49973 203.7571 263.7738 10
# result <- df[df$age > 5, ] 169.85649 171.02084 176.47480 185.9394 191.2803 10
library(data.table)
DT <- as.data.table(df) # data.table
microbenchmark(DT[age > 5],times=10)
# Unit: milliseconds
# expr min lq median uq max neval
# DT[age > 5] 29.49726 29.93907 30.1813 30.67168 32.81204 10
그래서 이상 6 배 빠른 subset(...)
보다 두 배 빠른 which(...)
으로보다 조금 더합니다.
(1) 'microbenchmark'패키지로 일부 벤치 마크를 실행 해보십시오. – nrussell
또한 https://stackoverflow.com/questions/9860090/in-r-why-is-better-than-subset –
에서 확인하십시오. 큰 데이터 세트에서 속도가 걱정된다면 대신'dplyr' 또는'data.table' (또는'data.table'의 프론트 엔드로'dplyr')을 사용해야합니다. –