I needed help to convert below R dataframe. Can anyone can help me to do it? I tried
df:
YRMONTH | ABC | BCD | CDA | ING
---------------------------------
Jan-2021 | 123 | 123 | 123 | 123
FEB-2021 | 124 | 124 | 0 | 124
MAR-2021 | 125 | 125 | 125 | 125
APR-2021 | 126 | 126 | 126 | 0
Output:
YRMONTH | Jan-2021 | FEB-2021 | MAR-2021 | APR-2021 ...
---------------------------------------------------
ABC | 123 | 124 | 125 | 126
BCD | 123 | 124 | 125 | 126
CDA | 123 | 0 | 125 | 126
ING | 123 | 124 | 125 | 0
Thank you in advance for your help.
We could use data.table::transpose
data.table::transpose(df, make.names = 'YRMONTH', keep.names = 'YRMONTH')
-output
YRMONTH Jan-2021 FEB-2021 MAR-2021 APR-2021
1 ABC 123 124 125 126
2 BCD 123 124 125 126
3 CDA 123 0 125 126
4 ING 123 124 125 0
df <- structure(list(YRMONTH = c("Jan-2021", "FEB-2021", "MAR-2021",
"APR-2021"), ABC = 123:126, BCD = 123:126, CDA = c(123L, 0L,
125L, 126L), ING = c(123L, 124L, 125L, 0L)), class = "data.frame",
row.names = c(NA,
-4L))