Home > database >  Lookup table for 2 pairs of data in R
Lookup table for 2 pairs of data in R

Time:02-17

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 ))

CodePudding user response:

Is this what you are hoping to achieve? If not, what changes to the output table do you want to make?

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  11  3.95
32  12  3.9
32  13  3.85
32  14  3.8
32  15  3.75
32  16  3.7", header = TRUE)


left_join(A1, B1, by = "T", keep = TRUE)
#>    T.x RH.x T.y RH.y Rate
#> 1   32    0  32   11 3.95
#> 2   32    0  32   12 3.90
#> 3   32    0  32   13 3.85
#> 4   32    0  32   14 3.80
#> 5   32    0  32   15 3.75
#> 6   32    0  32   16 3.70
#> 7   45   12  NA   NA   NA
#> 8   36   15  NA   NA   NA
#> 9   29   25  NA   NA   NA
#> 10  28   35  NA   NA   NA

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

CodePudding user response:

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

  • Related