how to do stuff in r. part 1.

So I’ve been reading a lot about R while reading a lot about machine learning while reading a lot about image processing :-). Well, currently I’m in the process of learning Processing (yeah, bad pun..), but I’ve come across a really neat website with 2minutes-tutorials on R by ajdamico. 90 of them, and I do have some spare time.
(Granted, the tutorials take a little longer than two minutes to complete, but it’s a great project for a otherwise boring work week).

So, my notes on the tutorials, part 1-6:

1) Download and install R.
Download for Win on – http://cran.r-project.org/bin/windows/base/ (not in the mood to reboot..); install..
print("Hello, world")
[1] "Hello, world"

Magic.

2) Simple shortcuts for the windows r console
1+1 returns 2 (Runs the script by enter)
Alt+F+N in English/Alt+D+N in German (this sucks, btw) opens a new scripting file
Ctrl+R runs the code in your new file.
Alt+W+v/(Alt+W+c-jeez, this sucks, I’m considering reinstalling) to arrange the window vertically.

Arranging the windows vertically

The rest was just the regular Ctrl+A, Shift+End/typical selection in any normal text editor.

3) Simple arithmetic in R
Well, this one was not too hard. But I adore the podcaster. Note the %% as modulo..

> 5%%3
[1] 2
> pi
[1] 3.141593
> options (digits=20)
> pi
[1] 3.141592653589793116
> factorial(3)
[1] 6
> sqrt(4)
[1] 2
> -Inf
[1] -Inf

4) Create a variable with R
The mathematical operator of := is represented with <-. R’s behaviour is not really surprising. It can’t multiply texts with numbers. Jeez.

f
> x x+2
[1] 3
> y y
[1] "Hello"
> x*y
Fehler in x * y : nicht-numerisches Argument für binären Operator
> y x*y
[1] 5

(Update 10/’12: Some reading on error handling in R)

5) The combine (c) function
C basically seems to creae an array/a vector. The : is very familiar from other languages, and while it’s not mentioned, I’m itching to search for a way to change the increment from 1/-1 to somehing else.
> x x*4
[1] 4 8
> 45:34
[1] 45 44 43 42 41 40 39 38 37 36 35 34

6) What does object oriented statistical programming really mean?
(Or: Oh, no, it starts to count with 1!)
> x length(x)
[1] 11
> class(x)
[1] "integer"
> x[2]
[1] 315
> x[0]
integer(0)
> x[12]
[1] NA
> y length(y)
[1] 5
> length(y)==length(x)
[1] FALSE
> z z
[1] FALSE

Moar tomorrow..

Advertisements

2 comments

  1. […] from how to do stuff in R, following the […]

  2. Stefan · · Reply

    Hatte den Fehler “nicht-numerisches argument für binären operator” – klar, Zahlen mit Strings multipliziert. Danke!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: