[index]

# Basic Operations

### simple math [source]

```a1<-c(1:5)
a1 + 5
a1 - 3
a1 * 4
a1 ^ 2
a1 / 2
a1 %% 2
a2 <- a1 / 2
```
vector vs vector
`a1 + a2 (- * /)`
but:
```b1<-c(1:6)
a1 + b1```
does that work? why?
```b2<-c(1:10)
a1 + b2```
does that work? why?

using pre-defined functions
```sqrt(a1)
log2(a1)
exp(a1)
sin(a1)
```

### operations on vectors/matrices [source]

```c1<-runif(20)
length(c1)
min(c1) max(c1) sum(c1)
median(c1)
mean(c1)
sd(c1)
summary(c1) 		// all kinds of meta info
intersect(a1,a2)	// return the intersection of given vectors
union(a1,a2)
```
modify vectors
```c2<-append(c1,24:42) 			// append values to a vector
c3<-append(c1,24:42,after=length(c1)/2)	// at a certain index
c4<-match(24:42,c1) 			// find entries in each of the 2 vectors - what will this return?
c5<-match(24:42,c2) 			// what will be returned here?
c6<-match(24:42,c3)
```
combine vectors to a matrix
```d1<-cbind(a1,a2) 	// by column
d2<-rbind(a1,a2) 	// by row
d3<-t(d1) 		// transpose of d1, same thing as d2 now
d4<-merge(a1,a2) 	// merge, similar to DB join
dim(d4) 		// get the dimension of the matrix
d5<-apply(d1,1,sum) 	// apply the function 'sum' to all rows of matrix d1
d6<-apply(d1,2,sum)	// apply the function 'sum' to all columns of matrix d1
```
logic operations
```e1<-c1 > 0.5 		// return T or F for each index depending on condition
e2<-d1 > 1 		// ditto
```

### sorting (not so basic anymore) [source]

order function
```x<-sample(1:10,20,replace=T)	// 3 vectors containing 20 unsorted values each
y<-sample(1:10,20,replace=T)
z<-sample(1:10,20,replace=T)
xyz<-rbind(x,y,z) 		// bind to matrix
o<-order(x,y,z) 		// order by x, ties are broken by y and further by z; o holds indices
xyzo<-xyz[,o] 			// ordered matrix
```
sort function
```x1<-sort(x)
x2<-sort(x,decreasing=T) 	// decreasing order
x3<-sort(x,partial=c(3,4)) 	// partial values are in correct position and all smaller values are to the left, all greater to the right```
rank function
```x4<-rank(x)
x5<-rank(x,ties="average") 	// ties handled as follows: rank eqs mean value
x6<-rank(x,ties="first") 	// first come first serve
x7<-rank(x,ties="random") 	// random order
x8<-rank(x,ties="min") 		// all ties get min rank
x9<-rank(x,ties="max") 		// all ties get max rank
```

### correlation and linear regression [source]

correlation
```xx<-sample(10)				// 2 vectors holding values 1..10 in random order
yy<-sample(10)
xxyy1<-cor(xx,yy)			// correlation value (default=pearson)
xxyy2<-cor(xx,yy,method="kendall")	// kendall
xxyy3<-cor(xx,yy,method="spearman")	// spearman
```
linear regression (least squares)
```x<-c(1:70)			// vector, x-axis
y<-sample(50:0,70,replace=T)	// random values for y-axis
y<-y[order(-y)]			// sort and reverse order
plot(x,y,pch=20)		// plot dots
model<-lm(y~x)			// linear regression formula y=mx+n
abline(model)			// plot regression line