簡體   English   中英

如何在R中向數據框添加另一列,以顯示其他兩個數據框的列之間的差異?

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

我有的:

我有兩個數據框可以使用。 那些是:

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

和:

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

它們是由以下代碼生成的,我不想更改它們:

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

我想要的是:我想將另一列粘貼到myDF_2005 ,該列具有兩個先前數據框中都存在的國家/地區的B_Scores差異。 換句話說:我想產生以下輸出:

> 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

問題:執行此操作的最優雅的代碼是什么?

# 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

使用的解決方案。 想法是合並兩個數據框,然后計算差異。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM