[英]creating new columns based on several conditions in R
我有一个由三列组成的数据框,状态的唯一值如下“X”“0”“C”“1”“2”“3”“4”“5”。 一开始我不知道如何按每个id分组,根据条件创建几列,比如一个目标列,如果status是2、3、4、5则为1,否则为0。
month_balance 表示(提取数据的月份为起点,倒数,0为当前月份,-1为上个月,以此类推)
status代表(0:逾期1-29天,1:逾期30-59天,2:逾期60-89天,3:逾期90-119天,4:逾期120-149天,5:逾期或不良150天以上的债务核销C:当月还清,X:当月无贷款)
df <- data.frame (id = c("5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805"),
month_balance = c("0","-1","-2","-3","-4","-5","-6","-7","-8","-9","-10","-11","-12","-13","-14","-15","0","-1","-2","-3","-4","-5","-6","-7","-8","-9","-10","-11","-12","-13","-14"),
status = c("C","C","C","C","C","C","C","C","C","C","C","C","C","1","0","X","C","C","C","C","C","C","C","C","C","C","C","C","1","0","X")
)
最后,我想达到如下输出:
df1 <- data.frame (id = c("5008804","5008805"),
month_begin = c("16","15"),
paid_off = c("13","12"),
num_of_pastdues = c("2","2"),
no_loan = c("1","1"),
target = c("0","0"))
不太确定如何为target
编码,因为每个 id 的状态都出现了 target 0 和 1 多次出现。
以下是我为其他变量构建的方式:
df %>%
group_by(id) %>%
summarise(
month_begin=max(abs(as.numeric(month_balance)))+1,
paid_off=sum(status=="C"),
num_of_pastdues=sum(status %in% 0:5),
no_loan=sum(status=="X"))
# A tibble: 2 x 5
id month_begin paid_off num_of_pastdues no_loan
<chr> <dbl> <int> <int> <int>
1 5008804 16 13 2 1
2 5008805 15 12 2 1
library(tidyverse)
df <- data.frame (id = c("5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008804","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805","5008805"),
month_balance = c("0","-1","-2","-3","-4","-5","-6","-7","-8","-9","-10","-11","-12","-13","-14","-15","0","-1","-2","-3","-4","-5","-6","-7","-8","-9","-10","-11","-12","-13","-14"),
status = c("C","C","C","C","C","C","C","C","C","C","C","C","C","1","0","X","C","C","C","C","C","C","C","C","C","C","C","C","1","0","X")
) %>%
as_tibble()
df %>%
mutate(target = case_when(status %in% c(2, 3, 4, 5) ~ 1,
TRUE ~ 0),
paid_off = case_when(status == "C" ~ 1,
TRUE ~ 0),
no_loan = case_when(status == "X" ~ 1,
TRUE ~ 0)) %>%
group_by(id) %>%
summarise(month_begin = n(),
across(c(paid_off, no_loan, target), sum))
#> # A tibble: 2 x 5
#> id month_begin paid_off no_loan target
#> <chr> <int> <dbl> <dbl> <dbl>
#> 1 5008804 16 13 1 0
#> 2 5008805 15 12 1 0
由reprex 包于 2022-06-29 创建 (v2.0.1)
您可以尝试使用 dplyr。 首先,您可以创建具有所需条件的变量,然后您可以使用汇总来计算每组满足条件的次数。
df <- df %>%
mutate(num_of_pastdues = case_when(
status %in% c(2,3,4,5) ~ 1,
TRUE ~ 0
)) %>%
mutate(no_loan = case_when(
status == "X" ~ 1,
TRUE ~ 0
)) %>%
mutate(paid_off = case_when(
status == "C" ~ 1,
TRUE ~ 0
)) %>%
group_by(id) %>%
summarise(num_of_pastdues = sum(num_of_pastdues), no_loan = sum(no_loan), paid_off = sum(paid_off))
一个基本的 R 解决方案可以是创建一个自定义函数并将其应用于每个组,即
MyFunction <- function(x){
month_begin = length(x)
paid_off = sum(x == 'C')
num_of_pastdues = sum(x %in% 0:5)
no_loan = sum(x == 'X')
target = ifelse(any(x %in% 2:5), 1, 0)
return(c(month_begin=month_begin, paid_off=paid_off, num_of_pastdues=num_of_pastdues, no_loan=no_loan, target=target))
}
res <- t(sapply(split(df$status, df$id), MyFunction))
month_begin paid_off num_of_pastdues no_loan target
# 5008804 16 13 2 1 0
# 5008805 15 12 2 1 0
然后使其成为具有列 id 的数据框,
res_df <- data.frame(res)
res_df$id <- rownames(res_df)
rownames(res_df) <- NULL
res_df
#month_begin paid_off num_of_pastdues no_loan target id
#1 16 13 2 1 0 5008804
#2 15 12 2 1 0 5008805
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.