It is often said that 80% of data analysis is spent on the cleaning and preparing data.
In this aspect I sometimes spent quite some time to shape my data and finally get a data.frame
as shown in reproducible example.
I want to organize rows as columns in my data and order them one of the unique value .
you can assume No
is a key column, that will determine the number of columns in the transposed data frame
Here is a reproducible example;
set.seed(1)
No <- rep(seq(0,95,1),times=3)
AC <- rep(rep(c(78,110),each=1),times=length(No)/2)
AR <- rep(rep(c(256,320,384),each=2),times=length(No)/6)
AM <- rep(1,times=length(No))
DQ <- rep(rep(seq(0,15,1),each=6),times=3)
V <- rep(seq(100,300,100),each=96)
R <- rep(sample(5000:6000,96),times=3)
df <- data.frame(No,AC,AR,AM,DQ,V,R)
head(df)
No AC AR AM DQ V R
1 0 78 256 1 0 100 5455
2 1 110 256 1 0 100 5410
3 2 78 320 1 0 100 5810
4 3 110 320 1 0 100 5603
5 4 78 384 1 0 100 5652
6 5 110 384 1 0 100 5351
The output which I`m looking for;
# No 0 1 2 3 4 5 6 7 8 9 10 11 * * * 95
# AC 78 110 78 110 78 110 78 110 78 110 78 110 * * * 110
# AR 256 256 320 320 384 384 256 256 320 320 384 384 * * * 384
# AM 1 1 1 1 1 1 1 1 1 1 1 1 * * * 1
# DQ 0 0 0 0 0 0 1 1 1 1 1 1 * * * 15
# V
# 100 5265 5372 5572 5906 5201 5894 5939 5656 5624 5061 5204 5174 * * * 5722
# 200 5256 5720 5162 5481 5977 5711 5600 5752 5042 5885 5759 5467 * * * 5722
# 300 5265 5372 5572 5906 5201 5894 5939 5656 5624 5061 5204 5174 * * * 5722