I have a dataframe with 15 factor columns. I need to create a new column that gives a true/false binary for whether all the values in a given row (for just those columns) are identical.
I've found this strategy...
identical(df[['A']],df[['B']])
...but that checks the entire column, whereas I need a solution by row.
Using DF as shown in order to test it, apply unique to each row and for each row check whether the length of the result is 1
DF <- data.frame(A = 1:3, B = c(1:2, 1), C = 1)
transform(DF, same = apply(DF, 1, function(x) length(unique(x)) == 1))
giving:
A B C same
1 1 1 1 TRUE
2 2 2 1 FALSE
3 3 1 1 FALSE
Alternately using dlyr 1.0 or later:
library(dplyr)
DF %>%
rowwise %>%
mutate(same = n_distinct(unlist(cur_data())) == 1) %>%
ungroup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With