简体   繁体   English

如何在 R 中将行转换为列,将列转换为行

[英]how to convert rows into column and columns into rows in R

how can I get output like this.我怎样才能得到这样的 output 。

my example input is我的示例输入是

X  Y 
2  3
4  5
7  9
0  1

The required output is所需的 output 是


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

Assuming your data is stroed in df then pivot_longer is exactly what you are after假设您的数据在df中存储,那么pivot_longer正是您所追求的

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

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

gather(df,'name ','value')收集(df,'名称','值')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM