mardi 16 août 2022

Assigning a weighted random variable to a new column in an R dataframe

I have a Dataframe that looks like this in R:

df1 |date|location|daytype| |----|---|---| |2022-9-1|NT|Thur| |2022-9-2|NT|Fri| |2022-9-3|AP|Sat| |2022-9-4|AP|Sun| |2022-9-5|NT|Mon|

I want to create a new column for either a morning or an afternoon shift based on random weight sampling:

df2 |shift|weight| |---|---| |Morning|0.8| |Evening|0.2|

Is there a way to do this?

df1$shift <- sample(df2, prob = df$weight)




Aucun commentaire:

Enregistrer un commentaire