R is like a giant calculator for grownups

One of the things that is interesting about R is how flexible it is. One of the fun things about it is how interactive it can be. While my examples so far have been a little bit more involved, it can be useful to spend some time just getting acquainted with how R performs basic calculations. In fact, I sometimes like to think of R as a giant calculator for grownups to play with. The following syntax snippets show how you can perform basic calculations with R. This is by no means complete, but it should provide a reasonable introduction to someone just getting started with R. (Experienced R users would find this TOTALLY useless….)

Let’s get started with really basic stuff…. (By the way, everything in the syntax following the “#” is treated as a comment and is not processed by R. They are just there to explain what’s going on at each step for the uninitiated….)

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> 2+2 # addition
[1] 4
> 2-2 # subtraction
[1] 0
> 2*3 # multiplication
[1] 6
> 2/2 # division
[1] 1
> 2^3 # exponents
[1] 8
> sqrt(16)/2 # square root
[1] 2
> 16^(1/2) # square root (as exponent)
[1] 4
> 8^(1/3) # cubed root (you get the idea)
[1] 2

Creating objects in R can make it convenient to perform calculations.

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> aa = 8^(1/3) # assigning a variable with the name "aa"
> aa^2 # aa squared
[1] 4
> (8^(1/3))^2 # same as "aa^2" above
[1] 4
> bb = c(1,2,3,4,5) # an object with the numbers 1 to 5, named "bb"
> cc = 10 # a single object with the value "10"
> bb/cc # doing basic math on the objects
[1] 0.1 0.2 0.3 0.4 0.5
> bb+cc # more basic math
[1] 11 12 13 14 15
> dd = c(25:21) # another way to create a series of numbers (descending)
> dd-bb # subtracts 1 from 25, 2 from 24, and so on
[1] 24 22 20 18 16
> sum(dd-bb) # "sum" of the resulting values of the expression "dd-bb"
[1] 100
> mean(dd-bb) # average of the resulting values of the expression "dd-bb"
[1] 20
> ddd = dd-bb # you can even create *another* object....
> mean(ddd) # you get the idea...
[1] 20
> summary(ddd) # convenient summary statistics about a distribution
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
     16      18      20      20      22      24

You can even set up your own equations as “functions” in R.

?View Code RSPLUS
1
2
3
4
5
6
7
8
> area.circ = function(r) {pi*r^2} # solving simple equations
> area.circ(1) # calculating for a radius of 1...
[1] 3.141593
> area.circ(2) # ... and a radius of 2
[1] 12.56637
> y = function(m,x,b) (m*x+b) # more simple equations... set up the function
> y(12,2,2) # and feed R the values that you want to use
[1] 26

A table (a data frame, really) is an example of an object in R. Below, we create two tables, each with 5 rows and 3 columns. Then, we perform some basic calculations on the tables.

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
> var.1 = c(1:5) # create a vector with the numbers 1 to 5
> set.seed(123) # set a seed so you can replicate this
> var.2 = c(sample(300,5)) # randomly sample 5 numbers from 300
> set.seed(123) # ...
> var.3 = c(sample(500,5)) # randomly sample 5 numbers from 500
> var.1 + var.2 + var.3 # do some basic mathematics
[1] 232 632 329 706 751
> table.1 = data.frame(var.1, var.2, var.3) # put your objects into a table
> table.1 # view your table
  var.1 var.2 var.3
1     1    87   144
2     2   236   394
3     3   122   204
4     4   263   439
5     5   279   467
> table.1+10 # add 10 to every item in your table
  var.1 var.2 var.3
1    11    97   154
2    12   246   404
3    13   132   214
4    14   273   449
5    15   289   477
> table.1*var.1 # multiply your table by var.1
  var.1 var.2 var.3
1     1    87   144
2     4   472   788
3     9   366   612
4    16  1052  1756
5    25  1395  2335
> # notice that R goes from top to bottom and loops.
> table.1*c(1:3) # Try the same thing with multiplying by c(1:3))
  var.1 var.2 var.3
1     1   261   288
2     4   236  1182
3     9   244   204
4     4   789   878
5    10   279  1401
> var.4 = c(6:10) # set up some new variables
> set.seed(123)
> var.5 = c(sample(100,5))
> set.seed(123)
> var.6 = c(sample(200,5))
> table.2 = data.frame(var.4, var.5, var.6) # and create a second table
> table.2 # view your table
  var.4 var.5 var.6
1     6    29    58
2     7    79   157
3     8    41    81
4     9    86   174
5    10    91   185
> table.1+table.2 # add table.1 and table.2 together
  var.1 var.2 var.3
1     7   116   202
2     9   315   551
3    11   163   285
4    13   349   613
5    15   370   652
> table.1*table.2 # multiply table.1 and table.2 together
  var.1 var.2 var.3
1     6  2523  8352
2    14 18644 61858
3    24  5002 16524
4    36 22618 76386
5    50 25389 86395

Creating a matrix by multiplying vectors is also very easy.

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
> aa = var.1 %o% var.4; # create a matrix multiplying var.1 by var.4
> row.names(aa) = var.1; colnames(aa) = var.4 # set your row and column names
> aa # view your output. It's a nice multiplication table!
   6  7  8  9 10
1  6  7  8  9 10
2 12 14 16 18 20
3 18 21 24 27 30
4 24 28 32 36 40
5 30 35 40 45 50

Related posts (possibly):

  1. A sample size calculator function for R IMPORTANT: This is here mostly to remind me of how...
  2. The new sample size calculator for R (already) aka “Maybe I shouldn’t post so quickly” Just hours ago,...
  3. Getting data into R When you first open R, you’re greeted with a screen...
  4. Quickly reshaping data from “wide” to “long” formats in R A lot of the times, students at the Academy enter...
  5. Stratified Random Sampling in R–A Function in Progress IMPORTANT: This is here mostly to remind me of how...
This entry was posted in (all categories), Geekiness, Useless Knowledge and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.