I have a large data frame that I am attempting to run a for loop, but need to get all the different observations first. The process that I am currently trying to approach is this:
- Create a list/vector/data frame of the observation
- Use each observation in a
for loop
Here is how I tackled it:
Generate some data:
x <- gl(12,5)
The result I am looking for is a list of all the different observations:
1 2 3 4 5 6 7 8 9 10 11 12
I could create a data frame, then drop the number columns like this:
list <- data.frame(table(x))
list[,2] <- NULL
print(list)
From this list/vector/dataframe, I would run through the for loop like so:
for (i in list) print(i)
There must be a more efficient way to get the list for the for loop. Can someone suggest one?
Several options include:
The
levels()ones are most logical and appropriate if you want the groupings.Now whether you really want this is another question. See
?splitwhich can split up a data frame on the basis of a factor (which you canlapply()over), or?aggregateamong others. There is also the plyr package which seems very popular for this sort of thing.