【问题标题】:have diff_data show the full comparison of names from two lists让 diff_data 显示两个列表中名称的完整比较
【发布时间】:2021-11-09 04:45:47
【问题描述】:

我对 R 有点陌生。我正在尝试比较来自两个不同数据集的两列。列中的信息是几种植物物种的科学名称列表。我正在尝试将一个数据集的列表与另一个数据集进行比较,以查看其中一个或另一个缺少什么名称。

我一直无法找到可以隔离这两列的代码。我尝试了daff 包中的diff_data 并得到了一个很好的比较列表,但该列表停止了并且没有继续比较。如何让 diff_data 显示完整列表?

diff_data(List1,List2) Daff 比较:“List1”与“List2” 前 6 条和后 6 条补丁线: A:A B:B
@@ 家族学名
1:1 家庭1 姓名1
2:2 -> 家族 1 名称 2 3:- --- Family2 Name3
4:- --- Family2 Name4
5:3 -> Family2 Name5
... ... ... ...
994:- --- Family Viola purpurea subsp.紫红色 995:- --- VIOLACEAE Viola quercectorum
996:- --- VIOLACEAE Viola sheltonii
997:743 -> VITACEAE Vitis girdiana
998:- --- WOODSIACEAE Cystopteris fragilis
999:- --- ZYGOPHYLLACEAE Tribulus terrestris

【问题讨论】:

  • 如果您保存diff_data 函数的结果(例如result <- diff_data(List1,List2)),是否可以解决您的问题?

标签: r


【解决方案1】:

不是 100% 确定您要的是什么,但也许这个使用 tidyverse 函数的示例会有所帮助:

# Load libraries
library(tidyverse)

# Load example dataset from the 'tidyverse' package
data(fruit)

# Set seed to allow reproducible sampling
set.seed(123)

# Create two dataframes by sampling the 'fruit' dataset
df1 <- data.frame(fruit = sample(fruit, 20, replace = FALSE),
                  values = 1:20)
df2 <- data.frame(fruit = sample(fruit, 40, replace = FALSE),
                  values = rnorm(40))

# Find fruit common to both
dplyr::inner_join(df1, df2, by = "fruit")
#>           fruit values.x     values.y
#> 1           fig        1 -0.006198262
#> 2    ugli fruit        2 -0.778997240
#> 3       kumquat        8  1.242918775
#> 4     tamarillo        9 -0.900791751
#> 5          date       10  1.685887244
#> 6      rambutan       11 -0.520869344
#> 7  blood orange       13 -1.318937604
#> 8   dragonfruit       14  0.619283535
#> 9    blackberry       15 -0.886436716
#> 10   star fruit       16 -1.048975504

# Find fruit in df1 but not in df2
dplyr::anti_join(df1, df2, by = "fruit")
#>           fruit values
#> 1     nectarine      3
#> 2    cantaloupe      4
#> 3        quince      5
#> 4    kiwi fruit      6
#> 5      mulberry      7
#> 6  passionfruit     12
#> 7        raisin     17
#> 8     pineapple     18
#> 9    cloudberry     19
#> 10        guava     20

# Find fruit in df2 but not in df1
dplyr::anti_join(df2, df1, by = "fruit")
#>                fruit      values
#> 1          tangerine -0.16393097
#> 2         strawberry -0.93438506
#> 3          cherimoya  0.39370865
#> 4         goji berry  0.40363146
#> 5             jujube  0.02884391
#> 6            currant -0.43212979
#> 7             durian  1.68987252
#> 8          persimmon  1.22839278
#> 9              olive  0.27602348
#> 10        redcurrant  1.62320252
#> 11       huckleberry -1.07006823
#> 12             grape -0.24168977
#> 13        elderberry -0.46820048
#> 14       bell pepper -0.77297823
#> 15      blackcurrant  2.14991934
#> 16        breadfruit -1.33435363
#> 17      canary melon  0.49587048
#> 18        clementine  1.23397624
#> 19        gooseberry  0.63436212
#> 20 purple mangosteen  0.41202227
#> 21       pomegranate  0.79358531
#> 22            pomelo -0.15241063
#> 23         cranberry -0.22889582
#> 24       salal berry -0.73502616
#> 25            lychee -1.42768578
#> 26            cherry -0.68570685
#> 27            feijoa -0.27933353
#> 28          bilberry -0.78273028
#> 29         raspberry -0.37480009
#> 30          cucumber -0.31939381

reprex package (v2.0.1) 于 2021 年 9 月 14 日创建

【讨论】:

  • 感谢这个想法,我会试试的。我最终通过使用 intersect 并添加 $ 来指定一列来使该功能正常工作,但由于某种原因,它没有找到几个匹配项,所以我接下来会尝试这个
猜你喜欢
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多