简体   繁体   English

在 R 中,如何在不依赖任何先前列的小标题中创建一个新的列表列变量?

[英]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.

相关问题 如何使用 tibble::tribble() 创建列表列? - How to create a list-column with tibble::tribble()? 如何创建一个列表列,它是 R 中另一个列表列的子集? - How can I make a list-column that is a subset of another list-column in R? 小标题中的列表列:我可以将一个列表列与另一个列表列链接吗? - List-columns in tibbles: Can I link a list-column with another list-column? 如何使用huxtable()在小标题中使用回归模型的打印列表列 - How do I use print list-column of regression models in a tibble using huxtable() 在 tibble 中,通过改变新的列表列从命名列表中提取名称 - In a tibble, extract names from named lists by mutating a new list-column 如何在嵌套列表列中的 tibble 的字符列和行之间粘贴字符串 - How to paste strings between tibble's character column and rows in a nested list-column 将列表列添加到每个元素都是另一个列表列的第一个元素的 tibble - Adding a list-column to a tibble in which each element is the first element of another list-column 如何重新编码<null>单元格嵌套 NA (<lgl [1]> ) 在 tibble 的列表列中?</lgl></null> - How to recode <NULL> cells to nested NA (<lgl [1]>) in a tibble's list-column? 将 tibbles 的列表列添加到基于过滤器 quosures 的列表列的 tibble function - Add list-column of tibbles to a tibble based on a list-column of filter quosures without helper function R - 如何在 data.table 中执行列表列操作 - R - How to perform list-column operations in data.table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM