Skip to content

Instantly share code, notes, and snippets.

@toyeiei
Last active Aug 15, 2019
Embed
What would you like to do?
generate data R for Excel users
## generate vectors
set.seed(99)
id <- 1:100
student_name <- replicate(100, paste0(sample(LETTERS, size = 5, replace = T), collapse = ""))
gender <- factor(sample(c(0,1), size = 100, replace = T, prob = c(.7, .3)),
labels = c("M", "F"))
age <- sample(19:32, size = 100, replace = T)
gpa <- rnorm(n = 100, mean = 3.15, sd = 0.25)
gpa <- ifelse(gpa > 4, 4.00, gpa)
nationality <- factor(sample(c(0,1,2), size = 100, replace = T, prob = c(0.5, 0.3, 0.2)),
labels = c("TH", "US", "CH"))
## create a dataframe from vectors
df <- data.frame(id, student_name, gender, age, gpa, nationality)
head(df)
## create a lookup table in this example
set.seed(99)
us_students <- df %>% filter(nationality == "US") %>% .[["id"]]
lookup_state <- data.frame(us_students,
states = sample(state.name, size = length(us_students), replace = TRUE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment