Question: Assume I have a dataframe:

```
Col1   Col2   Col3   Val
  a      1      a     2
  a      1      a     3
  a      1      b     5
  b      1      a     10
  b      1      a     20
  b      1      a     25

```

I want to aggregate across all columns and then attach this to my dataframe so that I have this output:

```
Col1   Col2   Col3   Val    Tot
  a      1      a     2      10
  a      1      a     3      10
  a      1      b     5      10
  b      1      a     10     55
  b      1      a     20     55
  b      1      a     25     55

```

Is there something I can do with `tapply` to accomplish this? I'm a little stuck on the most efficient way.

Answer: 
No guarantees in terms of performance, but for each directory you could check its files to see if it contains any with the matching extension:

```
List<string> imageDirectories = Directory.GetDirectories(sourceTextBox.Text, "*", SearchOption.AllDirectories)
    .Where(d => Directory.EnumerateFiles(d)
        .Select(Path.GetExtension)
        .Where(ext => ext == ".png" || ext == ".jpg")
        .Any())
    .ToList();

```
You can use [`Directory.EnumerateFiles`](http://msdn.microsoft.com/en-us/library/system.io.directory.enumeratefiles(v=vs.110).aspx) method to get the file matching criteria and then you can get their Path minus file name using `Path.GetDirectoryName` and add it to the `HashSet`. `HashSet` would only keep the unique entries. 

```
HashSet<string> directories = new HashSet<string>();
foreach(var file in Directory.EnumerateFiles(sourceTextBox.Text, 
                                             "*.png",  
                                             SearchOption.AllDirectories))
{
    directories.Add(Path.GetDirectoryName(file));
}

```

For checking multiple file extensions you have to enumerate all files and then check for extension for each file like:

```
HashSet<string> directories = new HashSet<string>();
string[] allowableExtension = new [] {"jpg", "png"};
foreach(var file in Directory.EnumerateFiles(sourceTextBox.Text,
                                             "*", 
                                             SearchOption.AllDirectories))
{
    string extension = Path.GetExtension(file);
    if (allowableExtension.Contains(extension))
    {
        directories.Add(Path.GetDirectoryName(file));
    }
}

```