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

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

R - Mode of elements between multiple dataframes

问题

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

  1. # 创建一个函数,用于获取一组数据框中每个位置的众数
  2. get_mode <- function(dataframes) {
  3. result <- list()
  4. for (i in 1:length(dataframes[[1]])) {
  5. result[[i]] <- lapply(dataframes, function(df) {
  6. col <- unlist(df[i])
  7. mode(col)
  8. })
  9. }
  10. return(result)
  11. }
  12. # 调用函数并存储结果
  13. result <- get_mode(df)
  14. # 将结果合并为数据框
  15. 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)

  1. df1
  2. col1 col2 col3
  3. 1 e 6 FALSE
  4. 2 b 1 FALSE
  5. 3 d 1 TRUE
  6. 4 e 2 TRUE
  7. 5 d 5 TRUE
  8. &gt; df2
  9. col1 col2 col3
  10. 1 b 2 FALSE
  11. 2 f 0 TRUE
  12. 3 e 5 FALSE
  13. 4 e 1 TRUE
  14. 5 b 1 FALSE
  15. &gt; df3
  16. col1 col2 col3
  17. 1 r 0 TRUE
  18. 2 d 1 TRUE
  19. 3 d 0 FALSE
  20. 4 b 5 TRUE
  21. 5 e 2 TRUE
  22. &gt; df4
  23. col1 col2 col3
  24. 1 d 5 TRUE
  25. 2 e 1 TRUE
  26. 3 b 2 FALSE
  27. 4 d 0 TRUE
  28. 5 e 5 TRUE

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

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

The given data can be recreated with the following code:

  1. df1 = data.frame(col1 = c(&quot;e&quot;, &quot;b&quot;, &quot;d&quot;, &quot;e&quot;, &quot;d&quot;) ,
  2. col2 = c(6, 1, 1, 2, 5),
  3. col3= c(FALSE, FALSE, TRUE,TRUE, TRUE))
  4. df1 &lt;- data.frame(lapply(df1,factor))
  5. df2 = data.frame(col1 = c(&quot;b&quot;, &quot;f&quot;, &quot;e&quot;, &quot;e&quot;, &quot;b&quot;) ,
  6. col2 = c(2, 0, 5, 1, 1),
  7. col3= c(FALSE, TRUE, FALSE,TRUE, FALSE))
  8. df2 &lt;- data.frame(lapply(df2,factor))
  9. df3 = data.frame(col1 = c(&quot;r&quot;, &quot;d&quot;, &quot;d&quot;, &quot;b&quot;, &quot;e&quot;) ,
  10. col2 = c(0, 1, 0, 5, 2),
  11. col3= c(TRUE, TRUE, FALSE,TRUE, TRUE))
  12. df3 &lt;- data.frame(lapply(df3,factor))
  13. df4 = data.frame(col1 = c(&quot;d&quot;, &quot;e&quot;, &quot;b&quot;, &quot;d&quot;, &quot;e&quot;) ,
  14. col2 = c(5, 1, 2, 0, 5),
  15. col3= c(TRUE, TRUE, FALSE,TRUE, TRUE))
  16. df4 &lt;- data.frame(lapply(df4,factor))
  17. df &lt;- list(df1,df2,df3,df4)

Thanks a lot for the help!

答案1

得分: 2

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

  1. library(dplyr)
  2. library(purrr)
  3. Mode <- function(x) {
  4. ux <- unique(x)
  5. ux[which.max(tabulate(match(x, ux)))]
  6. }
  7. map_df(df, ~.x %>% mutate(position = row_number())) %>%
  8. summarise(across(everything(), Mode), .by = position) %>%
  9. select(-position)
  10. # col1 col2 col3
  11. #1 e 6 FALSE
  12. #2 b 1 TRUE
  13. #3 d 1 FALSE
  14. #4 e 2 TRUE
  15. #5 e 5 TRUE
英文:

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

  1. library(dplyr)
  2. library(purrr)
  3. Mode &lt;- function(x) {
  4. ux &lt;- unique(x)
  5. ux[which.max(tabulate(match(x, ux)))]
  6. }
  7. map_df(df, ~.x %&gt;% mutate(position = row_number())) %&gt;%
  8. summarise(across(everything(), Mode), .by = position) %&gt;%
  9. select(-position)
  10. # col1 col2 col3
  11. #1 e 6 FALSE
  12. #2 b 1 TRUE
  13. #3 d 1 FALSE
  14. #4 e 2 TRUE
  15. #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:

确定