Replace values by conditions from different df

Hi , I try to replace some values from one data frame (lets call it: "xrt") by compare to another data frame (lets call it:"code names") and if they match, replace old value in "xrt " to new values from "code names" in another column.
So let's I show:
That output of my first df - xrt.
I have column with 1777 row with codes (total levels is 68)

A tibble: 1,777 x 2
XRT_code_small Date_XRT

1 77261 2015-07-30
2 77261 2015-08-02
3 77295 2015-07-23
4 77401 2015-08-03
5 77401 2015-08-05
6 77401 2015-08-06
7 77401 2015-08-09
8 77401 2015-08-10
9 77401 2015-08-11
10 77401 2015-08-12
... with 1,767 more rows

Another df - code names:

A tibble: 68 x 2
Input_Code Name_code

1 77261 General_Office
2 77280 General_Office
3 77295 General_Office
4 77401 General_Office
5 81781 General_Office
6 77751 NA
7 77753 NA
8 77900 WBRT 3D
9 77902 NA
10 77903 Brain 3D
...with 58 more rows

So I want compare columnt XRT_code from "xrt" to Input_Code from "code names" and if it match - replase with name from column Namec_code respectively.

I trying case_when function, but unsuccessfully.
Join_left good soltion (Thanks for user mishabalyasin) but again is at really write any function with statement IF?

Thank you

I think, it'll be easier to help you if you provide reproducible example. Please have a look at this guide, to see how to create one:

In general, problems like yours can be solved with using appropriate join.

This is very confusing, I'm going to make a reprex so we can all work on the same code, Can you explain why this doesn't work for you?

xrt <- data.frame(
  stringsAsFactors = FALSE,
    XRT_code_small = c(77261L,77261L,77295L,77401L,
                       77401L,77401L,77401L,77401L,77401L,77401L),
          Date_XRT = c("2015-07-30","2015-08-02",
                       "2015-07-23","2015-08-03","2015-08-05","2015-08-06",
                       "2015-08-09","2015-08-10","2015-08-11","2015-08-12")
)

code_names <- data.frame(
  stringsAsFactors = FALSE,
        Input_Code = c(77261L,77280L,77295L,77401L,
                       81781L,77751L,77753L,77900L,77902L,77903L),
         Name_code = c("General_Office",
                       "General_Office","General_Office","General_Office","General_Office",
                       NA,NA,"WBRT_3D",NA,"Brain_3D")
)

library(dplyr)

xrt %>% 
    left_join(code_names, by = c("XRT_code_small" = "Input_Code"))
#>    XRT_code_small   Date_XRT      Name_code
#> 1           77261 2015-07-30 General_Office
#> 2           77261 2015-08-02 General_Office
#> 3           77295 2015-07-23 General_Office
#> 4           77401 2015-08-03 General_Office
#> 5           77401 2015-08-05 General_Office
#> 6           77401 2015-08-06 General_Office
#> 7           77401 2015-08-09 General_Office
#> 8           77401 2015-08-10 General_Office
#> 9           77401 2015-08-11 General_Office
#> 10          77401 2015-08-12 General_Office

Created on 2020-02-05 by the reprex package (v0.3.0.9001)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.