[英]In R, how can I create a new list-column variable in a tibble that doesn't depend on any prior columns?
This seems like a fairly basic question but I'm strugging to solve it.这似乎是一个相当基本的问题,但我正在努力解决它。 In the simplest form my problem is this: I have a vector of latitudes and a vector of longitudes and I'd like to generate 3 random integers without replacement for each one.在最简单的形式中,我的问题是:我有一个纬度向量和一个经度向量,我想生成 3 个随机整数而不替换每个整数。
The code I want to make in my head runs something like this:我想在脑海中编写的代码是这样的:
library(tidyverse)
LAT = c(0, 10, 20, 30, 40)
LON = c(-180, -120, -60, 0, 60, 120)
Case = c("All", "These", "Are", "Arbitrary")
df <- crossing(LAT, LON, Case) %>%
nest(TotalCases = Case) %>%
mutate(ViewingAngles = sample(0:50, 3, replace=F))
The 'TotalCases' element there is representative of some of the other irrelevant code I have to do on this dataframe, but the effect is the same - I generate a large (in this case 120x3) tibble that nests down to a 30x3 nested tibble. 'TotalCases' 元素代表了我必须在此数据帧上执行的其他一些不相关代码,但效果是相同的 - 我生成了一个大的(在本例中为 120x3)tibble,它嵌套到一个 30x3 的嵌套 tibble。 I'd then like to add another nested list-column to the end on the basis of that Sample function, but the output needs to be generated without replacement (meaning I cannot generate the three values separately) and must be distinct for each lat-lon pair, which means I cannot simply include "ViewingAngles" in my crossing()
call.然后,我想在该 Sample 函数的基础上将另一个嵌套列表列添加到末尾,但输出需要在不替换的情况下生成(这意味着我不能单独生成三个值)并且每个 lat- 必须是不同的lon 对,这意味着我不能简单地在crossing()
调用中包含“ViewingAngles”。
My first inclination was to use some variant of map
but that doesn't work without a .x
and none makes sense to me.我的第一个倾向是使用map
的一些变体,但是没有.x
就行不通,而且对我来说没有任何意义。 I also cannot simply use mutate(ViewingAngles = list(sample(0:50,3,replace=F))
because then each lat-lon pair has the same set of ViewingAngles.我也不能简单地使用mutate(ViewingAngles = list(sample(0:50,3,replace=F))
因为每个 lat-lon 对都有相同的 ViewingAngles 集。
Wut Do?吴做?
Thanks.谢谢。
library(tidyverse)
LAT <- c(0, 10, 20, 30, 40)
LON <- c(-180, -120, -60, 0, 60, 120)
Case <- c("All", "These", "Are", "Arbitrary")
# create the dataframe
df <- crossing(LAT, LON, Case) %>%
nest(TotalCases = Case)
# generate random numbers
lists_numbers <- replicate(nrow(df), sample(0:50, 3, replace = F), simplify = FALSE)
# check whether unique vectors are created for each LON LAT combination
length(unique(as.numeric(sapply(lists_numbers, paste0, collapse = "")))) == nrow(df)
#> [1] TRUE
# create the nested list column
new_df <- df %>%
mutate(ViewingAngles = lists_numbers)
new_df
#> # A tibble: 30 × 4
#> LAT LON TotalCases ViewingAngles
#> <dbl> <dbl> <list> <list>
#> 1 0 -180 <tibble [4 × 1]> <int [3]>
#> 2 0 -120 <tibble [4 × 1]> <int [3]>
#> 3 0 -60 <tibble [4 × 1]> <int [3]>
#> 4 0 0 <tibble [4 × 1]> <int [3]>
#> 5 0 60 <tibble [4 × 1]> <int [3]>
#> 6 0 120 <tibble [4 × 1]> <int [3]>
#> 7 10 -180 <tibble [4 × 1]> <int [3]>
#> 8 10 -120 <tibble [4 × 1]> <int [3]>
#> 9 10 -60 <tibble [4 × 1]> <int [3]>
#> 10 10 0 <tibble [4 × 1]> <int [3]>
#> # … with 20 more rows
Created on 2022-07-07 by the reprex package (v2.0.1)由reprex 包于 2022-07-07 创建 (v2.0.1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.