Home > Enterprise >  How do I assign each row of a data frame to its respective value from another data frame?
How do I assign each row of a data frame to its respective value from another data frame?

Time:04-30

There are two data frames, DF_A and DF_B.

DF_A:

domain valid
googl.nl 1
hwenzl.de 0
aytqfis.com 0
tokzrrtss.es 1
cnn.net 0

DF_B:

domain good_page
googl.nl NA
googl.nl NA
googl.nl NA
googl.nl NA
googl.nl NA
googl.nl NA
hwenzl.de NA
hwenzl.de NA
hwenzl.de NA
aytqfis.com NA
tokzrrtss.es NA
tokzrrtss.es NA
tokzrrtss.es NA
tokzrrtss.es NA
cnn.net NA
cnn.net NA
cnn.net NA
cnn.net NA

I want to change good_page for each row in DF_B to the respective value in valid of DF_A. Using mutate caused an error and using a for loop didn't help either.

CodePudding user response:

In dplyr piplines:

DF_B %>% right_join(DF_A)

gives

Joining, by = "domain"
# A tibble: 18 × 3
   domain       good_page valid
   <chr>        <lgl>     <dbl>
 1 googl.nl     NA            1
 2 googl.nl     NA            1
 3 googl.nl     NA            1
 4 googl.nl     NA            1
 5 googl.nl     NA            1
 6 googl.nl     NA            1
 7 hwenzl.de    NA            0
 8 hwenzl.de    NA            0
 9 hwenzl.de    NA            0
10 aytqfis.com  NA            0
11 tokzrrtss.es NA            1
12 tokzrrtss.es NA            1
13 tokzrrtss.es NA            1
14 tokzrrtss.es NA            1
15 cnn.net      NA            0
16 cnn.net      NA            0
17 cnn.net      NA            0
18 cnn.net      NA            0

which is the bulk of the work.

CodePudding user response:

How about:

DF_C <- left_join(DF_B, DF_A, by="domain")

Or maybe some of the *_join() function family: 9.2.3 Merging data frames with *_join()

  •  Tags:  
  • r
  • Related