2016-07-27 51 views
1

我有一個程序,可以導出csv文件,但不會引用新行或使用/n而不是/n/r。它在記錄的中間使用與末尾相同的行尾。該程序確實使用變量之間的逗號分隔符。我如何告訴R刪除所有的eol標記,直到達到數據中的變量數目?R有沒有辦法加入csv文件的虛線?

我的數據是這樣的:

name, rank, serial number, age, height, weight 

mike, noob, 123456, 22, 6, 34.4 

bob, officer, 345 

323, 24, 6, 2 

3.5 

ted, officer, 34234, 2 

5, 6, 35.2 

我怎麼會基本上在2行中的5後3行,第6行2之後刪除CR,2個後?每行應該有5個逗號和6個變量。我的數據在每行之間沒有額外的行。我只是無法停止將它全部放在一條線上,而沒有這樣做。我的數據是43個變量,並不斷產生新的行。大多數情況下,閱讀時有幾千行。其中約20%有CR問題。

此外,還希望添加一個新行將始終在新行開始,它不會跟在前一行,如果這是有道理的。

數據幀應該是這樣的:

name, rank, serial number, age, height, weight 

mike, noob, 123456, 22, 6, 34.4 

bob, officer, 345323, 24, 6, 23.5 

ted, officer, 34234, 25, 6, 35.2 

這是我的數據是什麼樣子沒有什麼幫助。第一行是一個標題,後面是6條記錄,但是read.csvfread以及我嘗試過的所有內容都給出了10條記錄。第6條記錄有額外的CR,但仍有42個變量。剛分解成5行。

EFPCName,EFUseAPPE,log pdl,pdl error,device pretty name,num pages,num sheets,copies printed,total pages printed,total sheets printed,total color pages printed,total bw pages printed,total tab pages printed,total sample pages printed,num copies,print status,instructions,notes1,notes2,username,noneutf8lastuser,non utf8 submitted by,title,size,logical printer,fiery,time,date,total rip duration,timestamp spooling,timestamp done spooling,timestamp waiting to rip,timestamp ripping,timestamp done ripping,timestamp waiting to print,timestamp printing,timestamp done printing,media weight,input slot,media size,media type,interpreter, 

LZX Laser 24 - 11 x 17 Tabloid,,postscript,,Canon,2,1,1,2,1,1,1,0,0,1,OK,,,,TeamMember,,TeamMember,78053.01.pdf,4004491,Canon hold,SERVER-Shredder,2013 06 07 19 37 13,2013 06 07 19 37 00,3,2013 06 07 19 37 23,2013 06 07 19 37 24,2013 06 07 19 38 02 118342,2013 06 07 19 38 02 118342,2013 06 07 19 38 09,2013 06 07 19 38 09,2013 06 07 19 38 38,2013 06 07 19 39 19 124419,,Tray5,Tabloid,Plain,PS, 

LZX Laser 24 - 11 x 17 Tabloid,,postscript,,Canon,2,1,1,2,1,1,1,0,0,1,OK,,,,TeamMember,,TeamMember,78053.01.pdf,4004520,none,SERVER-Shredder,2013 06 07 19 37 13,2013 06 07 19 37 00,,2013 06 07 19 44 07 926090,2013 06 07 19 44 07 926744,2013 06 07 19 44 07 926090,2013 06 07 19 44 07 926090,2013 06 07 19 44 07 926744,2013 06 07 19 44 07,2013 06 07 19 44 11,2013 06 07 19 44 53 141084,,Tray5,Tabloid,Plain,PS, 

LZX Laser 24 - 11 x 17 Tabloid,,postscript,,Canon,2,1,1,2,1,1,1,0,0,1,OK,,,,TeamMember,,TeamMember,78053.01.pdf,4004520,none,SERVER-Shredder,2013 06 07 19 37 13,2013 06 07 19 37 00,,2013 06 07 19 46 01 550964,2013 06 07 19 46 01 551451,2013 06 07 19 46 01 550964,2013 06 07 19 46 01 550964,2013 06 07 19 46 01 551451,2013 06 07 19 46 01,2013 06 07 19 46 05,2013 06 07 19 46 46 911557,,Tray5,Tabloid,Plain,PS, 

