sqldf 함수를 사용하여 테이블의 서브 세트를 반복적으로 조인합니다. 반복 프로세스는 for 루프 내에서 발생합니다. 색인을 추가하면 이러한 조인의 성능이 향상 될 수 있음을 읽었습니다 here.R sqldf - 루프 내부/외부의 인덱스
내 질문에 - 루프에서 반복적으로이 작업을 수행하는 경우 루프가 실행될 때마다 인덱스를 다시 만들어야한다는 의미입니까, 아니면 루프 외부에서 인덱스를 유지할 수있는 방법이 있습니까? 루프 내부에서 사용 되는가?
나는 단지이 버전 본 적이 즉:
for(i in 1:10){
df1 <- sqldf(c('create index...','select * from table1'))
}
을 이런 식으로 뭔가를 할 수있는 방법이 있나요 :
df1 <- sqldf('create index...') # create index outside of loop
for(i in 1:10){
df2 <- sqldf('select * from t1 left join t2 on t1.col1 = t2.col1')
}
편집 :
> sqldf()
NULL
>
> sqldf("create index idx on iris(Species)") ##
data frame with 0 columns and 0 rows
> sqldf("select count(*) from main.iris where Species = 'virginica'") ##
Error in rsqlite_send_query([email protected], statement) :
no such table: main.iris
> sqldf("select count(*) from main.iris where Species <> 'virginica'") ##
Error in rsqlite_send_query([email protected], statement) :
no such table: main.iris
>
> sqldf()
<SQLiteConnection>
Path: :memory:
Extensions: TRUE
>
EDIT_2 :
> sqldf()
NULL
> # close an old connection if it exists
> if (!is.null(getOption("sqldf.connection"))) sqldf()
> sqldf()
<SQLiteConnection>
Path: :memory:
Extensions: TRUE
> sqldf("create index idx on iris(Species)") ##
data frame with 0 columns and 0 rows
> sqldf("select count(*) from main.iris where Species = 'virginica'") ##
count(*)
1 50
> sqldf("select count(*) from main.iris where Species <> 'virginica'") ##
count(*)
1 100
> sqldf()
NULL
위의 출력이 올바른가요? – screechOwl
예, 문제의 EDIT_2에 표시된 출력이 올바르게 보입니다. –