-1

Here is the dataframe df on which I'm trying to do a pivot using cast function

dput(df)
structure(list(Val = c(1L, 2L, 2L, 5L, 2L, 5L), `Perm  1` = structure(c(1L, 
2L, 3L, 3L, 3L, 3L), .Label = c("Blue", "green", "yellow"
), class = "factor"), `Perm  2` = structure(c(1L, 2L, 2L, 3L, 
3L, 3L), .Label = c("Blue", "green", "yellow"), class = "factor"), 
    `Perm  3` = structure(c(1L, 2L, 2L, 2L, 3L, 3L), .Label = c("Blue", 
    "green", "yellow"), class = "factor")), .Names = c("Val", 
"Perm  1", "Perm  2", "Perm  3"), row.names = c(NA, 6L), class = "data.frame")

And expecting the data after pivot

Blue       1    1    1
green      2    4    9
yellow     14   12   7

I tried doing

cast(df, df$Val ~ df$`Perm  1`+df$`Perm  2`+df$`Perm  3`, sum, value = 'Val')

But this gives error

Error: Casting formula contains variables not found in molten data: df$Val, df$`Perm1`, df$`Perm2`

How can I be able to do pivot so that I'll be able to get the desired O/P

P.S- The dataframe DF has around 36 column but for simplicity I took only 3 columns. Any suggestion will be appreciated.

Thank you

Domnick

Domnick
  • 509
  • 8
  • 25

1 Answers1

1

It appears you want to sum, grouped by each permutation in your dataset. Although hacky, I think this works for your problem. First we create a function to perform that summation using tidyeval syntax. Link for more information: Group by multiple columns in dplyr, using string vector input

sum_f <- function(col, df) {
    library(tidyverse)
    df <- df %>% 
          group_by_at(col) %>% 
          summarise(Val = sum(Val)) %>% 
          ungroup()
    df[,2]
}

We then apply it to your dataset using lapply, and binding together the summations.

bind_cols(lapply(c('Perm1', 'Perm2', 'Perm3'), sum_f, df))

This gets us the above answer. Caveats: Need to know the name of the columns you have to sum over for this to work. Also, each column needs to have the same levels of your permutations i.e. blue, green, yellow. The code will respect this ordering.

jacobsg
  • 121
  • 6