R knows about integers, floating point numbers, even complex numbers like 1+2i.
In [1]:
2+2
In [2]:
sin(.1)
In [6]:
1i*1i
In [9]:
(1+2i)*(2+3i)
A vector is a concatenation of numbers, so use the c operator for form the vector.
A variable can be assigned using either the equals sign, or the assignment arrow.
In [14]:
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
In [11]:
x
In [15]:
y = c(x,2,2,x)
In [16]:
y
R has a funny way of adding vectors of different lengths. It just recycles the entries from the shorter vector.
In [18]:
x+y
In [19]:
1/x
In [20]:
y/x
In [21]:
mean(x)
In [22]:
var(x)
In [23]:
# a direct way to compute the variance
sum((x-mean(x))^2)/(length(x)-1)
In [24]:
m = array(c(1,2,3,4),dim=c(2,2))
In [25]:
m
In [26]:
det(m)
In [27]:
m*m
In [28]:
m%*%m
In [29]:
m^2
In [31]:
plot(c(1,2,3,4))
In [32]:
# Define 2 vectors
cars <- c(1, 3, 6, 4, 9)
trucks <- c(2, 5, 4, 5, 12)
# Graph cars using a y axis that ranges from 0 to 12
plot(cars, type="o", col="blue", ylim=c(0,12))
# Graph trucks with red dashed line and square points
lines(trucks, type="o", pch=22, lty=2, col="red")
# Create a title with a red, bold/italic font
title(main="Autos", col.main="red", font.main=4)
In [ ]: