简体   繁体   English

如何将dplyr函数应用于列表列?

[英]How apply dplyr functions into a list-column?

My list-column : 我的list-column

library(tidyverse)

dataset<-as_tibble(matrix(rnorm(6*30,1000,100),ncol=6))
cluster<-kmeans(dataset,centers=3)
dataset$kmeans<-as.factor(cluster[['cluster']])
mylist<-split(dataset,dataset$kmeans)
names(mylist)<-str_c('dataset',seq_along(mylist))

obj<-dataset%>%
  group_by(kmeans)%>%
  nest()

I try: 我尝试:

obj%>%
  summarise_if(.data,is.numeric,sum)

Error: Can't convert a list to function 错误:无法将列表转换为函数

and

obj%>%
  map(~summarise_if(.data,is.numeric,sum))

Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "rlang_data_pronoun" UseMethod(“ tbl_vars”)中的错误:没有适用于“ tbl_vars”的适用方法应用于类“ rlang_data_pronoun”的对象

among other attempts... 在其他尝试中...

So, how do I apply dplyr functions into a list-column ? 那么,如何将dplyr函数应用于list-column

A solution is use dplyr::pull in list-column ( obj$data ): 一种解决方案是在列表列( obj$data )中使用dplyr::pull

library(tidyverse)

obj%>%
  pull(data)%>%
  map(~summarise_if(.,is.numeric,sum))

[[1]]
# A tibble: 1 x 6
      V1     V2     V3     V4     V5     V6
   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 12865. 11787. 12864. 13443. 12548. 13170.

[[2]]
# A tibble: 1 x 6
      V1     V2     V3     V4     V5     V6
   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 11655. 12197. 11379. 10023. 10659. 11154.

[[3]]
# A tibble: 1 x 6
     V1    V2    V3    V4    V5    V6
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5175. 6053. 6855. 6130. 6504. 5967.

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

相关问题 如何“传播”列表列? - how to “spread” a list-column? R 在列表列工作流中使用 dplyr::select() - R using dplyr::select() in a list-column workflow 如何通过将字符串转换为环境中可用的同名函数来改变包含函数的列表列? - How to mutate a list-column that holds functions by converting strings to functions with the same name that are available in the environment? 如何使用 tibble::tribble() 创建列表列? - How to create a list-column with tibble::tribble()? 如何将ggplots的列表列打印为pdf? - how to print a list-column of ggplots to pdf? 如何创建一个列表列,它是 R 中另一个列表列的子集? - How can I make a list-column that is a subset of another list-column in R? 使用管道在列表中的单个列上应用 dplyr 函数 - Apply dplyr functions on a single column across a list using piping R {dplyr}: `rowwise` 列表列中的`rename` 或`mutate` data.frames 在LHS 上有不同的列名 - R {dplyr}: `rename` or `mutate` data.frames in `rowwise` list-column with different column names on LHS 通过purrr和dplyr按组对小标题列表列的每个元素进行均值 - Mean across each element of a tibble list-column by group with purrr and dplyr 使用R igraph和dplyr(purrr)有条件地在列表列图中设置边属性 - Setting edge attributes conditionally in list-column graphs using R igraph and dplyr (purrr)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM