Yorgos
Yorgos

Reputation: 30445

Restructure a data frame in R

This is the initial data.frame

temp <- structure(list(Initial = c(
  32.5, 30.4, 36.5, 4.2, 24.3
), Amount = c(
  374.24, 79.05, 1.02, 0.79, 0.71
), Load  = c(
  11.512, 2.605, 0.027, 0.021, 0.019
), Extra = c(
  36.9, 32.5, 12.2, 12.2, 12.2
), Perc = c(
  114L, 107L, 33L, 33L, 33L
)), row.names = c(
  1L, 2L, 3L, 4L, 5L
), class = "data.frame")

That is what I want to create. I would prefer a tidyverse way.

data <- structure(list(Rs = c(
  "Initial", "Initial", "Initial", "Initial",
  "Initial", "Initial", "Initial", "Initial", "Initial", "Initial",
  "Initial", "Initial", "Initial", "Initial", "Initial", "Initial",
  "Initial", "Initial", "Initial", "Initial", "Amount", "Amount",
  "Amount", "Amount", "Amount", "Amount", "Amount", "Amount", "Amount",
  "Amount", "Amount", "Amount", "Amount", "Amount", "Amount", "Load",
  "Load", "Load", "Load", "Load", "Load", "Load", "Load", "Load",
  "Load", "Extra", "Extra", "Extra", "Extra", "Extra"
), Rvalue = c(
  32.5,
  30.4, 36.5, 4.2, 24.3, 32.5, 30.4, 36.5, 4.2, 24.3, 32.5, 30.4,
  36.5, 4.2, 24.3, 32.5, 30.4, 36.5, 4.2, 24.3, 374.24, 79.05,
  1.02, 0.79, 0.71, 374.24, 79.05, 1.02, 0.79, 0.71, 374.24, 79.05,
  1.02, 0.79, 0.71, 11.512, 2.605, 0.027, 0.021, 0.019, 11.512,
  2.605, 0.027, 0.021, 0.019, 36.9, 32.5, 12.2, 12.2, 12.2
), Cs = c(
  "Amount",
  "Amount", "Amount", "Amount", "Amount", "Load", "Load", "Load",
  "Load", "Load", "Extra", "Extra", "Extra", "Extra", "Extra",
  "Perc", "Perc", "Perc", "Perc", "Perc", "Load", "Load", "Load",
  "Load", "Load", "Extra", "Extra", "Extra", "Extra", "Extra",
  "Perc", "Perc", "Perc", "Perc", "Perc", "Extra", "Extra", "Extra",
  "Extra", "Extra", "Perc", "Perc", "Perc", "Perc", "Perc", "Perc",
  "Perc", "Perc", "Perc", "Perc"
), Cvalue = c(
  374.24, 79.05, 1.02,
  0.79, 0.71, 11.512, 2.605, 0.027, 0.021, 0.019, 36.9, 32.5, 12.2,
  12.2, 12.2, 114, 107, 33, 33, 33, 11.512, 2.605, 0.027, 0.021,
  0.019, 36.9, 32.5, 12.2, 12.2, 12.2, 114, 107, 33, 33, 33, 36.9,
  32.5, 12.2, 12.2, 12.2, 114, 107, 33, 33, 33, 114, 107, 33, 33,
  33
)), class = "data.frame", row.names = c(NA, -50L))

Upvotes: 1

Views: 66

Answers (2)

A. Suliman
A. Suliman

Reputation: 13125

We can loop using names(temp), select the required columns then gather, finally binds all dataframes together

library(tidyverse)
#head(names(temp),-1)
map_dfr(names(temp)[-length(temp)], ~select(temp,.x:ncol(temp)) %>% 
                      gather(key = Cs,value = Cvalue,-.x) %>% mutate(Rs=.x) %>% 
                      select(Rs,Rvalue=.x,everything())) 

Upvotes: 1

IceCreamToucan
IceCreamToucan

Reputation: 28675

Explanation of what pmap does:

pmap(list(x, y, z), fun) is the same as Map(fun, x, y, z). Then pmap_dfr does the same thing, but additionally rbinds all the elements of the resulting list together into one data frame.

library(tidyverse)

pairs <- expand.grid(names(temp), names(temp), stringsAsFactors = F) %>% 
            filter(Var1 > Var2)

pmap_dfr(pairs, ~{
  tibble(Rs = .y, Rvalue = temp[[.y]], 
         Cs = .x, Cvalue = temp[[.x]])
})

edit: Actually, it's not clear how you're deciding which Rs, Cs pairs you include and which you don't. Whatever the logic, starting with expand.grid and filtering should probably get you there.

Upvotes: 1

Related Questions