如何使用 R 语言在基于多个二进制变量的数据框中创建新变量?

Posted

技术标签:

【中文标题】如何使用 R 语言在基于多个二进制变量的数据框中创建新变量?【英文标题】:How to create a new variable within a data frame based on multiple binary variables using R language? 【发布时间】:2021-05-06 06:39:00 【问题描述】:

数据框df有13个变量如下,

column 1-id 
column 2-pr_1 (pr_1 to pr_12 are binary variables)
column 3-pr_2
column 4-pr_3
column 5-pr_4
column 6-pr_5
column 7-pr_6
column 8-pr_7
column 9-pr_8
column10-pr_9
column11-pr_10
column12-pr_11
column13-pr_12 
Now, a variable "try" need to be created with 
the following rule within the data frame and 
for each observation,

1)-The value of pr_1 always equals to 1.
2)-If all elements from pr_1 to pr_12 are 1, then try=13
3)-If there is a missing value(NA) between pr_1 to pr_12, then try= NA
4)-If the 1st 0 occurs right after the last 1, for example, the 1st 0 occurs in the variable pr_6 and the last 1 is in the variable pr_5, then the value of "try" should equal to 6 (6=5+1). 

也就是说,“try”的值应该等于1的重复次数(重复次数中没有任何0或NA)加1。

带有新变量“try”的新数据集如下所示,

id      pr_1 pr_2 pr_3 pr_4 pr_5 pr_6 pr_7 pr_8 pr_9 pr_10 pr_11 pr_12 try 
j01       1    1    1    1    1   0    0     0   0     0     0    0     6
j02       1    1    1    0    0   0    0     0   0     0     0    0     4
j03       1    0    0    0    0   0    0     0   0     0     0    0     2
j04       1    1    1    1    1   1    1     1   1     1     1    1     13
j05       1    1    1    1    1   1    1     1   NA    1     1    NA    NA
j06       1    1    1    1    1   NA   NA   NA   NA    NA   NA    NA    NA
j07       1    0    NA   0    0   0    0     0   0     0     0    0     NA
j08       1    NA   0    0    0   0    0     0   0     0     0    0     NA
j09       1    NA   0   NA   NA  1    NA   NA   NA   NA     1    1      NA
j10       1    NA   1    1    1   1    1     1   1     1     1    0     NA

原始数据集的结构如下,

structure(list(id = c("j01", "j02", "j03", "j04", "j05", "j06", 
"j07", "j08", "j09", "j10"), pr_1 = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), pr_2 = c(1, 1, 0, 1, 1, 1, 0, NA, NA, NA), pr_3 = c(1, 
1, 0, 1, 1, 1, NA, 0, 0, 1), pr_4 = c(1, 0, 0, 1, 1, NA, 0, 
0, NA, 1), pr_5 = c(1, 0, 0, 1, 1, NA, 0, 0, 1, 1), pr_6 = c(0, 
0, 0, 1, 1, NA, 0, 0, NA, 1), pr_7 = c(0, 0, 0, 1, 1, NA, 0, 
0, NA, 1), pr_8 = c(0, 0, 0, 1, 1, NA, 0, 0, NA, 1), pr_9 = c(0, 
0, 0, 1, NA, NA, 0, 0, 1, 1), pr_10 = c(0, 0, 0, 1, 1, NA, 0, 
0, NA, 1), pr_11 = c(0, 0, 0, 1, 1, NA, 0, 0, NA, 1), pr_12 = c(0, 
0, 0, 1, 0, NA, 0, 0, NA, 0)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))->df

【问题讨论】:

【参考方案1】:

您可以添加所有pr 列的逐行总和。

df$try <- rowSums(df[-1]) + 1

# id       pr_1  pr_2  pr_3  pr_4  pr_5  pr_6  pr_7  pr_8  pr_9 pr_10 pr_11 pr_12   try
#   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 j01       1     1     1     1     1     0     0     0     0     0     0     0     6
# 2 j02       1     1     1     0     0     0     0     0     0     0     0     0     4
3 3 j03       1     0     0     0     0     0     0     0     0     0     0     0     2
# 4 j04       1     1     1     1     1     1     1     1     1     1     1     1    13
# 5 j05       1     1     1     1     1     1     1     1    NA     1     1     0    NA
# 6 j06       1     1     1    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
# 7 j07       1     0     0     0     0     0     0     0     0     0     0     0     2
# 8 j08       1    NA     0     0     0     0     0     0     0     0     0     0    NA
# 9 j09       1    NA    NA    NA     1    NA    NA    NA     1    NA    NA    NA    NA
#10 j10       1    NA     1     1     1     1     1     1     1     1     1     0    NA

或者使用dplyr

library(dplyr)
df %>% mutate(try = rowSums(select(., starts_with('pr'))) + 1)

【讨论】:

【参考方案2】:

我相信这会给你“尝试”专栏rowSums(apply(df[, 2:13], 2, function(x) (x == 1))) + 1。大体思路是按列检查元素是否等于 1,然后按行求和。请注意,您提供的数据集中pr_3 列与您在上面显示的列不同。为了获得您想要的相同结果,我假设这是一个错字并将其从 pr_3 = c(1, 1, 0, 1, 1, 1, NA, 0, NA, 1) 更改为 pr_3 = c(1,1, 0, 1, 1, 1, 0, 0, NA, 1)

【讨论】:

【参考方案3】:

您也可以使用此代码。但我相信当有 101111 之类的模式时,你想拥有 NA 吗?无论如何,下面的代码不起作用,仍然计算 1。

df %>% 
  tidyr::pivot_longer(-id) %>% 
  dplyr::group_by(id) %>% 
  dplyr::mutate(try = sum(value) + 1) %>% 
  dplyr::ungroup() %>% 
  tidyr::pivot_wider(names_from = name, values_from = value) 

【讨论】:

以上是关于如何使用 R 语言在基于多个二进制变量的数据框中创建新变量?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Python 中创建具有两列作为元组或 Pandas 数据框的单个变量?

R语言进行变量编码(recode):把dataframe中连续变量基于条件表达式转化为多个类别的离散变量(分类变量)使用attach函数绑定数据

计算并向R中的数据框添加新变量

如何从单个数据框中创建具有不同内容的多个 PDF?

在 R data.table 中创建虚拟变量

r语言如何把数据框内的数值型数据转变成日期型数据