0

I have a data like this:

  +---+----+----+----+------+
  | Su| Re | BM | CH | Eyes |
  +---+----+----+----+------+
  | 1 |  1 |  . |  0 |    0 |
  | 0 |  1 |  1 |  0 |    1 |
  | 1 |  1 |  2 |  . |    0 |
  | 0 |  1 |  3 |  1 |    1 |  
  | 1 |  2 |  . |  0 |    0 |
  | 0 |  2 |  2 |  0 |    1 |
  | 1 |  2 |  3 |  . |    1 |
  | 0 |  2 |  4 |  1 |    1 |
  +---+----+----+----+------+

I am trying to create multiple vectors based on this criteria Su=1 and Re =1,2,3...., Su=1 and Re=1 first data vector, Su=1 and Re=3 second vector so on....I am using a loop for iterating through Re and subsetting dataset based on Su and Re. This is my code below, its not working, i need help to find out where I am going wrong.

library(foreign)                            
a <- read.dta(".....Data.dta")

for (i in 1:10)               
{          
  b[i,]=subset(a, su==1 & re==i)
}     
b
bison2178
  • 747
  • 1
  • 8
  • 22
  • Why is this tagged as c++? – GWW Feb 25 '14 at 00:31
  • Smells like a database, use a database for this. – Thomas Matthews Feb 25 '14 at 00:37
  • (1) In your example data, you have `Su` and `Re`, and in your code you have `su` and `re`. (2) When you say it's not working, what exactly is happening? Do you receive an error message? (3) See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for info on how to ask a good reproducible question. – jbaums Feb 25 '14 at 00:37

2 Answers2

1

You want to create a stucture (or class) representing a row.

Create a vector of these structures.

Now create index tables (std::map) with your sort criteria as the first parameter (key) and a pointer or index of the row in the vector (value).

This is the best method if you intend to access the same records in different orderings. You can access the index table in a given order without changing to order of the data in the vector.

This is how databases work with tables and indices.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

If I understand your question correctly, you can modify your code like this:

library(foreign)                            
a <- read.dta(".....Data.dta")

b <- list(rep(NA, 10))

for (i in 1:10)               
{          
  b[[i]] <- subset(a, su==1 & re==i)
}     

This creates a list whose ith entry is the rows of a matching re==i. Is that what you're after?