Создайте новую переменную на основе условия mutilpe и цикла в R - PullRequest
0 голосов
/ 17 ноября 2018

У меня есть набор данных, который содержит 4 переменные, такие как - "ID", "V", "value", "weight".

  ID V     value     weight
   A 1  8723.286 0.12183436
   A 0  8889.905 0.09787817
   A 1 14984.370 1.00000000
   B 1  8176.189 0.12183436
   B 1  8342.808 0.09787817
   B 1 14437.272 0.18412047

Я хочу вычислить переменную с именем "output".Логика расчета выхода:

Для каждого идентификатора, если V1 = 1 и V2=0, то output = value1 * weight1 + value2*(1-weight1).
, если V1=1 и V2=1, тогда output = value1 * weight1 + [(value2 + value3)/2]*(1-weight1)

Результат будет таким:

    ID  V      value        weight       output
     A  1   8723.286    0.12183436  8869.605081
     A  0   8889.905    0.09787817  8869.605081
     A  1   14984.37    1.00000000  8869.605081
     B  1   8176.189    0.12183436  10998.48252
     B  1   8342.808    0.09787817  10998.48252
     B  1   14437.272   0.18412047  10998.48252

Я пробовал таким образом:

      dat <- data.frame(
        ID = rep(c("A","B"), each=3),
        V  = c(1,0,1,1,1,1)  ,
        value = c(8723.286, 8889.905, 14984.37, 8176.189, 8342.808, 14437.272),
        weight = c(0.12183436, 0.09787817, 1.00000000, 0.12183436, 0.09787817, 0.18412047)
      )

    dats <- split(dat, dat$ID)

ifelse(dats[[1]]$V[1]==1 & dats[[1]]$V[2]==0, dats[[1]]$weight[1]*dats[[1]]$value[1]+(1-dats[[1]]$weight[1])*dats[[1]]$value[2], NA)

ifelse(dats[[2]]$V[1]==1 & dats[[2]]$V[2]==1, dats[[2]]$weight[1]*dats[[2]]$value[1]+(1-dats[[2]]$weight[1])*((dats[[2]]$value[2]+dats[[2]]$value[3])/2), NA) 

Как я могу сделать это с помощью цикла?Спасибо.

1 Ответ

0 голосов
/ 17 ноября 2018

Мое предложение (могут быть более разумные способы), прокомментированное в исходном коде на основе ваших данных. Frame:

# create your data.frame plus a record producing NA
dat <- data.frame(
       ID = rep(c("A","B","C"), each=3),
       V  = c(1,0,1,1,1,1,0,0,1),
       value = c(8723.286, 8889.905, 14984.37, 8176.189, 8342.808, 14437.272,10,20,30),
       weight = c(0.12183436, 0.09787817, 1.00000000, 0.12183436, 0.09787817, 0.18412047,0.1,0.2,0.3))

# display the numbers as per your sample provided
options( digits = 10 )

# extract the unique ID strings (factors levels, actually)
IDs <- unique( dat$ID )

# loop through these IDs
for( i in IDs )
{
    # get the row numbers for the three rows with the record data
    idx <- which( dat$ID == i )
    # check if your first condition applies
    if(  dat$V[idx[ 1 ] ] == 1 & dat$V[idx[ 2 ] ] == 0 )
         # if that's the case, fill the three output column rows with the calculated value
         dat$output[ idx ] <- dat$value[ idx[ 1 ] ] * dat$weight[ idx[ 1 ] ] +
                              dat$value[ idx[ 2 ] ] * ( 1 - dat$weight[ idx[ 1 ] ] )
    # if the other case is true
    else if(  dat$V[idx[ 1 ] ] == 1 & dat$V[idx[ 2 ] ] == 1 )
         dat$output[ idx ] <- dat$value[ idx[ 1 ] ] * dat$weight[ idx[ 1 ] ] +
                              ( dat$value[ idx[ 2 ] ] + dat$value[ idx[ 3 ] ] ) / 2 *
                              ( 1 - dat$weight[ idx[ 1 ] ] )
    # fallback
    else
        dat$output[ idx ] <- NA
}

dat
  ID V     value     weight       output
1  A 1  8723.286 0.12183436  8869.605081
2  A 0  8889.905 0.09787817  8869.605081
3  A 1 14984.370 1.00000000  8869.605081
4  B 1  8176.189 0.12183436 10998.482520
5  B 1  8342.808 0.09787817 10998.482520
6  B 1 14437.272 0.18412047 10998.482520
7  C 0    10.000 0.10000000           NA
8  C 0    20.000 0.20000000           NA
9  C 1    30.000 0.30000000           NA
...