LZX80 Color Copy Cover - 11 x 17 Tabloid,,postscript,,Canon,1,2,2,2,2,2,0,0,0,2,OK,,,,TeamMember,,TeamMember,78011.01.pdf,874486,Canon hold,SERVER-Shredder,2013 06 07 19 47 07,2013 06 07 19 47 00,3,2013 06 07 19 47 17,2013 06 07 19 47 17 507576,2013 06 07 19 47 47 960542,2013 06 07 19 47 47 960542,2013 06 07 19 47 51,2013 06 07 19 47 51,2013 06 07 19 47 54,2013 06 07 19 48 25 77595,,Tray3,Tabloid,Heavy5,PS, 

LZX Laser 24 - 11 x 17 Tabloid,,postscript,,Canon,2,1,1,2,1,1,1,0,0,1,OK,,,,TeamMember,,TeamMember,78053.01.pdf,4004520,none,SERVER-Shredder,2013 06 07 19 37 13,2013 06 07 19 37 00,,2013 06 07 19 48 04 501212,2013 06 07 19 48 04 502522,2013 06 07 19 48 04 501212,2013 06 07 19 48 04 501212,2013 06 07 19 48 04 502522,2013 06 07 19 48 04,2013 06 07 19 48 07,2013 06 07 19 48 48 188474,,Tray5,Tabloid,Plain,PS, 

EX32 Laser 32 - 11 x 17 Tabloid,,pdf,,Canon,63,64,1,63,64,4,59,0,0,1,OK,Size: 11 x 17 
Finishing: Coil Binding Cutting Punching 
Pages: 
1-63 4/0 EX32 Laser 32 - 11 x 17 11 x 17 
,Color 77992:01Employee Handbook REVISED_2up(NFC).pdf, McAllen TX,EFI Pace,,,Color 77992:01Employee Handbook REVISED_2up(NFC).pdf,518880,none,SERVER-Shredder,2013 06 07 20 01 52,2013 06 07 20 01 00,3,2013 06 07 20 02 41 495216,2013 06 07 20 02 44 780196,2013 06 07 20 02 41 871208,2013 06 07 20 02 41 871208,2013 06 07 20 02 45,2013 06 07 20 02 45,2013 06 07 20 03 25,2013 06 07 20 05 45 741386,,Tray4,Tabloid,Heavy1,PS, 

回答

1

這就是我現在所擁有的。看看這對你的數據如何工作。

dat <- readLines("temp.txt") # read whatever is in there, one line at a time 
varnames <- unlist(strsplit(dat[1], ",")) # extract variable names 
nvar <- length(varnames) 

k <- 1 # setting up a counter 
dat1 <- matrix(NA, ncol = nvar, dimnames = list(NULL, varnames)) 

while(k <= length(dat)){ 
    k <- k + 1 
    if(dat[k] == "") {k <- k + 1 
     print(paste("data line", k, "is an empty string")) 
     if(k > length(dat)) {break} 
    } 
    temp <- dat[k] 
    # checks if there are enough commas or if the line was broken 
    while(length(gregexpr(",", temp)[[1]]) < nvar-1){ 
     k <- k + 1 
     temp <- paste0(temp, dat[k]) 
    } 
    temp <- unlist(strsplit(temp, ",")) 
    message(k) 
    dat1 <- rbind(dat1, temp) 
} 

dat1 = dat1[-1,] # delete the empty initial row  

總的想法是保持壓縮文本,直到字符串中有足夠的逗號。一旦完成,數據將以逗號分隔並作爲單行添加到矩陣中。代碼非常笨重,並且對於大型數據文件來說會很慢。這是我能做的最好的。

對於原始數據示例,代碼工作並創建一個具有42列和6行的字符矩陣。對於較小的示例,代碼無法處理最後一列中的中斷。

+0

我得到這個錯誤:錯誤if(dat [k] ==「」){:缺少值,其中TRUE/FALSE需要 另外:有50個或更多的警告(使用警告()看到前50 )。這也正在閱讀和顯示一個閃亮的應用程序。用戶無法干預。如果確實有某種問題,是否可以折騰?我還沒有看到那樣做,但我猜可能是可能的。 –

+0

如果k> length(dat),可能會發生此錯誤。 – nya

+0

@MichaelCiesielczyk它是否在datal表中創建了一些與您的真實數據正確識別的行? – nya

1

如果你想暗示添加空白領域,當你有不等長行,設置您的通話函數read.table填寫= TRUE。

如果這不是你問的問題,你能更清楚一點,並提供一個可重複的例子嗎?

+0

是的,我不想只用空白填充記錄的末尾,用空白填充下一個記錄的開頭。我需要的數據框看起來像編輯我的問題。 –

+0

數據中的空白是否有意義或者是否可以刪除全部?我的意思是 - 你需要空間嗎? – nya

+0

是的,我需要變量中的空白。 –

相關問題