tidyverse - 删除嵌套列/列表中的列 [英] tidyverse - delete a column within a nested column/list

查看:107
本文介绍了tidyverse - 删除嵌套列/列表中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

(注意:我在 tidyverse 中使用 dplyr 的当前 github 版本,它提供了一些新的实验功能,例如 condense - 我在下面使用,但我认为这与我的无关问题/疑问).

图书馆(tidyverse)图书馆(更正)dat <- data.frame(grp = rep(1:4, each = 25),Q1 = 样本(c(1:5, NA), 100, 替换 = TRUE),Q2 = 样本(c(1:5, NA), 100, 替换 = TRUE),Q3 = 样本(c(1:5, NA), 100, 替换 = TRUE),Q4 = 样本(c(1:5, NA), 100, 替换 = TRUE),Q5 = 样本(c(1:5, NA), 100, 替换 = TRUE),Q6 = 样本(c(1:5, NA), 100, 替换 = TRUE))

我现在想计算每个组内 Q1 到 Q6 之间的相关性,我正在使用:

cor_dat <- dat %>%group_by(grp) %>%浓缩(cor = correlate(cur_data()))

哪个给了我作为列表列(?)的相关性.在每个列表中,第一列称为 rowname,我想简单地以一种整洁的方式从每个列表中删除此列.我该怎么做?

我已经尝试过像 select (-rowname) 这样的幼稚方法,但这不起作用.

解决方案

如果我们删除组属性,@r2evans 的建议会起作用

库(dplyr)图书馆(咕噜咕噜)cor_dat %>%取消分组 %>%变异(cor = map(cor,〜选择(.x,-rowname)))# 小费:4 x 2# grp cor# <int><列表>#1 1 #2 2 <tibble [6 × 6]>#3 3 <tibble [6 × 6]>#4 4 <tibble [6 × 6]>

当有组属性时,导致报错

cor_dat %>%变异(cor = map(cor,〜选择(.x,-rowname)))

<块引用>

错误:mutate() 参数 cor 出错.ℹ cormap(cor, ~select(.x, -rowname)).ℹ 错误发生在第 1 行.✖ 没有适用于 'select_' 的方法应用于类字符"的对象运行 rlang::last_error() 以查看错误发生的位置.

如果我们提取为列,这与相同的行为一致

cor_dat$cor %>%地图(~ .x %>% 选择(-rowname))

<小时>

或者如果我们想让它更短,它可以在 condense 本身内完成,因为 correlate 根据文档添加了一个 rowname

dat %>%group_by(grp) %>%浓缩(cor = correlate(cur_data())%>%选择(-rowname))# 小费:4 x 2# 逐行:grp# grp cor# <int><列表>#1 1 #2 2 <tibble [6 × 6]>#3 3 <tibble [6 × 6]>#4 4 <tibble [6 × 6]>

I have the following data:

(Note: I'm using the current github version of dplyr within tidyverse which offerse some new experimental functions, like condense - which I'm using below, but I think that's not relevant for my problem/question).

library(tidyverse)
library(corrr)

dat <- data.frame(grp = rep(1:4, each = 25),
                  Q1 = sample(c(1:5, NA), 100, replace = TRUE),
                  Q2 = sample(c(1:5, NA), 100, replace = TRUE),
                  Q3 = sample(c(1:5, NA), 100, replace = TRUE),
                  Q4 = sample(c(1:5, NA), 100, replace = TRUE),
                  Q5 = sample(c(1:5, NA), 100, replace = TRUE),
                  Q6 = sample(c(1:5, NA), 100, replace = TRUE))

I now want to calculate the correlation between Q1 to Q6 within each group, and I'm using:

cor_dat <- dat %>%
  group_by(grp) %>%
  condense(cor = correlate(cur_data()))

Which gives me the correlations as a list-column (?). Within each list, the first column is called rowname and I want to simply delete this column from each list in a tidyverse way. How can I do this?

I already tried something naive like select (-rowname), but this doesn't work.

解决方案

The suggestion by @r2evans would work if we remove the group attribute

library(dplyr)
library(purrr)
cor_dat %>%
     ungroup %>%
     mutate(cor = map(cor, ~ select(.x, -rowname)))
# A tibble: 4 x 2
#    grp cor             
#  <int> <list>          
#1     1 <tibble [6 × 6]>
#2     2 <tibble [6 × 6]>
#3     3 <tibble [6 × 6]>
#4     4 <tibble [6 × 6]>

When there is a group attribute, it results in error

cor_dat %>% 
    mutate(cor = map(cor, ~ select(.x, -rowname)))   

Error: mutate() argument cor errored. ℹ cor is map(cor, ~select(.x, -rowname)). ℹ The error occured in row 1. ✖ no applicable method for 'select_' applied to an object of class "character" Run rlang::last_error() to see where the error occurred.

which is consistent with the same behavior if we extract as a column

cor_dat$cor %>% 
           map(~ .x %>% select(-rowname))


Or if we want to make it shorter, it can be done within condense itself because correlate adds a rowname column as per the documentation

dat %>%
  group_by(grp) %>%
  condense(cor = correlate(cur_data()) %>%
                              select(-rowname))
# A tibble: 4 x 2
# Rowwise:  grp
#    grp cor             
#  <int> <list>          
#1     1 <tibble [6 × 6]>
#2     2 <tibble [6 × 6]>
#3     3 <tibble [6 × 6]>
#4     4 <tibble [6 × 6]>

这篇关于tidyverse - 删除嵌套列/列表中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