Home > Net >  Update values conditionally in for loop
Update values conditionally in for loop

Time:04-21

I wondered why I can't replicate the first example with a for loop. More specifically you can see that if we try to execute the logic with a for loop the second and third row are not populated with a new value. Why is this?

example to copy:

df <- data.table(name=c('a', 'b', 'c'))

df[name=='a', x:=1]
df[name=='b', x:=2]
df[name=='c', x:=3]

df

####

df <- data.table(name=c('a', 'b', 'c'))

values <- unique(df$name)

for (x in 1:3) {
  df[name==values[x], x:=x]
}

df

my output:

> df <- data.table(name=c('a', 'b', 'c'))
> df[name=='a', x:=1]
> df[name=='b', x:=2]
> df[name=='c', x:=3]
> df
   name x
1:    a 1
2:    b 2
3:    c 3
> df <- data.table(name=c('a', 'b', 'c'))
> values <- unique(df$name)
> for (x in 1:3) {
    df[name==values[x], x:=x]
  }
> df
> df
   name  x
1:    a  1
2:    b NA
3:    c NA

CodePudding user response:

This is a scope problem, the first step in the loop defines x, and subsequent steps use this definition (which is equal to NA for rows 2 and 3).

If you use i instead of x, it works :

for (i in 1:3) {
  df[name==values[i], x:=i]
}

df
Index: <name>
     name     x
   <char> <int>
1:      a     1
2:      b     2
3:      c     3
  • Related