※ 引述《gn01349943 (flying)》之銘言:
: 但我題目可能說的不夠清楚,實際情況是有值項結束不是固定的,
: 這樣就可能就需要再加個判斷有多少數量在each row,
: 例如,資料可能長這樣子,
: (dd <- data.table(x=1:6,b=c(2,'',4,5,6,7),c(1,'','',4,5,6),c(1,'','',4,5,'')
,c
: x b V3 V4 V5
: 1: 1 2 1 1
: 2: 2
: 3: 3 4
: 4: 4 5 4 4 4
: 5: 5 6 5 5 5
: 6: 6 7 6
: 想要變成這樣的格式,
: x b V3 V4 V5
: 1 1 2 1 1
: 2 2
: 3 3 4
: 4 4 5 4 4 4
: 5 5 6 5 5 5
: 6 6 7 6
這個除了直轉橫做外,提供一個用regular expression的作法
當作csv檔案,做一個平移的動作即可
好讀版程式:http://pastebin.com/xh3qt4Xa
library(data.table)
library(dplyr)
library(magrittr)
(dt <- data.table(x=1:6,b=c(2,'',4,5,6,7),c(1,'','',4,5,6),c(1,'','',4,5,''),
c('','','',4,5,'')))
old_names = names(dt)
dt %<>% setnames(paste0("V", 1:ncol(dt)))
txt = dt %>% mutate_(Vcobime = paste0("paste(", paste0("V", 1:ncol(dt),
collapse = ","), ", sep = \",\")")) %>% .$Vcobime
loc = which(substr(txt, sapply(txt, nchar), sapply(txt, nchar)) == ",")
txt[loc] = gsub("(([^,]+,)*)(,*)", "\\3\\1", txt[loc]) %>%
substr(1, sapply(.,nchar)-1) %>% paste0(",", .)
dt = fread(paste(txt, collapse = "\n")) %>% setnames(old_names)
順便補一個用20萬列、100行的資料測試結果:
set.seed(100)
n_cols = 100
n_rows = 2e5
rightShift = sample(0:39, n_rows, TRUE)
m = matrix(rbinom(n_cols*n_rows, 10, 0.5), n_rows)
dt = t(sapply(1:n_rows, function(i){
c(as.character(m[i,1:(n_cols-rightShift[i])]), rep('', rightShift[i]))
})) %>% data.table
用regular expression 大概花 8秒 (amazing!!!)
用直轉橫再用.SD做平移大概花220秒
測試機器:RRO-3.2.0, windows 7 64bit, i7-3770K@4.4GHz, 16G ram