Zandi
Zandi

Reputation: 21

Converting multiple row data to single row in R

I have same data set but with 886,120 rows (34 ID and each ID has 22,271 rows) to be convert to 34 rows with 22,271 columns.

Raw  ID(34)  Values(22,271 for each ID)

1   AND 0.08333
2   BEL 0.08333
3   ARR 0.2292
4   AND 0.3056
5   AND 0.3056
6   BEL 0.3333
7   AKT 0.3421
8   BEL 0.3667
9   AKTK 0.3684
10  ARR 0.4583
11  ARR 0.4583
12  AKTK 0.7105
...
886120  ARR 0.2152

===========================================
To be convert with the following format:
===========================================
ID     Value1   Value2   Value3   ... Value22,721

AKTK    0.7105  0.3421  0.3684  
AND 0.3056  0.3056  0.08333 
ARR 0.4583  0.4583  0.2292  
BEL 0.3333  0.3667  0.08333 

Can anybody help me ?

Upvotes: 2

Views: 618

Answers (1)

atiretoo
atiretoo

Reputation: 1902

If your dataframe was sorted by ID, this would work. You add a column describing the groups of values that should go together in a column of the result, and then this works:

df <- data.frame(ID=rep(LETTERS,100),
                valueID=rep(1:100,each=26),
                values=rnorm(2600))
library(reshape2)
df2 <- dcast(df,ID~valueID)

So perhaps you could do

oo <- order(your.df$ID)
df <- cbind(your.df[oo,],valueID=paste0('value',rep(1:22721,each=34)))

and then do the what I did above. @akrun's data.table solution also works because N and ID together specify a unique cell, so mean doesn't contribute anything. But in both cases you need to sort first.

Upvotes: 1

Related Questions