2013-03-01 258 views
0

在R i中有2個數據幀「df1」和「df2」。它們如下:比較兩個數據幀並且在R中改變一個數據幀R

> df1 
    date  value 
1 1980-12-10  5 
2 1980-12-11  5 
3 1980-12-12  5 
4 1980-12-13  5 
5 1980-12-14  5 


>df2 
    date  value 
1 1980-12-10  15 
2 1980-12-11  2 
3 1980-12-12  23 
4 1980-12-13  44 
5 1980-12-14  434 
6 1980-12-15  242 
7 1980-12-16  22 
8 1980-12-17  82 
9 1980-12-18  723 
10 1980-12-19  72 

我想更改「df2」。只有當df1與df2具有相同的日期時,df2才必須包含這些值。 其實我需要的輸出如下:

>df2 
    date  value 
1 1980-12-10  15 
2 1980-12-11  2 
3 1980-12-12  23 
4 1980-12-13  44 
5 1980-12-14  434 

是否有可能在R'

回答

2

你可以使用子集和%in%

df2[df2$date%in%df1$date,] 
     date value 
1 1980-12-10 15 
2 1980-12-11  2 
3 1980-12-12 23 
4 1980-12-13 44 
5 1980-12-14 434 
1
# read in both data frames 
df1 <- 
    read.table(h = TRUE , text = "date  value 
    1980-12-10  5 
    1980-12-11  5 
    1980-12-12  5 
    1980-12-13  5 
    1980-12-14  5") 

df2 <- 
    read.table(h = TRUE , text = "date  value 
    1980-12-10  15 
    1980-12-11  2 
    1980-12-12  23 
    1980-12-13  44 
    1980-12-14  434 
    1980-12-15  242 
    1980-12-16  22 
    1980-12-17  82 
    1980-12-18  723 
    1980-12-19  72") 

# merge df1 and df2, only keeping the date column from df1 
# but note no comma, which maintains the `class` of df1, 
# allowing the merge to work appropriately 
merge(df1[ 'date' ] , df2) 

# and if you wanted to overwrite df2 with the new results: 
df2 <- merge(df1[ 'date' ] , df2) 
1

您可以使用sqldf,做一個SQL INNER JOIN(R合併),例如:

library(sqldf) 
sqldf('SELECT df2.* 
     FROM df2 
     INNER JOIN df1 
     ON df1.date = df2.date') 
     date value 
1 1980-12-10 15 
2 1980-12-11  2 
3 1980-12-12 23 
4 1980-12-13 44 
5 1980-12-14 434