coding_heart
coding_heart

Reputation: 1295

reshape data into panel with multiple variables and no time variable in R

I'm new to reshaping data in R and can't figure out how to use reshape() (or another package) to create a panel data. There are two time observations for each geographical unit, however each of the time observations is formatted in a variable. For example:

subdistrict <- 1:4
control_t1 <- 5:8
control_t2 <- 9:12
motivation_t1 <- 12:15
motivation_t2 <- 16:19

data_mat <- as.data.frame(cbind(subdistrict, control_t1, control_t2, motivation_t1, motivation_t2))

data_mat
  subdistrict control_t1 control_t2 motivation_t1 motivation_t2
1           1          5          9            12            16
2           2          6         10            13            17
3           3          7         11            14            18
4           4          8         12            15            19

Here, control_t1 and control_t2 each refer to a different period. My goal is to reshape the data such that a time variable can be established and the named variable can be collapsed so to produce the following frame:

  subdistrict time control motivation
1           1            1         12            
1           2            5         16
2           1            2         13            
2           2            6         17
3           1            3         14            
3           2            7         18
4           1            4         15            
4           2            8         19

I'm not sure how to create the new time variable, and collapse and rename the variables to reshape the data as such. Thanks for any help.

Upvotes: 0

Views: 202

Answers (2)

Dinre
Dinre

Reputation: 4216

A simple answer is to split and rebind the data frame into your new form, like so:

new_Data <- data.frame(
    subdistrict=data_mat[,1],
    control=unlist(data_mat[,2:3]),
    motivation=unlist(data_mat[,4:5]))

All we are doing here is collapsing the two columns of 'control' and 'motivation' into single columns of data by using the 'unlist' function and then binding it all into a new data frame. The 'subdistrict' data repeats, so there is no reason to specify it twice.

Upvotes: 1

PAC
PAC

Reputation: 5366

You just have to use the reshape() function with option direction = "long". Here is the code :

district <- 1:4
control_t1 <- 5:8
control_t2 <- 9:12
relax_t1 <- 12:15
relax_t2 <- 16:19
data_mat <- as.data.frame(cbind(district, control_t1, control_t2, relax_t1, relax_t2))
reshape(data = data_mat, direction = "long", idvar = "district", timevar = "time", varying = list(c(2:3), c(4:5)))
#     district time control_t1 relax_t1
# 1.1        1    1          5       12
# 2.1        2    1          6       13
# 3.1        3    1          7       14
# 4.1        4    1          8       15
# 1.2        1    2          9       16
# 2.2        2    2         10       17
# 3.2        3    2         11       18
# 4.2        4    2         12       19

Have a look at the R Programming wikibooks to learn more.

Upvotes: 2

Related Questions