【问题标题】:Compare dataframe column to another dataframe column将数据框列与另一个数据框列进行比较
【发布时间】:2016-03-11 14:07:32
【问题描述】:

我有一个包含页面路径的数据框列(我们称之为 A):

pagePath
/text/other_text/123-string1-4571/text.html
/text/other_text/string2/15-some_other_txet.html
/text/other_text/25189-string3/45112-text.html
/text/other_text/text/string4/5418874-some_other_txet.html
/text/other_text/string5/text/some_other_txet-4157/text.html
/text/other_text/123-text-4571/text.html
/text/other_text/125-text-471/text.html

我还有另一个字符串数据框列,我们称之为 (B)(两个数据框不同,它们的行数不同)。

这是我在数据框 B 中的列的示例:

names
string1
string11
string4
string3
string2
string10
string5
string100

我要做的是检查我的页面路径 (A) 是否包含来自其他数据框 (B) 的字符串。

我遇到了困难,因为我的两个数据框长度不同,而且数据没有组织。

预期输出

我希望得到这个输出结果:

 pagePath                                                  names     exist
/text/other_text/123-string1-4571/text.html                string1   TRUE
/text/other_text/string2/15-some_other_txet.html           string2   TRUE
/text/other_text/25189-string3/45112-text.html             string3   TRUE
/text/other_text/text/string4/5418874-some_other_txet.html string4   TRUE
/text/string5/text/some_other_txet-4157/text.html          string5   TRUE
/text/other_text/123-text-4571/text.html                     NA      FALSE
/text/other_text/125-text-471/text.html                      NA      FALSE

如果我的问题需要进一步澄清,请提及。

【问题讨论】:

  • A$pagePath[B$pagePath] ?
  • @mtoto 我想检查我的 A 列是否包含来自 B 列的数据(A 列和 B 列中的数据不相等,但有时 A 列包含 B 列中的数据)
  • @sarah 我认为你应该检查一下 R 中的匹配功能。
  • 请展示B 的示例以及您的预期输出。
  • @mtoto 我编辑了我的问题并写了一个 B 的例子。

标签: r apply grepl


【解决方案1】:

我们可以使用grepl() 生成exist

# Collapse B$names into one string with "|" 
onestring <- paste(B$names, collapse = "|") 

# Generate new column
A$exist <- grepl(onestring, A$pagePath)

【讨论】:

  • 我试过你的解决方案,它给了我这个错误:Error in grepl(onestring, A$pagePath) : invalid regular expression。我认为这是由于真实数据中存在- 字符。
  • 确保你的列是character
  • 现在我面临另一个错误:`无效的正则表达式,原因内存不足`
  • 您的数据有多大?尽管如此,它应该为您提供正确的输出以及发布的示例数据。
  • 名称约为 11000 字符串,页面路径约为 37000 :(
【解决方案2】:

不太好,因为包含一个 for 循环:

names <- rep(NA, length(A$pagePath))
exist <- rep(FALSE, length(A$pagePath))

for (name in B$names) {
  names[grep(name, A$pagePath)] <- name
  exist[grep(name, A$pagePath)] <- TRUE
}

【讨论】:

  • 感谢@dotton,我正在尝试您的解决方案。如果我的情况没问题,我会尝试将循环转换为应用函数:)
【解决方案3】:

我们可以使用来自stringr 包的str_extract_all,但NA 被替换为character(0),所以我们必须更改它

df$names <- as.character(str_extract_all(df$pagePath, "string[0-9]+"))
df$exist <- df$names %in% df1$names
df[df=="character(0)"] <- NA
df
#                                                 pagePath       names   exist
#1                  /text/other_text/123-string1-4571/text.html string1  TRUE
#2             /text/other_text/string2/15-some_other_txet.html string2  TRUE
#3               /text/other_text/25189-string3/45112-text.html string3  TRUE
#4   /text/other_text/text/string4/5418874-some_other_txet.html string4  TRUE
#5 /text/other_text/string5/text/some_other_txet-4157/text.html string5  TRUE
#6                     /text/other_text/123-text-4571/text.html    <NA> FALSE
#7                      /text/other_text/125-text-471/text.html    <NA> FALSE

数据

dput(df)
structure(list(pagePath = structure(c(1L, 5L, 4L, 7L, 6L, 2L, 
3L), .Label = c("/text/other_text/123-string1-4571/text.html", 
"/text/other_text/123-text-4571/text.html", "/text/other_text/125-text-471/text.html", 
"/text/other_text/25189-string3/45112-text.html", "/text/other_text/string2/15-some_other_txet.html", 
"/text/other_text/string5/text/some_other_txet-4157/text.html", 
"/text/other_text/text/string4/5418874-some_other_txet.html"), class = "factor")), .Names = "pagePath", class = "data.frame", row.names = c(NA, 
-7L))
dput(df1)
structure(list(names = structure(c(1L, 4L, 7L, 6L, 5L, 2L, 8L, 
3L), .Label = c("string1", "string10", "string100", "string11", 
"string2", "string3", "string4", "string5"), class = "factor")), .Names = "names", class = "data.frame", row.names = c(NA, 
-8L))

【讨论】:

  • 我的问题是两列的长度不一样
  • 它应该可以工作。我使用的数据与您共享的数据相同
  • @sarah 您也应该将数据框转换为字符,因为路径因素没有意义。
  • 谢谢。我会试试这个,我会给出我的反馈。
【解决方案4】:

这是使用 apply 的一种方式:

df$exist <- apply( df,1,function(x){as.logical(grepl(x[2],x[1]))} )

【讨论】:

    猜你喜欢
    • 2020-08-13
    • 2018-03-14
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多