Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

error when using ifelse to create new columns in R

$
0
0

I am trying to generate some random data, where the value of the cells in 4 columns should be either 1 or 0. But for each row, only one of the four columns can be 1. The chance should be equally distributed, so about 25% for each column. I've tried to do this using ifelse, but every time i run it, one of the columns always comes up with only zeros. It's a different column every time, too. Can anyone help me with this?

df <- data.frame(index = c(1,2,3,4,5,6,7,8,9,10), stringsAsFactors = F)

df$var1 <- sample(c(0,1), nrow(df), prob = c(0.75,0.25), replace = T)
df$var2 <- ifelse(df$var1 == 1, 0, sample(c(0,1), prob = c(0.67,0.33), replace = T))
df$var3 <- ifelse(df$var1 == 1 | df$var2 == 1, 0, sample(c(0,1), prob = c(0.5,0.5), replace = T))
df$var4 <- ifelse(df$var1 == 1 | df$var2 == 1 | df$var3 == 1, 0, 1)

I'm trying to generate something like this:

+-------+------+------+------+------+
| index | var1 | var2 | var3 | var4 |
+-------+------+------+------+------+
|     1 |    0 |    1 |    0 |    0 |
|     2 |    1 |    0 |    0 |    0 |
|     3 |    0 |    1 |    0 |    0 |
|     4 |    0 |    1 |    0 |    0 |
|     5 |    0 |    0 |    1 |    0 |
|     6 |    0 |    1 |    0 |    0 |
|     7 |    0 |    0 |    0 |    1 |
|     8 |    0 |    0 |    0 |    1 |
|     9 |    1 |    0 |    0 |    0 |
|    10 |    0 |    0 |    1 |    0 |
+-------+------+------+------+------+

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>