Reputation: 789
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
Upvotes: 0
Views: 112
Reputation: 18
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?
Upvotes: 0
Reputation: 57708
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.
Upvotes: 1