我有一个数据框:
x <- data.frame(a = letters[1:7], b = letters[2:8],
c = c("bla bla [ text1 ]", "bla bla [text2]", "how how [text3 ]",
"wow wow [ text4a ] [ text4b ]", "ba ba [ text5a ][ text5b]",
"my text A", "my text B"), stringsAsFactors = FALSE)
x
我想根据其中两个方括号[...]
之间的内容拆分列c。如果列c只包含一组方括号,我希望字符串转到下一列。如果列c包含由[
和]
包围的两组字符串,我只希望最后[
]
之间的字符串转到新列。
我是这样做的。这似乎很复杂,我正在使用循环。是否有可能以更简约的方式做到这一点?
library(stringr)
# Counting number of square brackets "[" in column c:
sqrbrack_count <- str_count(x$c, pattern = '\\[')
# Creating a new column:
x$newcolumn <- NA
for(i in 1:nrow(x)){ # looping through rows of x
if(sqrbrack_count[i] == 0) next # do nothing of 0 square brackets
minilist <- str_split_fixed(x[i, "c"], pattern = '\\[', n = Inf) # split string
if(sqrbrack_count[i] == 1) { # if there is only one square bracket "["
x[i, "c"] <- minilist[1]
x[i, "newcolumn"] <- minilist[2]
} else { # if there are >1 square bracket "["
x[i, "c"] <- paste(minilist[1:2], collapse = "+")
x[i, "newcolumn"] <- minilist[3]
}
}
# Replacing renmaning square brackets we don't need anymore:
x$c <- str_replace(x$c, pattern = " \\]", replacement = "")
x$c <- str_replace(x$c, pattern = "\\]", replacement = "")
x$newcolumn <- str_replace(x$newcolumn, pattern = " \\]", replacement = "")
x$newcolumn <- str_replace(x$newcolumn, pattern = "\\]", replacement = "")
x
答案 0 :(得分:1)
以下代码更短,可能更容易理解,因为大多数复杂逻辑都发生在两行中。我在这两行上面添加了评论,我认为其余的都是不言自明的。
library(plyr)
# find all strings between characters '[' and ']'
strmatches = lapply(1:nrow(x), function(y) {regmatches(x$c[y], gregexpr("(?<=\\[).*?(?=\\])", x$c[y], perl=T))[[1]]})
# parse these to a dataframe called 'new_cols'
new_cols = rbind.fill(lapply(strmatches, function(x) {as.data.frame(t(x),stringsAsFactors = F)}))
df = cbind(x,new_cols)
df$c = gsub("\\[.*$", "", x$c) # only keep everything before '['
df$c[!is.na(df$V2)] = paste0(df$c[!is.na(df$V2)], '+',df$V1[!is.na(df$V2)])
df$V1[!is.na(df$V2)] = df$V2[!is.na(df$V2)]
df$V2=NULL
colnames(df)[colnames(df)=="V1"]="newcolumn"
输出:
a b c V1
1 a b bla bla text1
2 b c bla bla text2
3 c d how how text3
4 d e wow wow + text4a text4b
5 e f ba ba + text5a text5b
6 f g my text A <NA>
7 g h my text B <NA>
希望这有帮助!
PS:这符合您的预期输出,但您可能希望在其中添加一些str_trim。