0
我在一個類似的問題的工作作爲this other posting指出,並試圖適應代碼選擇我感興趣的列,使之適合我的數據文件。修剪大數據
我的問題,然而,就是生成的文件已經變得比原來的大,我不知道該代碼工作我打算的方式。
當我用SPSS打開,該數據集似乎已經採取的標題行,然後賺了幾百萬份未經第二行結束(我不得不強制停止進程)。
我注意到有一個在while循環沒有專櫃,指定的路線,可能是這種情況?我用R編程的背景非常有限。該文件是一個.csv文件,具有329個變量和數百萬行,爲4.8GB。我只需要保留大約30個變量。
這是我使用的代碼:
##Open separate connections to hold cursor position
file.in <- file('npidata_20050523-20130707.csv', 'rt')
file.out<- file('Mainoutnpidata.txt', 'wt')
line<-readLines(file.in,n=1)
line.split <-strsplit(line, ',')
##Column picking, only column 1
cat(line.split[[1]][1:11],line.split[[1]][23:25], line.split[[1]][31:33], line.split[[1]][308:311], sep = ",", file = file.out, fill= TRUE)
##Use a loop to read in the rest of the lines
line <-readLines(file.in, n=1)
while (length(line)){
line.split <-strsplit(line, ',')
if (length(line.split[[1]])>1) {
cat(line.split[[1]][1:11],line.split[[1]][23:25], line.split[[1]][31:33], line.split[[1]][308:311],sep = ",", file = file.out, fill= TRUE)
}
}
close(file.in)
close(file.out)