data <- structure(list(
x = c(1, 2, 1, 2, 2, 1, 3, 3, 1),
y = c(20, 30, 40, 10, 15, 34, 57, 72, 12)),
class = "data.frame",
row.names = c(NA,-9L))
Hi guys, I want to create a new variable from above data.frame in rstudio but it doesn't work. what I want to do is the same of this command in stata but in rstudio
gen var = y*3600 if x == 1
so I runned this r command but it didn´t work:
df$var[df$x == 1] <- df$y*3600
the new variable should look like this:
| x | y | var |
|---|---|---|
| 1 | 20 | 72000 |
| 2 | 30 | NA |
| 1 | 40 | 144000 |
| 2 | 10 | NA |
| 2 | 15 | NA |
| 1 | 34 | 122400 |
| 3 | 57 | NA |
| 3 | 72 | NA |
| 1 | 12 | 43200 |
I appreciate any help and thanks in advance