For example I have data frame:
df <- data.frame(V1=c("a", "a", "b"),
V2 = c("b", "a", "a"),
V3 = c("a", "a", "b"))
> df
V1 V2 V3
1 a b a
2 a a a
3 b a b
I want to find most commont element in a row (a, a, b).
I have following code, which does that:
most_freq <- function(df){
k <- nrow(df)
values <- NULL
for(i in 1:k){
values[i] <- names(sort(table(unlist(df[i,])), decreasing = TRUE))[1]
}
return(values)
}
But it's, in my opinion, bad. First of all, it works slowly, uses many functions. Is there any simplier way to do that? Keep in mind, that I have factors too. So i can't use cbind, because it converts factor to numeric.
You can try a tidyverse
library(tidyverse)
df %>%
rownames_to_column() %>%
gather(k, v, -rowname) %>%
group_by(rowname) %>%
count(v) %>%
filter(n==max(n))
# A tibble: 3 x 3
# Groups: rowname [3]
rowname v n
<chr> <chr> <int>
1 1 a 2
2 2 a 3
3 3 b 2
In base R
you can try
apply(df, 1, function(x) names(table(x))[which.max(table(x))])
[1] "a" "a" "b"