【发布时间】:2021-09-03 04:03:16
【问题描述】:
我有以下数据框,
df = data.frame("title1" = c("This is the Title", "This is a longer Title"),
"title2" = c("This is the Title", "This is an even longer Title"),
"url" = c("google.com","google.com"),
"idx" = c(1,2),
"insertion" = c("word1","word2")
)
看起来像这样:
我想要实现的是,对于每一行,来自列 insertion 的单词被插入到列 title1 中列 idx 的位置的字符串中。这是我的方法:
df$title1 <- sapply(df$title1, function(x) unlist(strsplit(x, " ")))
df$title1 <- sapply(df$title1, function(x) append(x, df$insertion, after = df$idx))
df$title1 <- sapply(df$title1, function(x) paste(x, collapse = ' '))
然而,这会返回一个警告,并且只使用列 idx 和 insertion 的第 1 行来计算列 title1。如何获得所需的行为?
【问题讨论】:
-
嗨,非常感谢您提示我正确的方向。您的代码不能动态工作(当 idx 列更改时)。我想这是由于这部分:paste('\\1\\2', y)。我在下面发布了最适合我的解决方案。
-
df = data.frame("title1" = c("This is the Title", "This is a longer Title"), "title2" = c("This is the Title", "这是一个更长的标题"), "url" = c("google.com","google.com"), "idx" = c(2,3), "insertion" = c("word1"," word2") )
-
删掉第二行的第一个单词。
-
感谢您提供额外的示例。正则表达式需要稍微改进。
-
现在看起来不错,谢谢!