R条件申请不适用于所有记录

时间:2019-05-07 18:10:14

标签: r conditional apply

我有以下数据框。 Change_Month表示Adv的所有值都属于N类别的月份,从该月起,属于V类别的月份。

Adv_Code Change_Month Change_Dt     April   May June    July    August  September   October November    December    January     February    March
A201    April           7/4/2017    0       0   0       1       0       0           0       0           0           0           0           0
A198    April           7/4/2017    1       1   0       0       3       0           0       0           0           0           0           0
S1212   May             16/04/2017  0       0   0       0       0       0           0       0           0           0           0           1
S1213   January         4/1/2018    1       0   0       1       1       1           1       0           1           1           2           1

因此对于A201,所有值都将属于V类别。对于A198同样。 对于S1212,4月份的值将属于N类别,其余的则属于V。 同样,对于S1213,4月-12月将属于N类,而1月-3月将属于V类。

因此,预期输出为:

Adv_Code Change_Month Change_Dt     April   May June    July    August  September   October November    December    January     February    March N_OPN V_OPN
A201    April           7/4/2017    0       0   0       1       0       0           0       0           0           0           0           0       0   1
A198    April           7/4/2017    1       1   0       0       3       0           0       0           0           0           0           0       0   5
S1212   May             16/04/2017  0       0   0       0       0       0           0       0           0           0           0           1       0   1
S1213   January         4/1/2018    1       0   0       1       1       1           1       0           1           1           2           1       6   4

我尝试了以下代码:

col <- 4 #Column number from where the months start

df[c("N_OPN", "V_OPN")] <- t(apply(df, 1, function(x) {
  inds <- grep(x[["Change_Month"]], names(x))
  if (as.numeric(substr(x["Change_Dt"], 1, 2)) > 15)
    c(sum(as.numeric(x[col:pmax(col, inds - 1)])), 
      sum(as.numeric(x[inds:ncol(df)])))
  else
    c(sum(as.numeric(x[col:inds])), 
      sum(as.numeric(x[pmin(ncol(df), inds + 1):ncol(df)])))
}))

但这给出了:

Adv_Code Change_Month Change_Dt     April   May June    July    August  September   October November    December    January     February    March N_OPN V_OPN
A201    April           7/4/2017    0       0   0       1       0       0           0       0           0           0           0           0       0   1
A198    April           7/4/2017    1       1   0       0       3       0           0       0           0           0           0           0       1   5
S1212   May             16/04/2017  0       0   0       0       0       0           0       0           0           0           0           1       0   1
S1213   January         4/1/2018    1       0   0       1       1       1           1       0           1           1           2           1       7   3

我不确定为什么会这样。 有人可以帮我吗?

下面是用于重现数据帧的代码:

df <- structure(list(Adv_Code = structure(c(2L, 1L, 3L,4L), .Label = c("A198","A201", "S1212","S1213"), class = "factor"),
                        Change_Dt = structure(c(2L,3L, 1L,1L), .Label = c("07/04/2017", "07/04/2017", "16/04/2017","4/1/2018"), class = "factor"), 
                        Change_Month = structure(1:4, .Label = c("April", "April","May","January"), class = "factor"), April = c(0L, 1L, 0L,1L), 
                        May = c(0L, 1L, 0L,0L), June = c(0L, 0L, 0L,0L), July = c(1L, 0L,0L,1L),
                        August = c(0L, 3L, 0L,1L), September = c(0L,0L, 0L,1L), October = c(0L, 0L, 0L,1L), November = c(0L,0L, 0L,0L), 
                        December = c(0L, 0L, 0L,1L), January = c(0L,0L, 0L,1L), February = c(0L, 0L, 0L,2L), March = c(0L,0L, 1L,1L)), class = "data.frame", row.names = c(NA, -4L))

1 个答案:

答案 0 :(得分:1)

带有each()循环的选项是

for