东西供你考虑的情况下,要与结果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只是一个猜测。 –