I have a nest of tables whereby I want to convert the rowname into the row cells that its in and order each column accordingly by the rank before its replaced by the rowname.
For example:
mtX <- apply(mtcars, 2, as.numeric) %>% data.frame(.)
rownames(mtX) <- rownames(mtcars)
mtNest <- mtX %>% nest_by(vs) %>%
ungroup %>%
mutate(data = map(data, ~ .x %>%
nest(data = -am) %>%
mutate(data = map(
data, ~ .x %>%
nest(data = -gear) %>% mutate(data = map(
data, ~ .x %>% mutate(across(., function(x)
min_rank(-x))) %>% rownames(data)[order(-.x)]
))
))))
This is similar to my previous post: Descend each column and update with row names
However, I keep getting the following error:
Error in
mutate()
: ! Problem while computingdata = map(...)
. Caused by error inmutate()
: ! Problem while computingdata = map(...)
. Caused by error inmutate()
: ! Problem while computingdata = map(...)
. Caused by error inmutate()
: ! Problem while computing..1 = across(., function(x) min_rank(-x))
. Caused by error inacross()
: ! Must subset columns with a valid subscript vector. ✖ Subscript has the wrong type `tbl_df< mpg : double cyl : double disp: double hp : double drat: double wt : double qsec: double carb: double
CodePudding user response:
With tibble/data.table
, it wouldn't allow row.names other than the index. Thus, when we apply modifications on the dataset, it automatically replaces the row names with index when a function returns a tibble (here nest
returns a tibble). Therefore, it is better to create a row names column (rownames_to_column
from tibble
) before doing the nesting. Based on the code, we are recursively creating nested data. In the innermost nested 'data', we loop over the columns that are numeric
, get the rank and replace with the 'rn' column
library(dplyr)
library(purrr)
library(tibble)
out <- mtX %>%
rownames_to_column("rn") %>%
nest_by(vs) %>%
ungroup %>%
mutate(data = map(data, ~ .x %>%
nest(data = -am) %>%
mutate(data = map(
data, ~ .x %>%
nest(data = -gear) %>%
mutate(data = map(
data, ~ .x %>%
mutate(across(where(is.numeric), function(x) rn[min_rank(-x)])) ))))))
-output
> out
# A tibble: 2 × 2
vs data
<dbl> <list>
1 0 <tibble [2 × 2]>
2 1 <tibble [2 × 2]>
> out$data[[1]]$data[[1]]$data
[[1]]
# A tibble: 2 × 9
rn mpg cyl disp hp drat wt qsec carb
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Wag Mazda RX4 Wag Mazda RX4
2 Mazda RX4 Wag Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4 Mazda RX4
[[2]]
# A tibble: 4 × 9
rn mpg cyl disp hp drat wt qsec carb
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Porsche 914-2 Porsche 914-2 Maserati Bora Maserati Bora Maserati Bora Porsche 914-2 Maserati Bora Porsche 914-2 Maserati Bora
2 Ford Pantera L Ferrari Dino Porsche 914-2 Porsche 914-2 Ford Pantera L Ford Pantera L Ford Pantera L Maserati Bora Ferrari Dino
3 Ferrari Dino Ford Pantera L Ferrari Dino Ferrari Dino Ferrari Dino Ferrari Dino Ferrari Dino Ford Pantera L Ford Pantera L
4 Maserati Bora Maserati Bora Porsche 914-2 Ford Pantera L Porsche 914-2 Maserati Bora Porsche 914-2 Ferrari Dino Porsche 914-2