这是我的数据
viewpager
我想得到以下输出。 基本上是在数字之后重复的地方,我希望它在下面的一行重复
df <- structure(list(V1 = structure(c(1L, 5L, 5L, 1L, 5L, 5L, 5L, 5L,
NA, NA, NA, NA, 4L, 2L, 3L), .Label = c("1 x Bruit (U)", "1 x Bruit (U) 1 x TAMAN (M)",
"1 x Bruit (U) 2 x TAMAN (M)", "1 x TAMAN (M) 2 x TAMAN (M)",
"2 x Bruit (U)"), class = "factor"), V2 = structure(c(1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("BUX1_T10963",
"BUX1_T10964", "BUX1_T10965"), class = "factor")), .Names = c("V1",
"V2"), class = "data.frame", row.names = c(NA, -15L))
答案 0 :(得分:3)
我们可以使用dplyr
和tidyr
。关键是使用separate_rows
并在sep
参数中指定正则表达式。请参阅此(http://www.rexegg.com/regex-lookarounds.html)以了解有关正则表达式中前瞻和后瞻的更多信息。
library(dplyr)
library(tidyr)
df2 <- df %>%
separate_rows(V1, sep = "(?<=\\))\\s(?=[0-9]+)") %>%
select(names(df))
df2
# V1 V2
# 1 1 x Bruit (U) BUX1_T10963
# 2 2 x Bruit (U) BUX1_T10963
# 3 2 x Bruit (U) BUX1_T10963
# 4 1 x Bruit (U) BUX1_T10964
# 5 2 x Bruit (U) BUX1_T10964
# 6 2 x Bruit (U) BUX1_T10964
# 7 2 x Bruit (U) BUX1_T10964
# 8 2 x Bruit (U) BUX1_T10964
# 9 <NA> BUX1_T10964
# 10 <NA> BUX1_T10964
# 11 <NA> BUX1_T10964
# 12 <NA> BUX1_T10964
# 13 1 x TAMAN (M) BUX1_T10965
# 14 2 x TAMAN (M) BUX1_T10965
# 15 1 x Bruit (U) BUX1_T10965
# 16 1 x TAMAN (M) BUX1_T10965
# 17 1 x Bruit (U) BUX1_T10965
# 18 2 x TAMAN (M) BUX1_T10965