Ryan
Ryan

Reputation: 23

Random number assignment within ifelse function not returning expected results?

I have a dataset (summer2) with a collection of points, each one is assigned a boolean "Use" value (0 for random point, 1 for used point) and a Unique ID (UniqueID) between 1 and 747. I want to create a new column, "UniqueID_2", in which if it is a used point (Use = 1) I want it to equal the UniqueID field. If Use = 0, I want to assign a new random ID number between 728-747.

I have an ifelse function with the sample function nested within as the third argument, and when I run it I get values from 348-736, when every value should be between 728-747.

I am pretty new to R so I assume it's a syntax error or perhaps I am just using an improper function.

summer2["UniqueID_2"]
summer2$UniqueID_2 = ifelse(summer2$Use == 1, summer2$UniqueID, sample(727:748, 1))

Upvotes: 1

Views: 108

Answers (1)

John Coleman
John Coleman

Reputation: 51998

The third argument that you pass to ifelse should be a vector of the same length as the other two, otherwise recycling will kick in (with counter-intuitive results when you recycle a single random number). You need:

ifelse(summer2$Use == 1, summer2$UniqueID, sample(727:748, length(summer2$Use), replace = TRUE)

Upvotes: 1

Related Questions