【问题标题】:How to match a variable to a row in a dataframe and select the variable in a different column on the same row?如何将变量与数据框中的行匹配并在同一行的不同列中选择变量?
【发布时间】:2019-10-01 21:08:39
【问题描述】:

我有一堆聊天记录,并设法从中提取电子邮件地址并将域“@bacon.edu”分开我有一个与类别名称匹配的域列表。

基本上我想将变量与第 2 列中的一行匹配,从第 1 列中提取类别名称。

我应该提到,目前所有内容都格式化为因素,但这可能会改变。

在这个例子中 d1 = "bacon.edu" 和名单是一个这样设置的数据框:

d1 = "bacon.edu"

Workplace Name  Email List
Pancake         @bac.edu
Test place      @toe.edu
superworld      @bacon.edu
monkey gym      @aclu.edu
toaster oven    @yoyo.edu

目标是在第 3 行找到培根,从第 1 列第 3 行创建一个变量(所以 abc = "superworld"),但我很难找到开始的变量。

我试过了:

which(d1, namelist$Email.List)
which(namelist$Email.List == d1)
which(grep
match(d1, namelist$Email.list)
which(grepl("bacon.edu, namelist$Email.List

遗憾的是,我不记得所有错误或它们的来源,但它们包括:

integer(0)
object class not logical
level sets of factors are different.

我感觉到已删除失败的尝试。我敢肯定它很简单,我觉得不好问,但任何帮助将不胜感激!

【问题讨论】:

    标签: r dataframe dplyr grepl


    【解决方案1】:

    我们可以使用grep

    namelist$1Workplace Name`[grep(d1, namelist$`Email List`)]
    

    【讨论】:

      猜你喜欢
      • 2021-09-14
      • 2016-04-14
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多