What is the easiest way and the fastest way to compare dataframes. Input:
df1 <- data.frame(ID = c(1, 2, 3, 4),
A = c(1, 2, 3, NA),
B = c(1, 2, NA, NA))
> df1
ID A B
1 1 1 1
2 2 2 2
3 3 3 NA
4 4 NA NA
df2 <- data.frame(ID = c(1, 2, 3, 4),
A = c(1, 2, 3, 4),
B = c(1, 2, 3, NA))
> df2
ID A B
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 NA
I assume that data frames have the same amount of rows and columns. The output data frame should contain:
- Column ID with values from df1 or df2 - (doesn't mather if from df1 or df2 because they are the same.
- In columns A and B shoud be value from df2 if content of cell from df1 is different than in df2, or NA if values are the same.
I can use for and iterate for every row and column but it is not elegant solution. Is it better?
I tried:
result = apply(ifelse(df1[,] == df2[,], NA, df2[,])
but doesn't work.
The output df should look like this:
result <- data.frame(ID = c(1, 2, 3, 4),
A = c(NA, NA, NA, 4),
B = c(NA, NA, 3, NA))
> result
ID A B
1 1 NA NA
2 2 NA NA
3 3 NA 3
4 4 4 NA
Aucun commentaire:
Enregistrer un commentaire