0

code screenshot

I know for a fact that I have done this before (I'm new to R) but I cannot remember how to do it and nothing I search seems to be talking about the same thing. I created this data frame and I want to instead have a data frame with two variables; one being values, the other being their treatment group (originally the column they were in). For instance, an entry would be 118.8 DC. How do I do this?

Dan
  • 11,370
  • 4
  • 43
  • 68
Adam
  • 11
  • 3
  • have a look at the `pivot_wider` function from `tidyr`. – Paul van Oppen Jun 13 '20 at 00:30
  • Hi Adam. Not understanding exactly what you are trying to do with 118.8 DC. Make it into two variables? – Gray Jun 13 '20 at 00:31
  • Please add data using `dput` and not as images. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jun 13 '20 at 00:45

1 Answers1

0

Using pivot_longer() I created a two-variable data frame with treatment group and values assigned. I assume by your question this is what you were alluding to? My numbers could be off being that, without a reproducible example, I had to type them out from the image you posted.

Code:

data138 <- data.frame(DC = c(118.8, 122.6, 115.6, 113.6, 119.5, 115.9, 115.8, 115.1, 116.9, 115.4, 115.6, 107.9),
                  DCMK = c(105.4, 101.1, 102.7, 97.1, 101.9, 98.9, 100.0, 99.8, 102.6, 100.9, 104.5, 93.5),
                  MC = c(102.1, 105.8, 99.6, 102.7, 98.8, 100.9, 102.8, 98.7, 94.7, 97.8, 99.7, 98.6))

data138 %>% 
  pivot_longer(c(DC, DCMK, MC), names_to = "Treatment Group", values_to = "Values")

Output:

Treatment Group          Values
DC                       118.8          
DCMK                     105.4          
MC                       102.1          
DC                       122.6          
DCMK                     101.1          
MC                       105.8          
DC                       115.6          
DCMK                     102.7          
MC                       99.6           
DC                       113.6  
Eric
  • 2,699
  • 5
  • 17