-1

I have a data set in a wide format, consisting of two rows, one with the variable names and one with the corresponding values. The variables represent characteristics of individuals from a sample of size 1000. For instance I have 1000 variables regarding the size of each individual, then 1000 variables with the height, then 1000 variables with the weight etc. Now I would like to run simple regressions (say weight on calorie consumption), the only way I can think of doing this is to declare a vector that contains the 1000 observations of each variable, say for instance:

regressor1=c(mydata$height0, mydata$height1, mydata$height2, mydata$height3, ... mydata$height1000)

But given that I have a few dozen variables and each containing 1000 observations this will become cumbersome. Is there a way to do this with a loop?

I have also thought a about the reshape options of R, but this again will put me in a position where I have to type 1000 variables a few dozen times.

Thank you for your help.

Niccola Tartaglia
  • 1,537
  • 2
  • 26
  • 40
  • I feel like `t()` would do this, what have you tried? – Badger Mar 23 '16 at 19:15
  • reshape could probably do what you're looking for: [help page](http://www.statmethods.net/management/reshape.html) – desc Mar 23 '16 at 19:20
  • The problem with t() is that I then get all my variables stacked on top of each other. Then I can still not run the regressions. I would like each of the 1000 observations to be grouped into one column (or one vector). I wonder if there is a way (maybe via a loop) to group each 1000 into one vector or column. – Niccola Tartaglia Mar 23 '16 at 19:26
  • My problem with reshape is, that I would still have to type in all of the variables names. – Niccola Tartaglia Mar 23 '16 at 19:29
  • So you're looking for `mydata[1,]`? Then you'll simply have a vector of the first row, and `mydata[2,]` for the second row. Not really the same as transposing the data so much as it is data navigation. – Badger Mar 23 '16 at 19:32
  • Would it be easier if I just declared a vector for each variable, along the lines of: for(i in 1:1000) { name <- paste("height", i, sep = "") regressor1(name) } where name should be the value of height_i – Niccola Tartaglia Mar 23 '16 at 19:33
  • Yes, but how can I write a loop that would run through all 1000 variables. For instance I would like to have a vector that contains all 1000 values for the first characteristic. Say a vector called weight that is filled with (weight1, weight2, weight3..., weight1000). – Niccola Tartaglia Mar 23 '16 at 19:39
  • couldn't I instead just loop through the entire data set and run a few dozen loops with each taking the 1st to 1000st value of the data set and putting that into a vector? – Niccola Tartaglia Mar 23 '16 at 19:46
  • Ok, now I understood your suggestion Badger, many thanks it worked now!!! Thanks guys. – Niccola Tartaglia Mar 23 '16 at 19:59
  • Great article about looping in R: http://www.r-bloggers.com/for-loops-and-how-to-avoid-them/ Generally speaking looping in R is not the most efficient way to a final product. Sometimes they are required but often there are ways around it! – Badger Mar 23 '16 at 20:20
  • Your question is very unclear. Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). **Note**: there is no need for a data structure with a row of names and a row of values. Use a named vector. – alexwhitworth Mar 23 '16 at 21:14

1 Answers1

0

Here is how I would go about your issue. t() will transpose the data for you from many columns to many rows.

Note: t() can be used with a matrix rather than a data frame, I simply coerced to data frame to show my example will work with your data.

# Many columns, 2 rows
x <- as.data.frame(matrix(nrow=2,ncol=1000,seq(1:2000)))

#2 Columns, many rows
t(x)

Based on your comments you are looking to generate vectors.

If you have transposed:

regressor1 <- x[,1]

regressor2 <- x[,2]

If you have not transposed:

regressor1 <- x[1,]

regressor2 <- x[2,]
Badger
  • 1,043
  • 10
  • 25