简体   繁体   English

将 sample_n output 从 tibble 转换为变量

[英]Convert sample_n output from tibble to just a variable

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.我遇到的问题是 sample_n 的 output 本身就是一个小标题。 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.从中可以看出 dt1 和 dt2 中的变量 date 是一个 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错误:参数 1 不能是包含数据框的列表

Thanks谢谢

You can just pull the result to convert it into a vector:您可以只pull结果以将其转换为向量:

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 ?为什么不在base中使用sample() dplyr::sample_n() is to sample rows of a dataset. dplyr::sample_n()用于对数据集的行进行采样。 In your case you only need to sample a vector, ie dts$date , so dplyr::sample_n() is a detour.在您的情况下,您只需要对向量进行采样,即dts$date ,因此dplyr::sample_n()是绕道而行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM