I'm processing a data.frame of products called "`all`" whose first variable `all$V1` is a product family. There are several rows per product family, i.e. `length(levels(all$V1))` < `length(all$V1)`.

I want to traverse the data.frame and process by product family "`p`". I'm new to R, so I haven't fully grasped when I can do something vectorially, or when to loop. At the moment, I can traverse and get subsets by:

```
for (i in levels (all$V1)){
  p = all[which(all[,'V1'] == i), ];
  calculateStuff(p);
}

```

Is this the way to do this, or is there a groovy vectorial way of doing this with `apply` or something? There are only a few thousand rows, so performance gain is probably negligeable, but I'd like to devlop good habits for larger data ses.
['https://Stackoverflow.com/questions/26163209', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3790954/']
Data:

```
all = data.frame(V1=c("a","b","c","d","d","c","c","b","a","a","a","d"))

```

'all' can be split by V1: 

```
> ll = split(all, all$V1)
> ll
$a
   V1
1   a
9   a
10  a
11  a

$b
  V1
2  b
8  b

$c
  V1
3  c
6  c
7  c

$d
   V1
4   d
5   d
12  d

```

sapply can be used to analyze each component of list 'll'. Following finds number of rows in each component (which represents product family): 

```
calculateStuff <- function(p){
  nrow(p)
}

> sapply(ll, calculateStuff)
a b c d 
4 2 3 3 

```
There is unlikely to be much of a performance gain with the below, but it is at least more compact and returns the results of `calculateStuff` as a convenient list:

```
lapply(levels(all$V1), function(i) calculateStuff(all[all$V1 == i, ]) )

```

As @SimonG points out in his comment, depending on exactly what your `calculateStuff` function is, `aggregate` may also be useful to you if you want your results in the form of dataframe.