Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Last active May 19, 2016 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ram-N/6a5bd55082870e69cbdfba1450a61b78 to your computer and use it in GitHub Desktop.
Save Ram-N/6a5bd55082870e69cbdfba1450a61b78 to your computer and use it in GitHub Desktop.
library(dplyr)
df1 <- mtcars
df2 <- filter(mtcars, mpg >19)
names(df1)
col1 <- 'qsec'
val1 <- 15.5
col2 <- 'qsec'
val2 <- 15.50
row.names(df1[rownum1,])
row.names(df2[rownum2,])
names(df1)[3] <- 'three'
# if only ONE row matched in both data frames, compare
# all columns with the same names
compare_2dfs <- function(x, df1,r1,df2,r2){
list(df1 = df1[r1, x], "df2"=df2[r2, x], "diff"=df1[r1,x]-df2[r2,x])
}
oneRowOfComparisons <- function(df1, cond1, df2, cond2) {
r1 <- which(df1[,col1]==val1)
r2 <- which(df2[, col2] == val2)
if(singleRowMatch(df1, cond1) && singleRowMatch(df2, cond2)){
common_column_names <- intersect( names(df1), names(df2))
d <- data.frame(
sapply(common_column_names, compare_2dfs, df1, r1, df2, r2)
)
return(unlist(d))
}
}
singleRowMatch <- function(df1, cond1){
return(TRUE)
}
oneRowOfComparisons(df1,0,df2, 0)
#sapply(row.names(d), function(x){paste(x, names(d), sep="_")} )
filter_(mtcars, "mpg==19.70")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment