简体   繁体   English

R中2对数据的查找表

[英]Lookup table for 2 pairs of data in R

I did a lookup table for 2 pairs of data in R我为R中的2对数据做了一个查找表

Data test:数据测试:

T   RH
32  0
45  12
36  15
29  25
28  35  

Lookup Tabel (only some listed here):查找表(这里只列出了一些):

32  11  3.95
32  12  3.9
32  13  3.85
32  14  3.8
32  15  3.75
32  16  3.7

but the data that does not match with the lookup table is dropped in the output但是在 output 中丢弃了与查找表不匹配的数据

how to make the output have the same size of rows and with NA in the rows of output that are not matched to the lookup table?如何使 output 具有相同大小的行,并在 output 的行中使用 NA 与查找表不匹配?

btw my code is this:顺便说一句,我的代码是这样的:

A1 <- data-tes
B1 <- lookup

library(dplyr)

anti_join(anti_join(B1, A1 )) %>%
  select(-Rate)

TCI2 <- bind_rows(inner_join(B1, A1 ))

Sorry I mean like this:对不起,我的意思是这样的:

Data test: T RH 32 0 45 12 36 15 29 25 28 35数据测试:T RH 32 0 45 12 36 15 29 25 28 35

Look up Table: T RH Rate 32 0 5 45 12 4 70 15 3 80 25 2查表:T RH Rate 32 0 5 45 12 4 70 15 3 80 25 2

Output desired: T RH Rate 32 0 5 45 12 4 36 15 NA 29 25 NA 28 35 NA Output 所需:T RH 率 32 0 5 45 12 4 36 15 NA 29 25 NA 28 35 NA

NA because not mathced with Lookup Table NA 因为没有使用查找表进行数学运算

Edit:编辑:

Thanks for updating your question with more details, but please edit your original question to include the new details rather than adding them in a comment.感谢您使用更多详细信息更新您的问题,但请编辑您的原始问题以包含新的详细信息,而不是将它们添加到评论中。

It looks like you want a 'left join' (as suggested by @Maurits Evers in the comment above), eg看起来你想要一个“左连接”(正如@Maurits Evers 在上面的评论中所建议的那样),例如

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

A1 <- read.table(text = "T   RH
32  0
45  12
36  15
29  25
28  35 ", header = TRUE)

B1 <- read.table(text = "T   RH   Rate
32  0     5
45  12    4
70  15    3
80  25    2", header = TRUE)

desired_outcome <- read.table(text = "T   RH  Rate
32  0     5
45  12    4
36  15    NA
29  25    NA
28  35    NA", header = TRUE)

left_join(A1, B1)
#> Joining, by = c("T", "RH")
#>    T RH Rate
#> 1 32  0    5
#> 2 45 12    4
#> 3 36 15   NA
#> 4 29 25   NA
#> 5 28 35   NA
desired_outcome
#>    T RH Rate
#> 1 32  0    5
#> 2 45 12    4
#> 3 36 15   NA
#> 4 29 25   NA
#> 5 28 35   NA

Created on 2022-02-17 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-02-17

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM