Home > Net >  How to use case_when in nested for loops in R
How to use case_when in nested for loops in R

Time:06-22

I am trying to use for loops with a case when to assign values to a new column in my dataframe based off of values in the x, y, and z columns. When I run the loop it returns NA for all rows except for the where the final values where i and j match y and z.

for (i in 0:5){
  for (j in 1:6){
    df<- df%>% mutate(state_prob = case_when(
    x == 1 & y == i & z == j ~ table[i 1,c(j)],
    x == 1 & y >= 6 ~ 0,
    x == 0 ~ 0 ))
  }}

I would like for it to go from this:

x y z
1 0 3
0 0 4
1 4 1
1 1 6

To adding a column based off of the results of the values of x,y,x like this, where 0.65 = table[1,c(3)]:

x y z state_prob
1 0 3 0.65
0 0 4 0
1 4 1 0.03
1 1 6 0.09

What it is returning is this because the final iteration for i and j match the values for y and z:

x y z state_prob
1 0 3 NA
0 0 4 0
1 4 1 NA
1 1 6 0.09

Table structure:

structure(c(0.0935083435083435, 0.0214692714692715, 0.00529100529100529, 
0.00152625152625153, 0.000610500610500611, 0.000203500203500203, 
0, 0.000101750101750102, 0.0439130434782609, 0.00478260869565217, 
0.000434782608695652, 0, 0, 0, 0, 0, 0.0275735294117647, 0.00183823529411765, 
0, 0, 0, 0, 0, 0, 0.065, 0.00875, 0.00375, 0, 0, 0.00125, 0.00125, 
0, 0.0484581497797357, 0.00720865038045655, 0.000800961153384061, 
0.00040048057669203, 0, 0, 0, 0, 0.0526914329037149, 0.00530705079605762, 
0.00113722517058378, 0, 0, 0, 0, 0), .Dim = c(8L, 6L), .Dimnames = structure(list(
    c("1", "2", "3", "4", "5", "6", "7", "8"), c("CO", "IL", 
    "IN", "KS", "MO", "TN")), .Names = c("", "")), class = "table")

CodePudding user response:

This is what I ended up using that worked

for(k in 0:1){
  for (j in 1:6){
    for (i in 0:4){
      df$state_prob[stri_detect_fixed(df$x, k)& stri_detect_fixed(df$z, j) & stri_detect_fixed(df$y, i)] <- k*table[i 1,c(j)]
  • Related