Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

random - How do I create a column with two values (or names) at a given weight in R

I'm trying to create a column in my data in .dbf. I need to create a column in which 70% is 0 (or TRUE) and 30% is 1 (or FALSE), only at random.

How can I do this?

question from:https://stackoverflow.com/questions/65938871/how-do-i-create-a-column-with-two-values-or-names-at-a-given-weight-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you need for example 100 values, try this:

sample(c(0,1), size = 100, replace = TRUE, prob = c(0.7, 0.3))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...