jeudi 11 avril 2019

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))

Aucun commentaire:

Enregistrer un commentaire