1

Let me start out by saying I am a complete beginner. So please bear with me if I lack the most basic terminology

Here's what I am trying to do:

test <- read.table(text = "Tube Window Session Value
                            A 1 1 13
                            A 1 2 14
                            B 1 1 17
                            B 1 2 14", header = TRUE)

to

result <- read.table(text = "Window Session Tube.A.Value Tube.B.Value
                            1 1 13 17
                            1 2 14 14", header = TRUE)

I searched for a bit and found the tidyr package. I am fairly certain that its spread() function is what I am looking for, but I can't get it to do what I need.

B.P.
  • 11
  • 4
  • 1
    In English the word you're looking for 'reshaping', in 'old' R language you're looking for 'casting', and in 'new' R language (tidyverse) you're looking for 'spread'. The similar question (linked at the top) demonstrates various ways of doing this. – SymbolixAU Feb 08 '18 at 21:51
  • Thank you. I already found that answer. But it's not exactly what I am looking for, I think. It doesn't explain how you merge the A, B and Value columns in my example into 2 new columns: Tube.A.Value and Tube.B.Value. Am I missing something? – B.P. Feb 08 '18 at 22:03
  • using the `data.table` method (because I know this method better than the others, and I wrote the answer on that linked question), you can do `library(data.table); setDT(test); dcast(test, formula = Window + Session ~ Tube)` – SymbolixAU Feb 08 '18 at 22:27

0 Answers0