Varun
Varun

Reputation: 1321

R randomly allocate different values from vector to dataframe column based on condition

I've got a dataset called df of answers to a question Q1

df = data.frame(ID = c(1:5), Q1 = c(1,1,3,4,2))

I also have a vector where each element is a word

words = c("good","bad","better","improved","fascinating","improvise")

My Objective

My Attempt

#If answer to Q1 is 1 or 2, then randomly allocate a word to newly created column "followup"
#Else leave blank
df$followup=ifelse(df$Q1==1 | df$Q1==2,sample(words,1),"")

However doing this leads to repetition of the same randomly selected word for each row that contains a 1 or 2.

  ID Q1    followup
1  1  1 fascinating
2  2  1 fascinating
3  3  3            
4  4  4            
5  5  5  

I'd like every word to be randomized and different.

Any inputs would be highly appreciated.

Upvotes: 1

Views: 814

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

For that we may use

df$followup[df$Q1 %in% 1:2] <- sample(words, sum(df$Q1 %in% 1:2))
df
#   ID Q1  followup
# 1  1  1    better
# 2  2  1 improvise
# 3  3  3      <NA>
# 4  4  4      <NA>
# 5  5  2       bad

Since we are generating those values in a single call, replace = FALSE (the default value) in sample gives the desired result of all the values being different.

Upvotes: 3

Related Questions