2017-10-09 82 views
0

我正在嘗試將文件讀入R,第一行中具有不同的分隔符的文件具有空格作爲分隔符,但是從第二行到第一列之間的最後一行有一個空格,第二個和第三個之間相同,那麼兩個零和一個塊的所有塊應該是不同的列。 任何提示?!用不同的分隔符將數據讀入R

ID Chip AX-77047182 AX-80910836 AX-80737273 AX-77048714 AX-77048779 AX-77050447 
3811582 1 2002202222200202022020200200220200222200022220002200000201202000222022 
3712982 1 2002202222200202022020200200220200222200022220002200000200202000222022 
3712990 1 2002202211200202021011100101210200111101022121112100111110211110122122 
3713019 1 2002202211200202021011100101210200111101022121112100111110211110122122 
3713025 1 2002202211200202021011100101210200111101022121112100111110211110122122 
3713126 1 2002202222200202022020200200220200222200022220002200000200202000222022 
+0

您能否提供一個預期輸出的例子? – emilliman5

+0

確定爲前三行:第一行:ID芯片AX-77047182 AX-80910836。第二行:3811582 1 2 0.分隔符應該是空格。 – Nico

+0

請修改您的帖子,而不是評論 – emilliman5

回答

0

肯定不是最完美的解決方案,但你可以嘗試以下。如果我已經正確理解了您的示例數據,那麼您並未提供零行/一行/二行所需的所有列名稱(AX-77047182,...)。如果我的理解錯誤,下面的方法不會產生所需的結果,但仍可能幫助您找到解決方法 - 您可能只需在第二個split命令中調整分隔符。我希望這有助於...

#read file as character vector 
chipstable <- readLines(".../chips.txt") 

#extact first line to be used as column names 
tablehead <- unlist(strsplit(chipstable[1], " ")) 

#split by first delimiter, i.e., space 
chipstable <- strsplit(chipstable[2:length(chipstable)], " ") 

#split by second delimiter, i.e., between each character (here number) 
#and merge the two split results in one line 
chipstable <- lapply(chipstable, function(x) { 

    c(x[1:2], unlist(strsplit(x[3], ""))) 

}) 

#combine all lines to a data frame 
chipstable <- do.call(rbind, chipstable) 

#assign column names 
colnames(chipstable) <- tablehead 

#turn values to numeric (if needed) 
chipstable <- apply(chipstable, 2, as.numeric) 
-1

你可以試試... read(pattern = " || 1 ", recursive = TRUE) 使後綁定

例如:

data <- "ID Chip AX-77047182 AX-80910836 AX-80737273 AX-77048714 AX-77048779 AX-77050447 
3811582 1 2002202222200202022020200200220200222200022220002200000201202000222022 
3712982 1 2002202222200202022020200200220200222200022220002200000200202000222022 
3712990 1 2002202211200202021011100101210200111101022121112100111110211110122122 
3713019 1 2002202211200202021011100101210200111101022121112100111110211110122122 
3713025 1 2002202211200202021011100101210200111101022121112100111110211110122122 
3713126 1 2002202222200202022020200200220200222200022220002200000200202000222022" 

teste <- strsplit(data, split = "\n") 

for(i in seq(1, length(teste[[1]]),1)) { 
    if (i==1) { 
    dataOut <- strsplit(teste[[1]][i], split = " ") 
    print(dataOut) 
    } else 
    dataOut <- strsplit(teste[[1]][i], split = " 1 ") 
    print(dataOut) 
}