R语言 创建一个列,列出特定行中包含负值所有列名

0aydgbwb  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(189)

我有一个非常大的 Dataframe 。
假设我有这样的东西:

> p
    fu2_difftime_mridate_d_date fu2_difftime_premri fu2_difftime_prevfu fu2_difftime_fudate_d_date
1                          30.4                12.7               -14.2                        6.4
2                          12.9               -48.1               -47.6                       13.3

我想忽略所有包含0和负值的列,我在dplyr中寻找一个解决方案,它创建了一个新的协变量,包含所有包含0或负值的列:

> p
                negvalues fu2_difftime_premri fu2_difftime_prevfu fu2_difftime_fudate_d_date
1    "fu2_difftime_prevfu"               12.7               -14.2                        6.4

数据类型

p <- structure(list(fu2_date = structure(c(15428, 13746), class = "Date"), 
    fu2_mri_date = structure(c(16156, 13732), class = "Date"), 
    fu2_difftime_mridate_d_date = c(30.4, 12.9), fu2_difftime_premri = c(12.7, 
    -48.1), fu2_difftime_prevfu = c(-14.2, -47.6), fu2_difftime_fudate_d_date = c(6.4, 
    13.3)), row.names = 1:2, class = "data.frame")
zqdjd7g9

zqdjd7g91#

下面是一个tidyverse解决方案。基本上我们在所有列上进行测试(注:across(,..与across(everything(),...)相同如果值为负,则. < 0如果为真给予当前列名,其中:cur_column()。此后,我们必须使用unite

library(dplyr)
library(tidyr)

p %>% 
  mutate(across(, ~case_when(. < 0 ~ cur_column()), .names = 'new_{col}')) %>%
  unite(Neg_cols, starts_with('new'), na.rm = TRUE, sep = ', ')
fu2_date fu2_mri_date fu2_difftime_mridate_d_date fu2_difftime_premri fu2_difftime_prevfu
1 2012-03-29   2014-03-27                        30.4                12.7               -14.2
2 2007-08-21   2007-08-07                        12.9               -48.1               -47.6
  fu2_difftime_fudate_d_date                                Neg_cols
1                        6.4                      fu2_difftime_prevfu
2                       13.3 fu2_difftime_premri, fu2_difftime_prevfu
t40tm48m

t40tm48m2#

碱R溶液可以是,

apply(p[-c(1, 2)] <= 0, 1, \(i)toString(names(which(i))))
#                    1                                          2 
#"fu2_difftime_prevfu" "fu2_difftime_premri, fu2_difftime_prevfu" 

#Add to the dataframe

p$new <- apply(p[-c(1, 2)] <= 0, 1, \(i)toString(names(which(i))))

p
    fu2_date fu2_mri_date fu2_difftime_mridate_d_date fu2_difftime_premri fu2_difftime_prevfu fu2_difftime_fudate_d_date                                      new
1 2012-03-29   2014-03-27                        30.4                12.7               -14.2                        6.4                      fu2_difftime_prevfu
2 2007-08-21   2007-08-07                        12.9               -48.1               -47.6                       13.3 fu2_difftime_premri, fu2_difftime_prevfu
dba5bblo

dba5bblo3#

tidyverse解决方案:

library(tidyverse)

p %>%
  mutate(across(where(is.numeric), ~ ifelse(.x <= 0, cur_column(), NA)), .keep = "used") %>%
  unite("negative", sep = ", ", na.rm = TRUE) %>%
  bind_cols(p)

#                                   negative   fu2_date fu2_mri_date fu2_difftime_mridate_d_date fu2_difftime_premri fu2_difftime_prevfu fu2_difftime_fudate_d_date
# 1                      fu2_difftime_prevfu 2012-03-29   2014-03-27                        30.4                12.7               -14.2                        6.4
# 2 fu2_difftime_premri, fu2_difftime_prevfu 2007-08-21   2007-08-07                        12.9               -48.1               -47.6                       13.3

相关问题