R:根据一个变量的因子级别创建条件变量,并分配给同一组的所有行

时间:2018-05-15 10:52:33

标签: r conditional r-factor

以下是我的数据示例。专栏" first_tx"是我想要的输出:

ID   first_date   dates        txtype     first_tx
11   2015-12-23   2015-12-23   A          A
11   2015-12-23   2016-12-23   A          A
11   2015-12-23   2017-12-23   B          A
22   2015-11-01   2015-11-01   B          B
22   2015-11-01   2016-11-01   C          B
22   2015-11-01   2016-12-01   C          B

我正在尝试创建" first_tx"按组,基于" txtype"的因子水平;当" first_date"等于"日期"

我试过

data$first_tx[which(data$first_date==data$dates)] <- as.character(data$txtype)[which(data$first_date==data$dates)]

这给了我以下输出:

ID   first_date   dates        txtype     first_tx
11   2015-12-23   2015-12-23   A          A
11   2015-12-23   2016-12-23   A          NA
11   2015-12-23   2017-12-23   B          NA
22   2015-11-01   2015-11-01   B          B
22   2015-11-01   2016-11-01   C          NA
22   2015-11-01   2016-12-01   C          NA

但是,我希望每个ID的所有行都具有相同的对应&#34; txtype&#34;而不是NA。水平。

3 个答案:

答案 0 :(得分:1)

使用dplyrtidyr我可以创建您的预期输出。

library(dplyr)
library(tidyr)

df %>% 
  mutate(first_tx = ifelse(first_date == dates, txtype, NA)) %>% 
  fill(first_tx)

  ID first_date      dates txtype first_tx
1 11 2015-12-23 2015-12-23      A        A
2 11 2015-12-23 2016-12-23      A        A
3 11 2015-12-23 2017-12-23      B        A
4 22 2015-11-01 2015-11-01      B        B
5 22 2015-11-01 2016-11-01      C        B
6 22 2015-11-01 2016-12-01      C        B

数据:

df <- structure(list(ID = c(11L, 11L, 11L, 22L, 22L, 22L), 
               first_date = c("2015-12-23", "2015-12-23", "2015-12-23", "2015-11-01", "2015-11-01", "2015-11-01"), 
               dates = c("2015-12-23", "2016-12-23", "2017-12-23", "2015-11-01", "2016-11-01", "2016-12-01"), 
               txtype = c("A", "A", "B", "B", "C", "C")),
          .Names = c("ID", "first_date", "dates", "txtype"), 
          row.names = c(NA, -6L), 
          class = "data.frame")

答案 1 :(得分:0)

你带这样的东西吗?

library(data.table)
data <- data.table(
  ID = c('11', '11', '11', '22', '22', '22'),
  first_date = c('2015-12-23', '2015-12-23', '2015-12-23', '2015-11-01', '2015-11-01', '2015-11-01'),
  dates = c('2015-12-23', '2016-12-23', '2017-12-23', '2015-11-01', '2016-11-01', '2016-12-01'),
  txtype = c('A', 'A', 'B', 'B', 'C', 'C')
)


data[first_date == dates,
     ':='(first_tx = txtype),
     by = .(txtype)]

答案 2 :(得分:0)

我玩弄了它,这很有效:

data <- data %>% group_by(ID) %>% mutate(first_tx = {if (first_date == dates) txtype[min(which(first_date == dates))] else NA})

感谢@phiver的帮助!