如果您想要具有结果的 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 中显示它们时。