【问题标题】:How to use multiple columns as inputs for apply function in R如何在 R 中使用多列作为应用函数的输入
【发布时间】: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 = ' '))

然而,这会返回一个警告,并且只使用列 idxinsertion 的第 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") )
  • 删掉第二行的第一个单词。
  • 感谢您提供额外的示例。正则表达式需要稍微改进。
  • 现在看起来不错,谢谢!

标签: r dataframe apply


【解决方案1】:

一个基本的 R 正则表达式解决方案 -

mapply(function(x, y, z) 
  sub(sprintf('((?:\\w+\\s){%d})(\\w+)', x), paste('\\1\\2', y), z), 
  df$idx - 1, df$insertion, df$title1)

#[1] "This word1 is the Title"      "This is word2 a longer Title"

我们从每个字符串中提取idx - 第一个单词并将其与insertion 单词一起粘贴。

【讨论】:

    【解决方案2】:

    动态工作的解决方案:

    mapply(function(x, y, z)
      paste(append(unlist(strsplit(z, " ")), y, after = x), collapse = " "),
      df$idx, df$url , df$title1)
    

    感谢 Ronak Shah 的提示!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      相关资源
      最近更新 更多