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

时间:2019-01-19 13:59:01

标签: r

我的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()

我尝试:

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

错误:无法将列表转换为函数

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

UseMethod(“ tbl_vars”)中的错误:     没有适用于“ tbl_vars”的适用方法应用于“ rlang_data_pronoun”类的对象

在其他尝试中...

那么,如何将dplyr函数应用到list-column中呢?

1 个答案:

答案 0 :(得分:0)

一种解决方案是在列表列(lbl = load_img(im) lbl = scipy.misc.imresize(lbl, (self.image_shape[1], self.image_shape[0])) bg_color = np.array([255, 255, 255]) building_color = np.array([255, 0, 0]) road_color = np.array([0, 0, 255]) gt_bg = np.all(lbl == bg_color, axis=2) building_bg = np.all(lbl == building_color, axis=2) road_bg = np.all(lbl == road_color, axis=2) building_bg = gt_bg.reshape(*building_bg.shape, 1) road_bg = gt_bg.reshape(*road_bg.shape, 1) gt_bg = gt_bg.reshape(*gt_bg.shape, 1) lbl = np.concatenate((gt_bg, building_bg, road_bg), axis=2) return np.array([lbl]) )中使用dplyr::pull

obj$data