如何根据其他列的true/false新建列?

fcipmucu  于 2023-03-15  发布在  其他
关注(0)|答案(4)|浏览(184)

我有多个包含TRUEFALSE语句的列,我想创建一个包含true列的列名的新列,它应该与示例类似。
颜色必须是新列。

color   red yellow orange  blue
1           blue FALSE  FALSE  FALSE  TRUE
2      red, blue  TRUE  FALSE  FALSE  TRUE
3    blue, green FALSE  FALSE  FALSE  TRUE
4         purple FALSE  FALSE  FALSE FALSE
5 yellow, orange FALSE   TRUE   TRUE FALSE

我试过使用case_when函数,但它是许多排列使用。

js5cn81o

js5cn81o1#

您可以将names子集化为applycbind

cbind(dat, clr=apply(dat[-1], 1, \(x) if (any(x)) toString(names(dat)[-1][x]) else NA))
#            color   red yellow orange  blue            clr
# 1           blue FALSE  FALSE  FALSE  TRUE           blue
# 2      red, blue  TRUE  FALSE  FALSE  TRUE      red, blue
# 3    blue, green FALSE  FALSE  FALSE  TRUE           blue
# 4         purple FALSE  FALSE  FALSE FALSE           <NA>
# 5 yellow, orange FALSE   TRUE   TRUE FALSE yellow, orange
  • 数据:*
dat <- structure(list(color = c("blue", "red, blue", "blue, green", 
"purple", "yellow, orange"), red = c(FALSE, TRUE, FALSE, FALSE, 
FALSE), yellow = c(FALSE, FALSE, FALSE, FALSE, TRUE), orange = c(FALSE, 
FALSE, FALSE, FALSE, TRUE), blue = c(TRUE, TRUE, TRUE, FALSE, 
FALSE)), class = "data.frame", row.names = c(NA, -5L))
kx7yvsdv

kx7yvsdv2#

我会使用tidyverse,并在之前以单独的方式创建列(可能有几种方法可以做到这一点):

# Prepare the data to add the id column
df <- df %>% 
  mutate(id = row_number())

# Compute the new column with the colors
df_new_col <- df %>% 
  pivot_longer(!id, names_to = "color", values_to = "presence") %>% 
  filter(presence) %>% 
  group_by(id) %>% 
  summarise(
    Color = paste0(color, collapse = ", ")
  )

# Add the new column, and remove the temporary id
df <- df %>% 
  left_join(df_new_col, by = "id") %>% 
  select(-id)

我这样做是为了防止某些行全是FALSE。

knsnq2tg

knsnq2tg3#

另一种方式:

library(dplyr)

df %>%
  rowwise %>%
  mutate(color = toString(names(.)[c_across(everything())])) %>%
  ungroup

输出:

# A tibble: 5 × 5
  red   yellow orange blue  color           
  <lgl> <lgl>  <lgl>  <lgl> <chr>           
1 FALSE FALSE  FALSE  TRUE  "blue"          
2 TRUE  FALSE  FALSE  TRUE  "red, blue"     
3 FALSE FALSE  FALSE  TRUE  "blue"          
4 FALSE FALSE  FALSE  FALSE ""              
5 FALSE TRUE   TRUE   FALSE "yellow, orange"
vlju58qv

vlju58qv4#

我们可以使用tidyverse作为

library(dplyr)
library(tidyr)
 df1 %>% 
  mutate(across(red:blue, ~ case_when(.x ~ cur_column()))) %>%
  unite(color, red:blue, na.rm = TRUE, sep = ", ", remove = FALSE)
  • 输出
color  red yellow orange blue
1           blue <NA>   <NA>   <NA> blue
2      red, blue  red   <NA>   <NA> blue
3           blue <NA>   <NA>   <NA> blue
4                <NA>   <NA>   <NA> <NA>
5 yellow, orange <NA> yellow orange <NA>

相关问题