Reputation: 13
I have this data frame and I' trying to create a new column considering the data in the first column and the column called "zero", I've been using sample(), for an interval from (0,df$first) but It gaves me a number between 0 and the data in the first column, not one(0) or another (as an example 9).
Initial data frame:
> df
first zero
1 9 0
2 8 0
3 16 0
What I want is something like this:
> df
first zero new
1 9 0 9
2 8 0 0
3 16 0 16
I really appreciate your answers or opinions. Thank you!
Upvotes: 0
Views: 240
Reputation: 101548
I am not sure if you want this
set.seed(1)
df$new <- df[cbind(seq(nrow(df)),
sample(2,nrow(df),replace = TRUE))]
such that
> df
first zero new
1 9 0 9
2 8 0 0
3 16 0 16
DATA
df <- structure(list(first = c(9, 8, 16), zero = c(0, 0, 0)), class = "data.frame", row.names = c(NA,
-3L))
Upvotes: 1