R等于array_pop以合并两个字符数据帧?

时间:2019-02-26 00:33:04

标签: r dplyr

我对R数据帧中缺少等效的数组弹出感到困惑。我检查了dplyr,没有什么比它更合适了。

这里是数据框1,它只不过是单词,数字和组合词。目标是按顺序将问题从数据帧2匹配到数据帧1的内容,当不再有匹配项时停止。

<!DOCTYPE html>
<html>
<head>
  <title>Please Participate in Our Survey!</title>
  <link href="https://fonts.googleapis.com/css?family=Oswald:300,700|Varela+Round" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
    <ul>
<div class="steven">
      <a class="ninja" href="https://cheese.com/" target="_blank"><img src="http://www.the-green-pantry.com/image/760/390/_data/uploads/huelsen.gif" width="65px" height="65px"></a>
      <a href="#home"><li>Home</li></a>
      <a href="#rides"><li>Rides</li></a>
      <a href="#pricing"><li>Pricing</li></a>
      <a href="#open hours"><li>Open Hours</li></a>
       </div>
    </ul>
</header>
  <div class="welcome">
    <h1>Welcome to Legume land, where your dreams become reality!</h1>
    <p>Join us today!</p>
  </div>
  <div class="rideimages">
  <ul class="ulride">
  <img class="1" src="Images/ride1.jpg" width="250px" height="250px">
  <img class="2" src="Images/ride2.jpg" width="250px" height="250px">
  <img class="3" src="Images/ride3.jpg" width="250px" height="250px">
  <img class="4" src="Images/ride4.jpg" width="250px" height="250px">
  </ul>
  </div>
  
  <footer>
  <div class="socials">
  <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank"><img src="Images/Youtube.svg" width="60" height="60"></a>
  <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank"><img src="Images/Instagram.svg" width="60" height="60"></a>
  <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank"><img src="Images/Facebook.svg" width="60" height="60"></a>
  <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank"><img src="Images/Twitter.svg" width="60" height="60"></a>
  </div>
</footer>
</body>
</html>

dplyr的left_join似乎是合乎逻辑的选择,但它不起作用,而是吐出一些看起来更像是完全联接的东西。

DF1
Word    Volume    Group
tuna    100    fish
tuna fish    90    fish
chicken eggs    90    eggs
ostrich eggs    80    eggs

DF2
Group_Word    Question
fish    how big is a tuna fish?
fish    what's the best tasting tuna fish?
eggs    how many eggs does a chicken lay per day?
eggs    how to poach eggs?
eggs    do eggs need to be refrigerated?
beef    what's the best flavor of beef

这是预期/预期结果:

blendeddf <- left_join(DF1,DF2, by =c("Group" = "Group_Word"))

这是实际结果:

DF1
Word    Volume    Group
tuna    100    fish    how big is a tuna fish?
tuna fish    90    fish    what's the best tasting tuna fish?
chicken eggs    90    eggs    how many eggs does a chicken lay per day?
ostrich eggs    80    eggs    how to poach eggs?

如果我在PHP中执行此操作,则先是array_match,然后是array_pop才能删除项目(以避免重复),但是我不知道如何在R中完成此操作。

已发送代码:

Word Volume Group                                  Question
1          tuna    100  fish                   how big is a tuna fish?
2          tuna    100  fish        what's the best tasting tuna fish?
3     tuna fish     90  fish                   how big is a tuna fish?
4     tuna fish     90  fish        what's the best tasting tuna fish?
5  chicken eggs     90  eggs how many eggs does a chicken lay per day?
6  chicken eggs     90  eggs                        how to poach eggs?
7  chicken eggs     90  eggs          do eggs need to be refrigerated?
8  ostrich eggs     80  eggs how many eggs does a chicken lay per day?
9  ostrich eggs     80  eggs                        how to poach eggs?
10 ostrich eggs     80  eggs          do eggs need to be refrigerated?

1 个答案:

答案 0 :(得分:2)

您可以通过在每个组中添加一个额外的列来标记数字来获得预期的结果:

DF1 = DF1 %>%
    group_by(Group) %>%
    mutate(GroupNum = 1:n())
DF2 = DF2 %>%
    group_by(Group_Word) %>%
    mutate(GroupNum = 1:n())

DF1 %>%
    left_join(DF2, by = c("Group" = "Group_Word", "GroupNum"))

输出:

# A tibble: 4 x 5
# Groups:   Group [2]
  Word         Volume Group GroupNum Question                                 
  <chr>         <int> <chr>    <int> <chr>                                    
1 tuna            100 fish         1 how big is a tuna fish?                  
2 tuna fish        90 fish         2 what's the best tasting tuna fish?       
3 chicken eggs     90 eggs         1 how many eggs does a chicken lay per day?
4 ostrich eggs     80 eggs         2 how to poach eggs?