简体   繁体   中英

How do I add another column to a dataframe in R that shows the difference between the columns of two other dataframes?

What I have:

I have two dataframes to work with. Those are:

> print(myDF_2003)
  A_score country B_score
1     200 Germany      11
2     150   Italy       9
3       0  Sweden       0

and:

> print(myDF_2005)
  A_score country B_score
1    -300  France      16
2     100 Germany      12
3     200   Italy      15
4      40   Spain      17

They are produced by the following code, which I do not want to change:

#_________2003______________

myDF_2003=data.frame(c(200,150,0),c("Germany", "Italy", "Sweden"), c(11,9,0))
colnames(myDF_2003)=c("A_score","country", "B_score")

myDF_2003$country=as.character(myDF_2003$country)
myDF_2003$country=factor(myDF_2003$country, levels=unique(myDF_2003$country))

myDF_2003$A_score=as.numeric(as.character(myDF_2003$A_score))
myDF_2003$B_score=as.numeric(as.character(myDF_2003$B_score))

#_________2005______________

myDF_2005=data.frame(c(-300,100,200,40),c("France","Germany", "Italy", "Spain"), c(16,12,15,17))
colnames(myDF_2005)=c("A_score","country", "B_score")

myDF_2005$country=as.character(myDF_2005$country)
myDF_2005$country=factor(myDF_2005$country, levels=unique(myDF_2005$country))

myDF_2005$A_score=as.numeric(as.character(myDF_2005$A_score))
myDF_2005$B_score=as.numeric(as.character(myDF_2005$B_score))

What I want: I want to paste another column to myDF_2005 which has the difference of the B_Scores of countries that exist in both previous dataframes. In other words: I want to produce this output:

> print(myDF_2005_2003_Diff)
      A_score country B_score B_score_Diff
    1    -300  France      16         
    2     100 Germany      12            1
    3     200   Italy      15            6
    4      40   Spain      17

Question: What is the most elegant code to do this?

# join in a temporary dataframe
temp <- merge(myDF_2005, myDF_2003, by = "country", all.x = T)
# calculate the difference and assign a new column
myDF_2005$B_score_Diff <- temp$B_score.x - temp$B_score.y

A solution using . The idea is to merge the two data frame and then calculate the difference.

library(dplyr)

myDF_2005_2 <- myDF_2005 %>%
  left_join(myDF_2003 %>% select(-A_score), by = "country") %>%
  mutate(B_score_Diff = B_score.x - B_score.y) %>%
  select(-B_score.y) %>%
  rename(B_score = B_score.x)
myDF_2005_2
#   A_score country B_score B_score_Diff
# 1    -300  France      16           NA
# 2     100 Germany      12            1
# 3     200   Italy      15            6
# 4      40   Spain      17           NA

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