Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

r - compare two columns in a data frame by data

I have two dataframe, lets call them DF1 and DF2, respectively, that look like the next:

|Symbol |   Date    | volume |price |
|------------------------------------
|A      |2014-01-01 | 1      |   5  |
|A      |2014-01-02 | 3      |   8  |
|A      |2014-01-03 | 7      |   4  |
|A      |2014-01-07 |3       |   6  |
|A      |2014-01-08 |34      |   7  |
|A      |2014-01-09 |45      |  34  |
|A      |2014-01-10 |4       |   5  | 
|A      |2014-01-11 |9       |   7  |
|A      |2014-01-14 |8       |   6  |
|A      |2014-01-15 |4       |   4  |
|A      |2014-01-16 |0       |   7  |
|A      |2014-01-17 |4       |   7  |

|   Date    | returns|
|--------------------
|2014-01-01 | 4      | 
|2014-01-02 | 6      |  
|2014-01-03 | 8      |  
|2014-01-07 | 2      |  
|2014-01-08 | 14     |   
|2014-01-09 | 5      |  
|2014-01-10 | 1      |    
|2014-01-11 | 2      |   
|2014-01-14 |8       |  
|2014-01-15 |4       |  
|2014-01-16 |0       |   
|2014-01-17 |4       |

And I would like to find a way to check if all the rows of the date column in the DF1 dataframe are equal to the corresponding entry of the date column in the DF2 dataframe and I don't know how to do it.

Maybe you can help me... thank you very much!

question from:https://stackoverflow.com/questions/65926036/compare-two-columns-in-a-data-frame-by-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Worth to mention the new package from the Hadley-verse: waldo

Gives really nice, concise, and informative messages with respect to the differences.

library(waldo)

compare(df1$Date, df2$Date)
#> ? No differences

data

# devtools::install_github("alistaire47/read.so")
df1 <- read.so::read_md("|Symbol |   Date    | volume |price |
  |------------------------------------
  |A      |2014-01-01 | 1      |   5  |
  |A      |2014-01-02 | 3      |   8  |
  |A      |2014-01-03 | 7      |   4  |
  |A      |2014-01-07 |3       |   6  |
  |A      |2014-01-08 |34      |   7  |
  |A      |2014-01-09 |45      |  34  |
  |A      |2014-01-10 |4       |   5  | 
  |A      |2014-01-11 |9       |   7  |
  |A      |2014-01-14 |8       |   6  |
  |A      |2014-01-15 |4       |   4  |
  |A      |2014-01-16 |0       |   7  |
  |A      |2014-01-17 |4       |   7  |")

  df2 <- read.so::read_md("|   Date    | returns|
  |--------------------
  |2014-01-01 | 4      | 
  |2014-01-02 | 6      |  
  |2014-01-03 | 8      |  
  |2014-01-07 | 2      |  
  |2014-01-08 | 14     |   
  |2014-01-09 | 5      |  
  |2014-01-10 | 1      |    
  |2014-01-11 | 2      |   
  |2014-01-14 |8       |  
  |2014-01-15 |4       |  
  |2014-01-16 |0       |   
  |2014-01-17 |4       |")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...