元素在多个数据框之间的 R 模式

huangapple go评论76阅读模式
英文:

R - Mode of elements between multiple dataframes

问题

我理解了你的请求,以下是翻译的代码部分:

# 创建一个函数,用于获取一组数据框中每个位置的众数
get_mode <- function(dataframes) {
  result <- list()
  for (i in 1:length(dataframes[[1]])) {
    result[[i]] <- lapply(dataframes, function(df) {
      col <- unlist(df[i])
      mode(col)
    })
  }
  return(result)
}

# 调用函数并存储结果
result <- get_mode(df)

# 将结果合并为数据框
result_df <- as.data.frame(result)

希望这有所帮助!

英文:

I got multiple dataframes of the same dimension within a single list. All dataframes are categorical, each has factorlevels. The columnnames of the dataframes are identical, in general they have the same factorlevels. It might be though that some factor levels don´t appear in all dataframes.
I need to create a dataframe where each element is the mode (most frequently appearing element) of all elements at this position from all the dataframes. If there is a tie for the most frequent then just take one of those, be it the first, the last or an random one.

Thats how the data looks for example. df1,df2,df3,df4 are stored in the list df &lt;- list(df1,df2,df3,df4)

df1
  col1 col2  col3
1    e    6 FALSE
2    b    1 FALSE
3    d    1  TRUE
4    e    2  TRUE
5    d    5  TRUE
&gt; df2
  col1 col2  col3
1    b    2 FALSE
2    f    0  TRUE
3    e    5 FALSE
4    e    1  TRUE
5    b    1 FALSE
&gt; df3
  col1 col2  col3
1    r    0  TRUE
2    d    1  TRUE
3    d    0 FALSE
4    b    5  TRUE
5    e    2  TRUE
&gt; df4
  col1 col2  col3
1    d    5  TRUE
2    e    1  TRUE
3    b    2 FALSE
4    d    0  TRUE
5    e    5  TRUE

Desired result would be this. Hopefully made no mistake, this was done by hand.

  col1 col2  col3
1    e    6 FALSE
2    b    1  TRUE
3    d    1  FALSE
4    e    2  TRUE
5    e    5  TRUE

The given data can be recreated with the following code:

df1 = data.frame(col1 = c(&quot;e&quot;, &quot;b&quot;, &quot;d&quot;, &quot;e&quot;, &quot;d&quot;) ,
                 col2 = c(6, 1, 1, 2, 5),
                 col3= c(FALSE, FALSE, TRUE,TRUE, TRUE))
df1 &lt;- data.frame(lapply(df1,factor))

df2 = data.frame(col1 = c(&quot;b&quot;, &quot;f&quot;, &quot;e&quot;, &quot;e&quot;, &quot;b&quot;) ,
                 col2 = c(2, 0, 5, 1, 1),
                 col3= c(FALSE, TRUE, FALSE,TRUE, FALSE))
df2 &lt;- data.frame(lapply(df2,factor))

df3 = data.frame(col1 = c(&quot;r&quot;, &quot;d&quot;, &quot;d&quot;, &quot;b&quot;, &quot;e&quot;) ,
                 col2 = c(0, 1, 0, 5, 2),
                 col3= c(TRUE, TRUE, FALSE,TRUE, TRUE))
df3 &lt;- data.frame(lapply(df3,factor))

df4 = data.frame(col1 = c(&quot;d&quot;, &quot;e&quot;, &quot;b&quot;, &quot;d&quot;, &quot;e&quot;) ,
                 col2 = c(5, 1, 2, 0, 5),
                 col3= c(TRUE, TRUE, FALSE,TRUE, TRUE))
df4 &lt;- data.frame(lapply(df4,factor))

df &lt;- list(df1,df2,df3,df4)   

Thanks a lot for the help!

答案1

得分: 2

你可以在每个列表中添加一个位置列,将它们合并成一个数据框,并为每个位置找到 Mode

library(dplyr)
library(purrr)

Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

map_df(df, ~.x %>% mutate(position = row_number())) %>%
  summarise(across(everything(), Mode), .by = position) %>%
  select(-position)

#  col1 col2  col3
#1    e    6 FALSE
#2    b    1  TRUE
#3    d    1 FALSE
#4    e    2  TRUE
#5    e    5  TRUE
英文:

You may add a position column in each list, combine them into one dataframe and find Mode for each position.

library(dplyr)
library(purrr)

Mode &lt;- function(x) {
  ux &lt;- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}


map_df(df, ~.x %&gt;% mutate(position = row_number())) %&gt;%
  summarise(across(everything(), Mode), .by = position) %&gt;%
  select(-position)

#  col1 col2  col3
#1    e    6 FALSE
#2    b    1  TRUE
#3    d    1 FALSE
#4    e    2  TRUE
#5    e    5  TRUE

huangapple
  • 本文由 发表于 2023年5月21日 11:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定