问题
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.
回答1:
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()
argumentcor
errored. ℹcor
ismap(cor, ~select(.x, -rowname))
. ℹ The error occured in row 1. ✖ no applicable method for 'select_' applied to an object of class "character" Runrlang::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]>
回答2:
Oh my, it was as simple as:
cor_dat <- dat %>%
group_by(grp) %>%
condense(cor = correlate(cur_data())) %>%
mutate(cor = list(cor[,-1]))
Got there by trial & error. Although this only works with column positions, not names.
来源:https://stackoverflow.com/questions/60270144/tidyverse-delete-a-column-within-a-nested-column-list