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 beena littlebitmoreinvolved, 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….)
> 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.10.20.30.40.5> bb+cc # more basic math[1]1112131415> 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]2422201816>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.
161820202224
You can even set up your own equations as “functions” in R.
> 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.
> 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]232632329706751> 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
118714422236394331222044426343955279467> table.1+10# add 10 to every item in your table
var.1 var.2 var.3
11197154212246404313132214414273449515289477> table.1*var.1 # multiply your table by var.1
var.1 var.2 var.3
118714424472788393666124161052175652513952335># 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
1126128824236118239244204447898785102791401> 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
1629582779157384181498617451091185> table.1+table.2 # add table.1 and table.2 together
var.1 var.2 var.3
1711620229315551311163285413349613515370652> table.1*table.2 # multiply table.1 and table.2 together
var.1 var.2 var.3
1625238352214186446185832450021652443622618763865502538986395
Creating a matrix by multiplying vectors is also very easy.
> 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!678910167891021214161820318212427304242832364053035404550
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….)
Creating objects in R can make it convenient to perform calculations.
You can even set up your own equations as “functions” in R.
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.
Creating a matrix by multiplying vectors is also very easy.
Related posts (possibly):