R concentrating data frame

Posted by user1631503 on Stack Overflow See other posts from Stack Overflow or by user1631503
Published on 2012-08-29T02:40:26Z Indexed on 2012/08/29 3:38 UTC
Read the original article Hit count: 127

Filed under:
|
|
|
|

I have a data frame like this

>X_com
  Day_1 Day_2 Day_3 Day_4 Day_5 Day_6 Day_7 Day_8 Day_9 Day_10
1     0     0     0     0     0     0     0     0     0      1
2     0     0     0     0     0     0     0     0     0      0
3     0     0     0     0     0     0     0     0     0      0
4     0     0     0     0     0     0     0     0     0      0
5     0     0     0     0     0     0     0     0     0      0
6     0     0     0     0     0     0     0     0     0      0
7     0     0     0     0     0     0     0     0     0      0
8     0     0     0     0     0     0     0     0     0      0

I need to concentrate all the values into one column and add another column with "1;" So I did this

> X_new=matrix(1,8,2)
> X_new[1,]=paste(X_com[1,1], X_com[1,2],X_com[1,3],X_com [1,4],X_com[1,5],X_com[1,6],X_com[1,7],X_com[1,8],X_com [1,9],X_com[1,10], sep="")
> X_new[2,]=paste(X_com[2,1], X_com[2,2],X_com[2,3],X_com [2,4],X_com[2,5],X_com[2,6],X_com[2,7],X_com[2,8],X_com [2,9],X_com[2,10], sep="")
> X_new[3,]=paste(X_com[3,1], X_com[3,2],X_com[3,3],X_com [3,4],X_com[3,5],X_com[3,6],X_com[3,7],X_com[3,8],X_com [3,9],X_com[3,10], sep="")
> X_new[4,]=paste(X_com[4,1], X_com[4,2],X_com[4,3],X_com [4,4],X_com[4,5],X_com[4,6],X_com[4,7],X_com[4,8],X_com [4,9],X_com[4,10], sep="")
> X_new[5,]=paste(X_com[5,1], X_com[5,2],X_com[5,3],X_com [5,4],X_com[5,5],X_com[5,6],X_com[5,7],X_com[5,8],X_com [5,9],X_com[5,10], sep="")
> X_new[6,]=paste(X_com[6,1], X_com[6,2],X_com[6,3],X_com [6,4],X_com[6,5],X_com[6,6],X_com[6,7],X_com[6,8],X_com [6,9],X_com[6,10], sep="")
> X_new[7,]=paste(X_com[7,1], X_com[7,2],X_com[7,3],X_com [7,4],X_com[7,5],X_com[7,6],X_com[7,7],X_com[7,8],X_com [7,9],X_com[7,10], sep="")
> X_new[8,]=paste(X_com[8,1], X_com[8,2],X_com[8,3],X_com [8,4],X_com[8,5],X_com[8,6],X_com[8,7],X_com[8,8],X_com [8,9],X_com[8,10], sep="")
> X_new[1:8,2]="1;"
> as.data.frame(X_new)
          V1 V2
1 0000000001 1;
2 0000000000 1;
3 0000000000 1;
4 0000000000 1;
5 0000000000 1;
6 0000000000 1;
7 0000000000 1;
8 0000000000 1;

I believe there's definitely a faster way of achieving this but have no clue.

The other problem is, I have over a thousand of data frame like this needs to be concentrated. I'm still learning how to loop these repetitive steps but is progressing quite slowly. If the original data frames were named uniquely, does that mean I have no choice but to work on each of them individually?

Thank you in advance.

© Stack Overflow or respective owner

Related posts about r

    Related posts about data