【问题标题】:apply agrep function across every row of a data frame对数据框的每一行应用 agrep 函数
【发布时间】:2020-08-20 21:30:51
【问题描述】:

我有以下代码,但我收到了消息;

argument 'pattern' has length > 1 and only the first element will be used.

如何编写一个函数,以便将test_master_raw2$match_work 应用于数据框的每一行?

test_master_raw2$match_work <- agrep(test_master_raw2$file_name4,meta2$file_name4, ignore.case = TRUE,value = TRUE,max.distance = .01)

【问题讨论】:

  • 您应该提供一个可重现的示例。将您的数据放在问题中。 dput(test_master_raw2)
  • 关于 SO(尤其是在 R 中)的问题如果是可重复的和独立的,则效果会更好。我的意思是包括尝试的代码(请明确说明非基础包)、示例代表性数据(可能通过dput(head(x)) 或以编程方式构建数据(例如data.frame(...)),可能在set.seed(1) 之后随机生成),可能是实际输出(带有逐字错误/警告)与预期输出。参考:stackoverflow.com/q/5963269minimal reproducible examplestackoverflow.com/tags/r/info

标签: r dplyr stringr


【解决方案1】:

你可以试试这个。为了示例,我编了一个df

df <- data.frame(x = c("hello", "halo"),
                 y = c("helo", "ahlu"))

apply(df, 1, function(x) agrep(x[1], x[2], ignore.case = TRUE, value = TRUE, max.distance = .01))

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2016-02-04
    相关资源
    最近更新 更多