- , , , rowname rownumber dat.
, , :
> dat <- data.frame(matrix(runif(30000*50),ncol=50))
> rownames(dat) <- as.character(sample.int(nrow(dat)))
> rownames(dat)[1:5]
[1] "21889" "3050" "22570" "28140" "9576"
rows 15000 , 50 1 30000 ( *):
> rows <- sapply(1:15000, function(i) as.character(sample.int(30000,size=sample.int(50,size=1))))
(ouch!):
# method 1
> system.time((res1 <- lapply(rows,function(r) dat[r,])))
user system elapsed
182.306 0.877 188.362
. map[i] i.
FIRST, 1:nrow(dat), ! , , :
> map <- sort(as.numeric(rownames(dat)), index.return=T)$ix
# NOTE: map[ as.numeric(rowname) ] -> rownumber into dat for that rowname.
:
> system.time((res2 <- lapply(rows,function(r) dat[map[as.numeric(r)],])))
user system elapsed
32.424 0.060 33.050
, ( , , R):
> all(rownames(res1)==rownames(res2))
[1] TRUE
, ~ 6x. , ...
SECOND , nrow(dat), , , , max(as.numeric(rownames(dat))) , nrow(dat). map map[rowname], , , map , :
map <- rep(-1,max(as.numeric(rownames(dat))))
obj <- sort(as.numeric(rownames(dat)), index.return=T)
map[obj$x] <- obj$ix
Then use mapas before ( dat[map[as.numeric(r),]]).