東西供你考慮的情況下,要與結果data.frame:
raw <- ">xy|number|Name
ABCABCABC
ABCABCABC
ABCABCABC
ABC
>xy|number2|Name2
ABCABCABC
ABCABC
>xy|number3|Name3
ABCABCABC
ABCABCABC
ABCABCABC
ABCAB"
s <- readLines(textConnection(raw)) # s is vector of strings
first.line <- which(substr(s,1,1) == ">") # find first line of set
N <- length(first.line)
first.line <- c(first.line, length(s)+1) # add first line past end
# Preallocate data.frame (good idea if large)
d <- data.frame(X1=rep("",N), X2=rep("",N), X3=rep("",N), X4=rep("",N),
stringsAsFactors=FALSE)
for (i in 1:N)
{
w <- unlist(strsplit(s[first.line[i]],">|\\|")) # Parse 1st line
d$X1[i] <- w[2]
d$X2[i] <- w[3]
d$X3[i] <- w[4]
d$X4[i] <- paste(s[ (first.line[i]+1) : (first.line[i+1]-1) ], collapse="")
}
d
X1 X2 X3 X4
1 xy number Name ABCABCABCABCABCABCABCABCABCABC
2 xy number2 Name2 ABCABCABCABCABC
3 xy number3 Name3 ABCABCABCABCABCABCABCABCABCABCAB
我希望在默認情況下[R左對齊的字符串時,它會顯示他們在一個data.frame。
我敢肯定,這可以在R上完成,但它幾乎可以肯定是錯誤的語言爲任務(和你有什麼打算用這些結構中的R辦?)。如果他想要做後期處理,R,並且該文件是不是巨大的考慮命令式語言如Perl或C. –
@MatthewLUndberg,我不明白爲什麼R是錯誤的語言來做到這一點。 – nograpes
@nograpes只是一個猜測。 –