简体   繁体   English

将 dataframe 拆分为嵌套数据框和矩阵的列表

[英]Split a dataframe into a list of nested data frames and matrices

I'd like to split the diamonds data frame into a list of 5 dataframe, group by cut .我想将diamonds数据框拆分为 5 dataframe 的列表,按cut分组。 This instruction got me started.这个指令让我开始了。 https://dplyr.tidyverse.org/reference/group_split.html https://dplyr.tidyverse.org/reference/group_split.html

diamonds_g <- diamonds%>% group_split(cut)%>% setNames(unique(diamonds$cut))

My desired output is a list of 5 nested lists.我想要的 output 是 5 个嵌套列表的列表。 Each nested list contains one data frame and one matrix, such that:每个嵌套列表包含一个数据框和一个矩阵,这样:

View(diamonds_g[[1]])
factors <- diamonds_g[[1]][2:4]
mat <- diamonds_g[[1]][6:10]

So each of the nested list (or each cut ) contains one data frame of n rows (depending on how many diamonds are classified as that cut) named factors by 3 columns, and one matrix of n rows by 10 columns named mat.因此,每个嵌套列表(或每个cut )都包含一个 n 行的数据框(取决于有多少钻石被归类为该 cut),由 3 列命名为 factors,以及一个由 n 行 x 10 列命名的矩阵,名为 mat。 In other words, the lowest level of the list (the nested matrix and data frame) should have identical names across the 5 nested lists.换句话说,列表的最低级别(嵌套矩阵和数据框)在 5 个嵌套列表中应该具有相同的名称。 How do I proceed?我该如何进行?

Thank you.谢谢你。

Do you mean something like this?你的意思是这样的吗?

result <- lapply(diamonds_g, function(x) 
                 list(factors = x[2:4], mat = as.matrix(x[6:10])))

We can use tidyverse我们可以使用tidyverse

library(dplyr)
library(purrr)
result <- map(diamonds_g, ~ list(factors = .x[2:4], mat = as.matrix(.x[6:10])))

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

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