gpt4 book ai didi

python - 在 R 中创建我的数据框列的所有可能组合

转载 作者:行者123 更新时间:2023-12-02 16:25:22 24 4
gpt4 key购买 nike

我正在使用 R 中的数据框,其中包含以下列:

Input Data Frame

我想创建一个新的数据框,其中包含我的列的所有可能组合(数据应该相乘),并按照下表填充数据。棘手的部分是,如果变量的较高组合的值为 1,则所有较低的组合都应设为零。例如,在第三行; a=1,b=1,c=1。此处,abc = 1 并且所有其他组合都应设为 NA,因为它们是 abc 的子集。我的原始数据框中有 6 列,因此使事情变得更加复杂。

Output Data Frame

最佳答案

我建议使用

na.act <- getOption('na.action')
options('na.action' = na.pass)

# Create wide data.
df_wide <- as.data.frame(model.matrix(~ (.)^3 - RESPID, #replace 3 with the number of columns in your data
data = df))
options('na.action' = na.act)
df_wide$RESPID <- df$RESPID

编辑:

花了一点时间,我想出了一个解决方案。它需要大量转换数据、分组、嵌套,并且可能比@Jonas 提供的答案效率更低。但它是逐步完成工作的。它是……有点可读性,并在其他方面进行了评论。它是使用 3 个基本步骤作为我的主要想法完成的。它们在代码中有描述,但代码本身没有很好的记录。

初始化:

df <- data.frame(RESPID = 1:3, A = c(1, NA, 1), B = c(NA, 1, 1), C = c(1, 1, 1))

# Start by creating a wide data.frame using model.matrix
na.act <- getOption('na.action')
options('na.action' = na.pass)
df_wide <- as.data.frame(model.matrix(~ (.- RESPID)^3 - 1, #replace 3 with the number of columns in your data
data = df))
options('na.action' = na.act)
df_wide$RESPID <- df$RESPID
df_wide

第一步:

# Next lets get the groups that should actually be filled.
# To do this, we'll
# first) make a "long" format, with all the values that are currently filled.
# second) Find the active columns (A, B, C in this case) for the values that are filled, and count the number of active columns
# third) Find the maximum number of active columns for each active column (A, B, C)
# It is not very readable.
library(tidyverse)
library(stringr)
# Create a long version of the data, and split columns into multiple names.
df_longer <- pivot_longer(df_wide, cols = 1:7) %>%
mutate(name_split = str_split(name, ':')) %>%
mutate(col_count = lengths(name_split)) %>%
unnest_wider(name_split) %>%
# Sort by the number of letters used.
arrange(col_count) %>%
rename('First_active' = '...1',
'Second_Active' = '...2',
'Third_Active' = '...3') %>%
pivot_longer(cols = 4:6,
names_to = 'second_name',
values_to = 'second_value')

第 2 步(和第 3 步):


# Find the columns with maximum number of letters used:
max_indx <-
df_longer %>% group_by(second_name) %>%
drop_na(second_value, value) %>%
ungroup() %>%
select(RESPID, second_value, value, col_count) %>%
group_by(second_value, RESPID) %>%
summarize(indx_max = max(col_count), .groups = 'drop') %>%
group_by(RESPID) %>%
nest(data = second_value) %>%
select(data) %>%
mutate(colname = paste0(data[[1]][[1]], collapse = ':')) %>%
ungroup() %>%
select(-data)

# Print for visualization
max_indx
# A tibble: 3 x 2
RESPID colname
<int> <chr>
1 1 A:C
2 3 A:B:C
3 2 B:C

第 3 步(或第 4 步?):


# Now that we have the max indices, lets overwrite the values in df_wide
for(i in seq_len(nrow(max_indx))){
name <- max_indx$colname[max_indx$RESPID == i]
df_wide[df_wide$RESPID == i, name] <- 1
df_wide[df_wide$RESPID == i, colnames(df_wide)[!colnames(df_wide) %in% c(name, 'RESPID')]] <- 0
}
df_wide
A B C A:B A:C B:C A:B:C RESPID
1 0 0 0 0 1 0 0 1
2 0 0 0 0 0 1 0 2
3 0 0 0 0 0 0 1 3

关于python - 在 R 中创建我的数据框列的所有可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64685666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com