【问题标题】:Repeated sampling according to a number in a row in R根据R中一行中的数字重复采样
【发布时间】:2020-03-18 17:36:22
【问题描述】:

我有一个问题,我会尽力解释。

假设我有以下数据集,我们称之为 extra_events:

   Year WEvents
     1       1
     2       3
     3       1
     4       2
     5       3
     6       0
     7       3
     8       0
     9       2
    10       3

我有一个不同的数据集,我们称之为weather_loss:

  Event Loss
    11    2
    21    3
    24    5
    27    8
    30   10
    34    7
    37    1
    41   14
    45   15

我的目标是从 weevents 列中随机抽取 weather_loss(事件列)到 extra_events 中每一行的次数(当然要替换)

例如,输出将如下所示:

   Year Wevents Sim1 Sim2 Sim3
     1       1   21   NA   NA
     2       3   24   30   37
     3       1   11   NA   NA
     4       2   45   41   NA
     5       3   30   34   37
     6       0   NA   NA   NA
     7       3   24   27   34
     8       0   NA   NA   NA
     9       2   37   45   NA
    10       3   11   21   30

通过这种方式,我可以看到每年我必须采样多少事件以及采样了哪些事件。

谁能帮我解决这个问题。我不一定需要有 NA。

【问题讨论】:

  • 我很困惑。您希望 extra_events 中的每一行都有来自 weather_loss 的 3 个不同的随机事件值吗?如果是这样,为什么是“NA”?
  • 另外,你用的是什么数据库?
  • 啊,对不起,重读后,我现在明白了这个问题。 WEvents 中的值是否有上限,还是任意的?
  • 这是任意的,我是从泊松分布中采样的。对于 WEvents,它可以永远持续下去。
  • 在这种情况下,返回的列数是非常动态的,那么您是否可以将每个 Sim 值作为单独的行,或者甚至是逗号分隔的字符串?

标签: r


【解决方案1】:

这是tidyverse 的一个选项,其中我们循环覆盖“WEvents”,sample 基于“WEvents”中的值的“事件”,作为list 列返回,然后使用unnest_widerpurrr 创建多个列

library(dplyr)
library(purrr)
extra_events %>% 
    mutate(Sim = map(WEvents, ~ sample(weather_loss$Event, .x) %>% 
                     as.list)) %>%
    unnest_wider(c(Sim)) %>%
    rename_at(vars(starts_with('..')), ~ str_c('Sim', seq_along(.)))
# A tibble: 10 x 5
#    Year WEvents  Sim1  Sim2  Sim3
#   <int>   <int> <int> <int> <int>
# 1     1       1    37    NA    NA
# 2     2       3    24    37    41
# 3     3       1    30    NA    NA
# 4     4       2    34    30    NA
# 5     5       3    45    21    11
# 6     6       0    NA    NA    NA
# 7     7       3    37    11    34
# 8     8       0    NA    NA    NA
# 9     9       2    27    24    NA
#10    10       3    45    27    11

或使用base R

lst1 <- lapply(extra_events$WEvents, function(x) sample(weather_loss$Event, x))
mx <- max(lengths(lst1))    
extra_events[paste0("Sim_", seq_len(mx))] <- do.call(rbind, 
              lapply(lst1, `length<-`, mx))

数据

extra_events <- structure(list(Year = 1:10, WEvents = c(1L, 3L, 1L, 2L, 3L, 0L, 
3L, 0L, 2L, 3L)), class = "data.frame", row.names = c(NA, -10L
))

weather_loss <- structure(list(Event = c(11L, 21L, 24L, 27L, 30L, 34L, 37L, 41L, 
45L), Loss = c(2L, 3L, 5L, 8L, 10L, 7L, 1L, 14L, 15L)), 
    class = "data.frame", row.names = c(NA, 
-9L))

【讨论】:

  • 非常感谢您的快速回复。我尝试使用您的第一个代码,但它无法识别 unnest_wider,我确实安装了 purrr 但它没有看到该功能。
  • @wdavid47 你能检查一下你的packageVersion('purrr')
  • @wdavid47 我正在使用packageVersion('purrr')# [1] ‘0.3.3’
  • 非常感谢。我在我的个人电脑上检查了它,它工作正常。
  • 我做了,但它不会显示,因为我的声望低于 15。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
相关资源
最近更新 更多