Compare two dataframes in R

不问归期 提交于 2021-01-28 10:54:29

问题


I have two dataframes in R and want to compare any entries of rows. I want two check if the value of the first entrie, second entrie etc. of first (any) row of the first dataframe is bigger as the entrie of the first entrie of the the first row of the second dataframe. Afterwards it should give me a TRUE if all entries are bigger and in the intervall (0,2). It looks like this.

Dataframe 1

Letter 2011 2012 2013
A       2     3    5
B       6     6    6
C       5     4    8

Dataframe 2

Letter 2011  2012 2013
A        1     1    4
C        5     5    5


Result for example like this (comparing rows A and A and C and C)

Letter  2011   2012  2013
 A        1      2     1      TRUE- all ok
 C        0      -1    3      FALSE- second entrie smaller of the first table and third entrie much more 
                              bigger of the first table.

回答1:


One approach could be to convert data to long format, perform an inner_join subtract values, check if all the values are in range and get the data back in wide format.

library(dplyr)
library(tidyr)

df1 %>% pivot_longer(cols = -Letter) %>%
   inner_join(df2 %>% pivot_longer(cols = -Letter), by = c("Letter", "name")) %>%
   mutate(value = value.x - value.y) %>%
   group_by(Letter) %>%
   mutate(check = all(between(value, 0, 2))) %>%
   select(-value.x, -value.y) %>%
   pivot_wider()

#  Letter check `2011` `2012` `2013`
#  <chr>  <lgl>  <int>  <int>  <int>
#1 A      TRUE       1      2      1
#2 C      FALSE      0     -1      3

data

df1 <- structure(list(Letter = c("A", "B", "C"), `2011` = c(2L, 6L,5L),
`2012` = c(3L, 6L, 4L), `2013` = c(5L, 6L, 8L)), row.names = c(NA, -3L), 
class = "data.frame")

df2 <- structure(list(Letter = c("A", "C"), `2011` = c(1L, 5L), `2012` = c(1L, 
5L), `2013` = 4:5), row.names = c(NA, -2L), class = "data.frame")


来源:https://stackoverflow.com/questions/59560914/compare-two-dataframes-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!