I have the following dataset:
set.seed(123)
library(dplyr)
var1 = rnorm(10000, 100,100)
var2 = rnorm(10000, 100,100)
var3 = rnorm(10000, 100,100)
var4 = rnorm(10000, 100,100)
var5 <- factor(sample(c("Yes", "No"), 1000, replace=TRUE, prob=c(0.4, 0.6)))
var6 <- factor(sample(c("Yes", "No"), 1000, replace=TRUE, prob=c(0.4, 0.6)))
my_data = data.frame( var1, var2, var3, var4, var5, var6)
I am trying to run the following code:
my_data %>%
group_by(var5) %>%
mutate(col1 = ntile(var1, 5))
This code works on this sample dataset, but on my real dataset - I always get the following error:
Error: Column 'col1' must be length xyz (the group size) or one, not abc
What kind of strategies can I use to avoid this error?
Thanks!