Perl one-liner to calculate an average of some value in a bunch of files

A quick and dirty one-liner (depending on the length of your lines ;)) to calculate the average of a value in a bunch of files in a directory structure.

The below one line picks out a value in each file that matches the name “Logfile*.txt” in the underlying directory structure.

In the below case, the line was in the form of:

Correctly Classified Instances 37 60.6557 %

or

Correctly Classified Instances 37 60 %

The code traverses the directory structure from the current dir and picks out the “60.6557” and sums that over the number of files that matched and then divides with however many files that matched.


find . -name "Logfile*.txt" -exec perl -ne '($var) = (/^Correctly.*\s+((\.|\d)+)\s+%/); print "$var\n" if $var;' '{}' \; | xargs perl -e 'use List::Util qw(sum); print(sum(@ARGV)/scalar(@ARGV)); print "\n";'

OBS: Not very robust!! But it IS a one-liner! 😉

Tags:

Index a DataFrame subset on string column name in Julia


julia> using RDatasets

julia> iris = dataset("datasets", "iris")

julia> iris[iris[:Species] .== "setosa", :]
50x5 DataFrame
|-------|-------------|------------|-------------|------------|----------|
| Row # | SepalLength | SepalWidth | PetalLength | PetalWidth | Species |
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | "setosa" |
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | "setosa" |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | "setosa" |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | "setosa" |
| 5 | 5.0 | 3.6 | 1.4 | 0.2 | "setosa" |
| 6 | 5.4 | 3.9 | 1.7 | 0.4 | "setosa" |
| 7 | 4.6 | 3.4 | 1.4 | 0.3 | "setosa" |
| 8 | 5.0 | 3.4 | 1.5 | 0.2 | "setosa" |
| 9 | 4.4 | 2.9 | 1.4 | 0.2 | "setosa" |

Tags: , ,

Nice post on Julia meta operations

Great post about basic Julia stuff @ Julia Helps

Tags:

Sample with replacement from DataFrame in Julia

julia> df = DataFrame(A = 1:10, B = 2:2:20)

10x2 DataFrame
|-------|----|----|
| Row # | A | B |
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 3 | 6 |
| 4 | 4 | 8 |
| 5 | 5 | 10 |
| 6 | 6 | 12 |
| 7 | 7 | 14 |
| 8 | 8 | 16 |
| 9 | 9 | 18 |
| 10 | 10 | 20 |

julia> df[sample([1:size(df,1)],20),:]

20x2 DataFrame
|-------|----|----|
| Row # | A | B |
| 1 | 2 | 4 |
| 2 | 3 | 6 |
| 3 | 1 | 2 |
| 4 | 4 | 8 |
| 5 | 3 | 6 |
| 6 | 5 | 10 |
| 7 | 8 | 16 |
| 8 | 5 | 10 |
| 9 | 4 | 8 |
| 10 | 8 | 16 |
| 11 | 9 | 18 |
| 12 | 10 | 20 |
| 13 | 9 | 18 |
| 14 | 4 | 8 |
| 15 | 1 | 2 |
| 16 | 1 | 2 |
| 17 | 3 | 6 |
| 18 | 4 | 8 |
| 19 | 10 | 20 |
| 20 | 7 | 14 |

Tags:

Convert a matrix (Array) of Any to a matrix (Array) of floats in Julia


convert(Array{Float64},array_of_Anys)

Tags:

Sample with replacement from Matrix in Julia

julia> sample([1 2 3;4 5 6;7 8 9], 10)
10-element Array{Int64,1}:
1
3
7
3
8
6
3
7
5
4

Tags:

Sample with replacement from a vector in Julia

julia> sample([1 2 3], 10)
10-element Array{Int64,1}:
2
3
2
3
1
1
2
1
1
1

Tags:

Print array in Julia with four number of decimals

The general to limit the number of decimals is:

println((round(Array,number of decimals))

so to get four decimals:

println((round(Array,4))

Tags:

Adding missing Keyboard Shortcuts to Mac OS X and OS X applications

There is a really cool feature in Mac OS X that enables you to add missing Keyboard shortcuts in OS X applications. For instance, I was missing a shortcut for Clearing the working environment in RStudio which removes the variables in the current workspace. To add this I added a keyboard shortcut for this in the App Shortcuts section in the System Preferences -> Keyboard -> Shortcuts section

Screen Shot 2014-01-24 at 19.31.14

If you want add shortcuts for sub-menu items you do this by using a “->” between the menu item names (see the File->New File->Text File example in the above screenshot).

I also very frequently select text and want to search for that on Google in a new Safari window, so I added a new shortcut for this in the Services section. The preferences for this look like this:

Screen Shot 2014-01-24 at 19.40.26

Tags:

Mac OS X X11 continously keeps craching

If some program requests to start X11 and X11 does not work you can end up in the situation that X11 keeps crashing even though you press “Quit”. This is due to X11 (and friends) are launched from “lunchctl”. To stop this use:


launchctl list

This gives you a set of jobs under “launchctl’s” command. You want to remove the “org.x.startx” job. This is done with:


launchctl remove org.x.startx

X11 should now stop keep crashing… 🙂