I am trying to build a report with comparisons from 2 data frames. I am using the comparedf
from the arsenal
package. The problem is that to be able to export it to an excel file I need to unnest the column, but because I have double and characters on the same column, it is not letting me unnest the column.
Here is the example, suppose I have the following data frames:
df1 <- data.frame(id = c("a", "b", "c", "d","e"),
var = c(1, 2, 3, 4, 5),
var2 = c("A","B","C","A","B"))
df2 <- data.frame(id = c("a", "b", "c", "d","e"),
var =c(1,3,4,2,5),
var2 = c("C","A","C","A","B"))
Then using comparedf I am generating another data frame with the comparison of non-matching values:
library(arsenal)
summary(comparedf(df1, df2, by="id"))
Table: Differences detected
var.x var.y id values.x values.y row.x row.y
------ ------ --- --------- --------- ------ ------
var var b 2 3 2 2
var var c 3 4 3 3
var var d 4 2 4 4
var2 var2 a A C 1 1
var2 var2 b B A 2 2
The columns values.x and values.y are two nested lists, thus I try to capture this table with the following codes:
vec1 <- summary(comparedf(df1, df2, by="id"))
library(tidyr)
diftable <- vec1$diffs.table |> unnest(cols = c(values.x, values.y))
Which gives me the error:
Can't combine `x[[1]]` <double> and `x[[4]]` <character>.
How can I force these two columns to unnest into one character column?
We can convert the two columns to character so they can then be combined into a homogenous atomic vector.
vec1$diffs.table %>%
mutate(across(values.x:values.y, as.character)) %>%
unnest(cols = c(values.x, values.y))