简体   繁体   中英

Lookup table for 2 pairs of data in R

I did a lookup table for 2 pairs of data in R

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

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?

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

Look up Table: 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

NA because not mathced with Lookup Table

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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