-1

I am trying to make a box plot out of a dataframe with 1000 rows and 80 columns. It currently looks like this. enter image description here

Now I want to move each column to stack 80 different columns but not sure how to do that. How can I separate the columns and stack to make a wide format dataframe to a long format data?

Current dataframe looks like this:

age1 age 2 age3 
x1   y1    z1 
x2   y2    z2 
x3   y3    z3 

Desired outcome is:

age value
 1   x1
 1   x2
 1   x3
 1   x1
 2   y1
 2   y2
 2   y3
 3   z1
 3   z2
 3   z3
  • Try with `stack()`. Can't help you more given you do not provide a code example nor a detail of your expected output. – Ric Jan 26 '23 at 02:18
  • 1
    See [Reshaping data.frame from wide to long format](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) – zephryl Jan 26 '23 at 02:51

1 Answers1

0

Suppose you have a dataframe like this:

dat <- data.frame(
  age1 = c(1, 2, 3),
  age2 = c(4, 5, 6),
  age3 = c(7, 8, 9)
)

You can use 'pivot_longer' to get your desired outcome:

library(tidyverse)
dat <- dat %>% 
  pivot_longer(cols = starts_with("age"),
               names_to = "age",
               names_prefix = "age",
               values_to = "value") %>% 
  arrange(age)
Leonardo19
  • 83
  • 5