Home > Blockchain >  how to convert rows into column and columns into rows in R
how to convert rows into column and columns into rows in R

Time:07-26

how can I get output like this.

my example input is

X  Y 
2  3
4  5
7  9
0  1

The required output is


X 2
Y 3
X 4
Y 5
X 7
Y 9
X 0
Y 1 

CodePudding user response:

Assuming your data is stroed in df then pivot_longer is exactly what you are after

library(tidyverse)    
df %>% pivot_longer(cols=everything()) 

CodePudding user response:

Or you could just use gather, but lose the original paired order:

df<-data.frame(x=c(2,4,7,0),y=c(3,5,9,1))

gather(df,'name ','value')

  • Related