![](/img/trans.png)
[英]Select data from a dataframe and then create new df with 0 and 1 by matched values in R
[英]Select row which matched to the words from another dataframe (R)
我對搜索這個問題的關鍵字有點困惑。 你能幫我或給我任何建議來解決 R 中的這個問題嗎
我有這樣的 dataframe A
list_fruit
<chr>
Orange
Grape
Watermelon
Mango
我想把這個 dataframe 與另一個數據集 B 匹配
這是數據集 B
Fruit Number Price
<chr> <int> <int>
Banana 56 25
Watermelon 5 100
Pineapple 96 150
Guava 47 20
Orange 3 120
因此將選擇“西瓜”和“橙色”的行並將其收集到新的 dataframe
Fruit Number Price
<chr> <int> <int>
Watermelon 5 100
Orange 3 120
謝謝先進
我認為您正在尋找inner_join
。 我們將您的第一個數據框稱為 df1 和第二個 df2。 然后:
library(tidyverse)
df1 %>%
inner_join(df2, by = c("list_fruit" = "Fruit"))
#> list_fruit Number Price
#> 1 Orange 3 120
#> 2 Watermelon 5 100
由代表 package (v2.0.1) 於 2022 年 1 月 27 日創建
從幫助頁面:
inner_join():包括 x 和 y 中的所有行。
在這里,這意味着您通過 df1 中的list_fruit
和 df 2 中的Fruit
匹配列。您通過將 df2 中的列添加到 df1 的右側來加入數據框。 而且你只保留你得到匹配的行。
df1 <- structure(list(list_fruit = c("Orange", "Grape", "Watermelon",
"Mango")), class = "data.frame", row.names = c(NA, -4L))
df2 <- structure(list(Fruit = c("Banana", "Watermelon", "Pineapple",
"Guava", "Orange"), Number = c(56L, 5L, 96L, 47L, 3L), Price = c(25L,
100L, 150L, 20L, 120L)), class = "data.frame", row.names = c(NA,
-5L))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.