This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script class="jsbin" src="http://cdnjs.cloudflare.com/ajax/libs/raphael/1.5.2/raphael-min.js"></script> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
<!--[if IE]> | |
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
<style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Data analysis with R -- It's better and better now | |
================================================== | |
A nice R resource, the “cookbook for R”: http://wiki.stdout.org/rcookbook/ | |
Installing | |
---------- | |
```{r installing} | |
# For Linux: Install package directly from CRAN accessible for all users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pairs can be constructed easily using parentheses: | |
val pair: (Char, Int) = ('c', 1) | |
// In order to access the two elements of a pair, you can use the accessors `_1` and `_2`: | |
val theChar = pair._1 | |
val theInt = pair._2 | |
// Another way to deconstruct a pair is using pattern matching: | |
pair match { | |
case (theChar, theInt) => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function computes for each unique character in the list `chars` the number of | |
* times it occurs. times(List('a', 'b', 'a')) should return List(('a', 2), ('b', 1)) | |
*/ | |
def howManyTimes(chars: List[Char]): List[(Char, Int)] = { | |
def incr(acc:Map[Char, Int], c:Char) = { | |
val count = (acc get c).getOrElse(0) + 1 // If acc get c is none getOrElse makes it 0 | |
// To add values to the Map we use operator + | |
// If key does not exist, a new (key, value) is inserted, otherwise substitution occurs | |
acc + ((c, count)) // I Double brakets tell scala we're dealing with a pair |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* For each unique character in the list `chars`, it calculates the number of | |
* times it occurs. times(List('a', 'b', 'a')) should return List(('a', 2), ('b', 1)) | |
*/ | |
def howManyTimes(w: String): List[(Char, Int)] = { | |
val grouped = w.groupBy(x => x.toLower) // Group by element (all chars to lowercase) | |
grouped.map(x => (x._1, x._2.length)).toList // Create pair for each element | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Remove all whitespace from String | |
"This is a string".replaceAll("\\s+","") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A list of strings into a single string | |
val food = "rice" :: "cookies" :: "watermelon" :: Nil | |
food.foldLeft("")(_ + ", " + _) | |
// But we would have to remove the first two chars: | |
// ", rice, cookies, watermelon" | |
// Now with mkString | |
val fruits = "apple" :: "orange" :: "kiwi" :: Nil | |
fruits mkString ", " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* For each unique character in the list `chars`, it calculates the number of | |
* times it occurs. times(List('a', 'b', 'a')) should return List(('a', 2), ('b', 1)) | |
*/ | |
def howManyTimes(w: String): List[(Char, Int)] = { // w = "Wooord" | |
val grouped = w.groupBy(x => x.toLower) // Map(d -> d, w -> W, r -> r, o -> ooo) | |
val occ = grouped.mapValues(_.length) // Map(d -> 1, w -> 1, r -> 1, o -> 3) | |
occ.toList // List((d,1), (w,1), (r,1), (o,3)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A simple function | |
function add(a, b) { | |
console.log(a + b); | |
} | |
// A first class function | |
// It can be passed along as a variable | |
var add = function (a, b) { | |
console.log(a + b); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If I define the 'prop' function | |
function prop(name) { | |
return function (object) { | |
return object[name]; | |
}; | |
} | |
// Instead of this | |
var str = "Mentioned by " + tweeps.map(function (t) { | |
return t.name; |
OlderNewer