[英]Element-wise most frequent element R
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). 我想在一排(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.
所以我不能使用cbind,因为它将因子转换为数值。
You can try a tidyverse
你可以尝试一个
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 在
R
您可以尝试
apply(df, 1, function(x) names(table(x))[which.max(table(x))])
[1] "a" "a" "b"
this works for me (on your sample data) 这对我有用(根据您的样本数据)
apply(df, 1, median)
[1] "a" "a" "b"
but since the median is not the way to go... try this: 但是由于中位数不是要走的路,请尝试以下操作:
library(dplyr)
df %>%
#melt your data to long format
gather() %>%
#group
group_by(key, value) %>%
#count per group
summarise( number = n() ) %>%
#arrange secending on number
arrange( desc( number ) ) %>%
#filter the first row of each group
filter(row_number()==1)
# A tibble: 3 x 3
# Groups: key [3]
key value number
<chr> <chr> <int>
1 V1 b 2
2 V2 a 2
3 V3 a 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.