Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按元素最频繁元素R_R_Row_Frequency - Fatal编程技术网

按元素最频繁元素R

按元素最频繁元素R,r,row,frequency,R,Row,Frequency,例如,我有数据框: 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 df V1 V2 V3 1 a b a 2 a a a 3 b a b 我想找到一行中最常见的元素(a、a、b) 我有以下代码,它可以: mo

例如,我有数据框:

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
df
V1 V2 V3
1 a b a
2 a a a
3 b a b
我想找到一行中最常见的元素(a、a、b)

我有以下代码,它可以:

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)
}
most_freq这对我很有效(在你的样本数据上)

但既然中位数不是我们要走的路。。。试试这个:

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
库(dplyr)
df%>%
#将数据转换为长格式
聚集()%>%
#团体
分组依据(键,值)%>%
#每组计数
摘要(数字=n())%>%
#按数字排列
排列(描述(编号))%>%
#筛选每个组的第一行
过滤器(行数()==1)
#一个tibble:3x3
#分组:键[3]
键值编号
1 V1 b 2
2 V2 a 2
3 V3 a 2
这适用于我(在您的示例数据上)

但既然中位数不是我们要走的路。。。试试这个:

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
库(dplyr)
df%>%
#将数据转换为长格式
聚集()%>%
#团体
分组依据(键,值)%>%
#每组计数
摘要(数字=n())%>%
#按数字排列
排列(描述(编号))%>%
#筛选每个组的第一行
过滤器(行数()==1)
#一个tibble:3x3
#分组:键[3]
键值编号
1 V1 b 2
2 V2 a 2
3 V3 a 2

您可以尝试一款
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

您可以尝试使用
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

对于示例数据,是的,它是有效的。但是,如果有第四列
V4=c(“b”、“a”、“a”)
,该怎么办?@Wimpel抱歉,您的第二个答案没有给出正确的输出。对于示例数据,是的,它是有效的。但是如果有第四列
V4=c(“b”、“a”、“a”)
?@Wimpel抱歉,您的第二个答案没有给出正确的输出。如果是领带,您希望输出什么?如果是领带,我更希望随机选择如果是领带,您希望输出什么?如果是领带,我更喜欢随机选择您可以只计算一次
table
名称(tbl或
apply(df,1,函数(x)名称(which.max(table(x)))
您可以只计算一次
table
名称(tbl或
apply(df,1,函数(x)名称(which.max(table(x)))