我有这样的数据
df1 <- structure(list(V1 = structure(c(1L, 2L, 3L, 5L, 4L), .Label = c("A0A061ACH4;Q95Q10;Q9U1W6",
"A0A061ACL3;Q965I6;O76618", "A0A061ACR1;Q2XN02;F5GUA3;Q22498",
"A0A061AL01", "H2FLH3;H2FLH2;A0A061ACT3;A0A061AE24;Q23551-2;Q23551;Q23551-4;Q23551-3;Q23551-5"
), class = "factor"), V2 = c(1L, 5L, 100L, 645L, 11L), V3 = c(67L,
10L, 33L, 99L, 10L), V4 = c(7L, 16L, 0L, 1L, 5L)), .Names = c("V1",
"V2", "V3", "V4"), class = "data.frame", row.names = c(NA, -5L
))
我想要的是将所有字符串保持在彼此之下并复制每行前面的值,在该行的每个字符串前面
预期的输出应该是这样的
output <- structure(list(V1 = structure(c(1L, 18L, 20L, 2L, 19L, 19L, 10L,
3L, 17L, 7L, 11L, 9L, 8L, 4L, 5L, 13L, 12L, 15L, 14L, 16L, 6L
), .Label = c("A0A061ACH4", "A0A061ACL3", "A0A061ACR1", "A0A061ACT3",
"A0A061AE24", "A0A061AL01", "F5GUA3", "H2FLH2", "H2FLH3", "O76618",
"Q22498", "Q23551", "Q23551-2", "Q23551-3", "Q23551-4", "Q23551-5",
"Q2XN02", "Q95Q10", "Q965I6", "Q9U1W6"), class = "factor"), V2 = c(1L,
1L, 1L, 5L, 5L, 5L, 5L, 100L, 100L, 100L, 100L, 645L, 645L, 645L,
645L, 645L, 645L, 645L, 645L, 645L, 11L), V3 = c(67L, 67L, 67L,
10L, 10L, 10L, 10L, 33L, 33L, 33L, 33L, 99L, 99L, 99L, 99L, 99L,
99L, 99L, 99L, 99L, 10L), V4 = c(7L, 7L, 7L, 16L, 16L, 16L, 16L,
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L)), .Names = c("V1",
"V2", "V3", "V4"), class = "data.frame", row.names = c(NA, -21L
))
例如,如果我们查看df1第一行,它看起来像这样
A0A061ACH4;Q95Q10;Q9U1W6 1 67 7
然后我分开琴弦;并将它们放在彼此之下并复制它们前面的值。所以对于df1的第一行,我会有
A0A061ACH4 1 67 7
Q95Q10 1 67 7
Q9U1W6 1 67 7
答案 0 :(得分:2)
可以使用separate_rows
tidyr
来完成此操作
library(tidyr)
separate_rows(df1, V1, sep=";")
或者使用cSplit
选项<{1}}
long
或使用其他library(splitstackshape)
cSplit(df1, 'V1', ';', 'long')
选项
base R
如果我们只需要第一个lst <- strsplit(as.character(df1$V1), ";")
cbind(V1= unlist(lst), df1[rep(1:nrow(df1), lengths(lst)),-1])
之前的子字符串,请使用;
匹配模式sub
后跟字符,直到字符串结尾(;
)和将其替换为空白(.*
)。
""
答案 1 :(得分:1)
这是&#34;基地&#34;方式。
# for each line...
out <- do.call(rbind, apply(df1, MARGIN = 1, FUN = function(x) {
# split by ; and...
do.call(rbind, sapply(unlist(strsplit(x[1], ";")), FUN = function(y, y2) {
# ... append the rest of the columns to individual element
c(y, y2)
}, simplify = FALSE, y2 = x[2:4]))
}))
rownames(out) <- NULL
out <- as.data.frame(out)
out
V1 V2 V3 V4
1 A0A061ACH4 1 67 7
2 Q95Q10 1 67 7
3 Q9U1W6 1 67 7
4 A0A061ACL3 5 10 16
5 Q965I6 5 10 16
6 O76618 5 10 16
7 A0A061ACR1 100 33 0
8 Q2XN02 100 33 0
9 F5GUA3 100 33 0
10 Q22498 100 33 0
11 H2FLH3 645 99 1
12 H2FLH2 645 99 1
13 A0A061ACT3 645 99 1
14 A0A061AE24 645 99 1
15 Q23551-2 645 99 1
16 Q23551 645 99 1
17 Q23551-4 645 99 1
18 Q23551-3 645 99 1
19 Q23551-5 645 99 1
20 A0A061AL01 11 10 5