英文:
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 <- 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
> df2
col1 col2 col3
1 b 2 FALSE
2 f 0 TRUE
3 e 5 FALSE
4 e 1 TRUE
5 b 1 FALSE
> df3
col1 col2 col3
1 r 0 TRUE
2 d 1 TRUE
3 d 0 FALSE
4 b 5 TRUE
5 e 2 TRUE
> 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("e", "b", "d", "e", "d") ,
col2 = c(6, 1, 1, 2, 5),
col3= c(FALSE, FALSE, TRUE,TRUE, TRUE))
df1 <- data.frame(lapply(df1,factor))
df2 = data.frame(col1 = c("b", "f", "e", "e", "b") ,
col2 = c(2, 0, 5, 1, 1),
col3= c(FALSE, TRUE, FALSE,TRUE, FALSE))
df2 <- data.frame(lapply(df2,factor))
df3 = data.frame(col1 = c("r", "d", "d", "b", "e") ,
col2 = c(0, 1, 0, 5, 2),
col3= c(TRUE, TRUE, FALSE,TRUE, TRUE))
df3 <- data.frame(lapply(df3,factor))
df4 = data.frame(col1 = c("d", "e", "b", "d", "e") ,
col2 = c(5, 1, 2, 0, 5),
col3= c(TRUE, TRUE, FALSE,TRUE, TRUE))
df4 <- data.frame(lapply(df4,factor))
df <- 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 <- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论