요약하면 여러 개의 txt 파일에 저장된 많은 양의 데이터를 가져 오기위한 스크립트가 있습니다. sigle 파일에서 이 아닌은 모든 행이 동일한 테이블 (DF가 DT로 전환됨)에 저장되므로 각 파일에 대해 동일한 DF에 속하는 모든 행을 선택합니다 (get
DF 및 assign
행).프로그래밍 방식으로 할당하는 효율성 R
나는 DF라는 이름의, 말, 표를 만들 처음 내가 할 :
name <- "table1" # in my code the value of name will depend on different factors
# and **not** known in advance
assign(name, someRows)
그런 다음, 실행 중에 내 코드는 다른 라인은 표 데이터 프레임에 넣어하는 (다른 파일에서) 찾을 수 있습니다 그래서 :
name <- "table"
assign(name, rbindfill(get(name), someRows))
내 질문은 : assign(get(string), anyObject)
프로그래밍 과제를 수행하기위한 가장 좋은 방법은? 감사합니다
편집 :
set.seed(1)
#
dataSource <- list(data.frame(fileType = rep(letters[1:2], each=4),
id = rep(LETTERS[1:4], each=2),
var1 = as.integer(rnorm(8))),
data.frame(fileType = rep(letters[1:2], each=4),
id = rep(LETTERS[1:4], each=2),
var1 = as.integer(rnorm(8))))
# # #
#
library(plyr)
#
tablesnames <- unique(unlist(lapply(dataSource,function(x) as.character(unique(x[,1])))))
for(l in tablesnames){
temp <- lapply(dataSource, function(x) x[x[,1]==l, -1])
if(exists(l)) assign(l, rbind.fill(get(l), rbind.fill(temp))) else assign(l, rbind.fill(temp))
}
#
#
# now two data frames a and b are crated
#
#
# different method using rbindlist in place of rbind.fill (faster and, until now, I don't # have missing column to fill)
#
rm(a,b)
library(data.table)
#
tablesnames <- unique(unlist(lapply(dataSource,function(x) as.character(unique(x[,1])))))
for(l in tablesnames){
temp <- lapply(dataSource, function(x) x[x[,1]==l, -1])
if(exists(l)) assign(l, rbindlist(list(get(l), rbindlist(temp)))) else assign(l, rbindlist(temp))
}
난 정말 당신의 질문을 이해하지 않지만 것이다 당신이 아마'get'도'assign'도 사용하지 말아야한다고 말하십시오. 재현 할 수있는 예제를 제공한다면 사람들은 데이터를 다루는 더 좋은 방법을 보여 줄 수 있습니다. – Roland
@Roland 안녕하세요, 제 질문을 수정했습니다. 당신이 실제로 이해하지 못하는 것은 무엇입니까? – Michele
@Roland 재현 가능한 예제가 준비되었습니다! – Michele