0

I want to reshape my data into a long format, but I would like to repeat the entire range of id's for each variable in my data set, even for those id entries on which the variable takes no value. At the moment I can get narrow data, with ids for each variable on which there is a corresponding entry

Suppose my data has 15 variables, with 20 possible id's, I want to create a narrow form of this data that is 15*20 in length (the range of ids, repeated for each variable), whereby each repeated range of id's shows the values taken by variable, for id1, id2, id3 e.t.c until the end of the range of id's is reached, then variable2 is displayed for id1, id2, id3 e.t.c..

I am unsure of ohw to do this in R, I am currently using the reshape package.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
user124123
  • 1,642
  • 7
  • 30
  • 50
  • 7
    It will be easier to help you if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your problem. – Kara Woo Jul 16 '14 at 16:06

2 Answers2

1

You can use the replicate function which is explained here

v1 <- 1:5
v2 <- 1:6
rep(v1, each = 6)
# 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5
rep(v2, 5)
#1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
germcd
  • 954
  • 1
  • 12
  • 24
1

Yeah, this is hard to work with, but you're looking for the melt function I think...

library(reshape2)
melt(yourdata, id.vars = 'ID COLUMN')

This will return a 300 x 3 data set that looks like:

ID COLUMN   variable    value
        1       col2        7
        1       col3        8
     ....       ....     ....
       20      col14       99
       20      col15      100
maloneypatr
  • 3,562
  • 4
  • 23
  • 33