rdplyrtidyrdata-manipulationtidy

how to do vlookup in R


I have a data set like below

  first_column <- c("value_1", "value_2", "value_3")
second_column <- c("keep", "remove", "remove")

main.df <- data.frame(first_column, second_column); main.df

first_column second_column
1      value_1          keep
2      value_2        remove
3      value_3        remove

first_column <- c("value_1", "value_2", "value_3")
column1  <- c("si", "no", "na")
column2 <-  c(5, 2, 3)

vlookupdf  <- data.frame(first_column, column1, column2);  vlookupdf 
first_column column1 column2
1      value_1      si       5
2      value_2      no       2
3      value_3      na       3

I would like to remove the rows from main.df if vlookupdf column1 is eather no or na

Expected Answer from main.df

first_column second_column
1      value_1          keep

many thanks in advance.


Solution

  • You could left_join your dataframe and filter based on your criteria like this:

    library(dplyr)
    main.df |>
      left_join(vlookupdf, by = 'first_column') |>
      filter(!(column1 %in% c("no", 'na'))) |>
      select(first_column, second_column) # if necessary
    #>   first_column second_column
    #> 1      value_1          keep
    

    Created on 2023-05-05 with reprex v2.0.2