I am trying to create a random set and one of the variables I need is a randomly selected date. The problem that I have is that the output of the sample_n is a tibble itself. This becomes problematic when I am trying to bind by row multiple sets.
Please see the example below.
library(tidyverse)
dts <-
tibble(date=seq(as.Date("2020-07-01"),as.Date("2020-07-31"),by="1 days")) %>%
mutate(wday=weekdays(date)) %>%
filter(wday!="Saturday" & wday!="Sunday")
dt1 <- tibble(
date=sample_n(dts[1], 5),
x=runif(5, 10, 20))
glimpse(dt1)
dt2 <- tibble(
date=sample_n(dts[1], 4),
x=runif(4, 10, 20))
glimpse(dt2)
From this you can see that the variable date in dt1 and dt2 is a tibble. I was expecting this to be just a date variable.
This becomes problematic when I am trying to combine these two sets.
dt <- dt1 %>% bind_rows(dt2)
When I run the last line I get the following error message:
Error: Argument 1 can't be a list containing data frames
Thanks
You can just pull
the result to convert it into a vector:
dts %>% sample_n(5) %>% pull(date)
[1] "2020-07-22" "2020-07-08" "2020-07-03" "2020-07-15" "2020-07-17"
Why not just use sample()
in base
? dplyr::sample_n()
is to sample rows of a dataset. In your case you only need to sample a vector, ie dts$date
, so dplyr::sample_n()
is a detour.
dt1 <- tibble(
date = sample(dts$date, 5),
x = runif(5, 10, 20)
)
dt1
# # A tibble: 5 x 2
# date x
# <date> <dbl>
# 1 2020-07-09 13.0
# 2 2020-07-17 18.0
# 3 2020-07-13 16.7
# 4 2020-07-28 16.5
# 5 2020-07-03 14.6
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.