stack data (from rows to column)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

stack data (from rows to column)

Kristi Glover
I was just wondering how to stack values of columns to rows. Would you give me some idea how I can stack? I just want to stack columns 2 and 3 but not column1. The column 1 is supposed to be repeated. I was able to stack using "stack" but I could not repeat the column 1.  

For example I have data like this
group t1 t2 t3
101 -31.77083 -33.79167
103 -27.26667 -27.9

data<-structure(list(group = c(101L, 103L), t1 = c(-31.77083, -27.26667
), t2 = c(-33.79167, -27.9)), .Names = c("group", "t1", "t2"), class = "data.frame", row.names = c(NA,
-2L))

-----
Want to make like this"
group     values     ind
101     -31.7708     t1
103     -27.2667     t1
101     -33.7917     t2
103     -27.9     t2

 final<-structure(list(group = c(101L, 103L, 101L, 103L), values = c(-31.77083,
-27.26667, -33.79167, -27.9), ind = structure(c(1L, 1L, 2L, 2L
), .Label = c("t1", "t2"), class = "factor")), .Names = c("group",
"values", "ind"), class = "data.frame", row.names = c(NA, -4L
))

I really appreciate for your help.
thanks
           
     
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: stack data (from rows to column)

arun kirshna


HI,
Try:
 library(reshape2)
 melt(data,measure.vars=c("t1","t2"),var="ind")

A.K.


On Sunday, November 24, 2013 9:31 PM, Kristi Glover <[hidden email]> wrote:
I was just wondering how to stack values of columns to rows. Would you give me some idea how I can stack? I just want to stack columns 2 and 3 but not column1. The column 1 is supposed to be repeated. I was able to stack using "stack" but I could not repeat the column 1. 

For example I have data like this
group t1 t2 t3
101 -31.77083 -33.79167
103 -27.26667 -27.9

data<-structure(list(group = c(101L, 103L), t1 = c(-31.77083, -27.26667
), t2 = c(-33.79167, -27.9)), .Names = c("group", "t1", "t2"), class = "data.frame", row.names = c(NA,
-2L))

-----
Want to make like this"
group     values     ind
101     -31.7708     t1
103     -27.2667     t1
101     -33.7917     t2
103     -27.9     t2

final<-structure(list(group = c(101L, 103L, 101L, 103L), values = c(-31.77083,
-27.26667, -33.79167, -27.9), ind = structure(c(1L, 1L, 2L, 2L
), .Label = c("t1", "t2"), class = "factor")), .Names = c("group",
"values", "ind"), class = "data.frame", row.names = c(NA, -4L
))

I really appreciate for your help.
thanks
           
                         
    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.