【问题标题】:Create new columns within loop or apply在循环中创建新列或应用
【发布时间】:2016-08-22 09:11:36
【问题描述】:

我正在使用的数据集是按客户和月份的计费数据。最后,我想制作一个数据框,其中包含行的客户 ID 和列名的月份 - 就像在原始数据集中一样。但是,我希望这个新数据集包含关于客户是否在那个月“获得”的虚拟变量。他们之前从未收到过账单,而当月是他们第一次收到账单。

这是一个可重现的示例以及我现在编写的循环:

set.seed(24)
example.data <- data.frame(
   ID = sample(11:20),
   Jan = sample(0:5, 10, replace = TRUE),
   Feb = sample(0:5, 10, replace = TRUE),
   Mar = sample(0:5, 10, replace = TRUE),
   Apr = sample(0:5, 10, replace = TRUE)
)
gained.df.ex <- data.frame(example.data$ID)

## customers can't be gained in the first month
## there's no previous data to verify that this is the first time they've been billed, so all values are 0

gained.df.ex$Jan <- rep(0, length(example.data$ID)

## here's the loop that isn't working

for(i in 3:5){
   new.month.dummy <- for (x in 1:length(gained.df.ex$example.data.ID)){
      ifelse(example.data[x,i] == 0, new.month.dummy[x] <- 0, ifelse(sum(example.data[x,2:(i-1)]} == 0, new.month.dummy[x] <-1, new.month.dummy <- 0))
}

我确定有一种方法可以使用 apply 来做到这一点,但我不确定如何。

预期的输出如下所示:

> example.data
   Jan Feb Mar Apr
15   0   3   4   3
19   1   3   0   5
20   4   2   5   1
12   2   1   3   0
14   0   0   2   1
17   5   5   4   4
11   3   4   1   5
18   1   0   0   2
13   3   2   5   3
16   2   5   1   2

> gained.df.ex
   Jan Feb Mar Apr
15   0   1   0   0
19   0   0   0   0
20   0   0   0   0
12   0   0   0   0
14   0   0   1   0
17   0   0   0   0
11   0   0   0   0
18   0   0   0   0
13   0   0   0   0
16   0   0   0   0

【问题讨论】:

  • 您能否根据示例发布预期的输出
  • 每个ID只有一行吗?
  • 预期输出已添加到问题中。
  • 是的,每个 ID 只有一行
  • 为什么预期输出中的第二行全为 0?所有的数字看起来都是独一无二的,在 0 之后有数字 5 的增益。

标签: r loops for-loop apply


【解决方案1】:

我们可以试试

gained.df.ex[names(example.data)] <- t(apply(example.data, 1, function(x) {
            i1 <- tail(which(cumsum(x)==0),1)
             x1 <- rep(0, length(x))
             if(length(i1) >0) replace(x1, i1+1, 1) else x1}))
gained.df.ex[names(example.data)]
#   Jan Feb Mar Apr
#1    0   1   0   0
#2    0   0   0   0
#3    0   0   0   0
#4    0   0   0   0
#5    0   0   1   0
#6    0   0   0   0
#7    0   0   0   0
#8    0   0   0   0
#9    0   0   0   0
#10   0   0   0   0

【讨论】:

  • 仍在尝试理解原始问题...但这是一个不错的方法。我要说的是适应和滞后,然后收工
猜你喜欢
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2017-03-24
  • 1970-01-01
相关资源
最近更新 更多