Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
95 views2 pages

R Cheat Sheet: Basic Syntax

The document provides a cheat sheet on basic R syntax and common operations for vectors, matrices, and data frames. It includes tables listing various symbols and their uses for comments and assignment, vector and matrix operations like concatenation and selection, and some utility functions. Examples are given demonstrating how to use symbols for comments, assignment, vector selection, matrix indexing, and common operations like multiplication, division, and concatenation. Commonly used utility functions like length(), dim(), sort(), and order() are also listed with brief descriptions.

Uploaded by

JF SV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views2 pages

R Cheat Sheet: Basic Syntax

The document provides a cheat sheet on basic R syntax and common operations for vectors, matrices, and data frames. It includes tables listing various symbols and their uses for comments and assignment, vector and matrix operations like concatenation and selection, and some utility functions. Examples are given demonstrating how to use symbols for comments, assignment, vector selection, matrix indexing, and common operations like multiplication, division, and concatenation. Commonly used utility functions like length(), dim(), sort(), and order() are also listed with brief descriptions.

Uploaded by

JF SV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

R Cheat Sheet

Basic Syntax

The following is a list of symbols that can be used in R. Please review the table below
for specific description and examples related to the listed syntaxes.

Symbol Description Example


# Comments # This is not interpreted
<- or = Assignment a <- 1; b = 2
<<- Global Assignment a <<- 10 # Not recommended
v[1] First element in a vector v[1]
* Scalar Multiplication c(1,1)*c(1,1) # 1 1
%*% Matrix Multiplication c(1,1)%*%c(1,1) # 2
/ Division 1/2 # 0.5
%/% Integer Division 1%/%2 # 0
%% Remainder 7%%6 # 1

Vector and Matrix Operations

Vectors can be used for concatenating rows and columns. Please review the table
below for examples related to vector and matrix operations in R.

Symbol Description Example


c() Concatenate v <- c(1,2,3,4) # 1 2 3 4
cbind() Column Concatenate cbind(v,v) # 2 Columns
rbind() Row Concatenate rbind(v,v) # 2 Rows
matrix() Create matrix mat <- matrix(v,nrow=2,ncol=2)
Selection

The following is a list of symbols that can be used in R. Please review the table below
for specific description and examples related to the listed syntaxes that can be used for
various selections.

Symbol Description Example


v[1] Select first # This is not interpreted
tail(v,1) Select last a <- 1; b = 2
mat[2,1] Select row 2, column 1 a <<- 10 # Not recommended
mat[1,] Select row 1 v[1]
mat[,2] Select column 2 c(1,1)*c(1,1) # 1 1
v[c(1,3)] Select the first and third values c(1,1)%*%c(1,1) # 2
v[-c(1,3)] Select all but the first and third values 1/2 # 0.5
mat[,c(1,2)] Select columns 1 and 2 1%/%2 # 0
mat[,1:5] Select columns 1 to 5 7%%6 # 1

Utility

Please review the table below for a list of most commonly used utility functions in R.

Symbol Description
length() Length of vector
dim() Dimensions of vector/matrix/dataframe
sort() Sorts vector
order() Index to sort vector e.g. sort(v) == v[order(v)]
names() Names of columns

You might also like