Basics and Data Types
Nice intro into R for programmers.
R uses $ in a manner analogous to the way other languages use dot.
R has several one-letter reserved words: c, q, s, t, C, D, F, I, and T.
Comment
Assign a variable
Use <-
symbols to assign value to a variable.
We can assign variable in oposit direction too:
1 -> x
As equivalent, we can use assign
function to assign value to a variable.
Print out value of an object
We can simply type name of a variable and that will print it out by itself. Or we can use print
function to print it out.
Sequence
We can create sequence of number as follows and print it the same way as mentioned above.
Structure of object
Find structure of an object.
str
function is very useful function that display compact structure of an object. It can return about any object, so we can use it also for functions.
Summary function
In case we want more information about an object, we can use summary
function.
Arrays
You cannot have array of multiples types. So, you cannot mix integers with strings in one array.
Now we can play with arrays to find out how they work with types.
Numbers
If you type 1
, it gives numeric type 1
.But if you type 1L
, it give 1
as integer type.
Attributes
An object can have attributes. To access attributes, use attributes()
. That allows you to set or modify the attributes.
Vectors
To create empty vector, use this.
c()
function will create a vector. So you can create vector like this:
Or we can create verctor like using vector
function.
What happens if you try to mix types in vecotor? R will try to convert it to the same type. So, a surprise might come out.
That leads to thing that you can convert variables to types using as. Like:
For example, you cannot convert strings to numbers.
List
In list, you can mix types.
Recursive lists.
List is index with double brackets.
Matrices
Special type of vector.
Or if you want to create matrix with values.
Matrix can be created from vector.
Or you can use cbind or rbind to create matrix.
Factors
Factor is self-describing categorical data. It can be ordered or not ordered.
Factors can be created with factors() function.
Factores are represented as numbers internally. You can set ordering using level attribute.
Missing values
You can yous is.na() function to test whether a value is na.
Data Frames
Data frames store tabular data and they can be of different classes. Every raw has names.
Names
All R objects can have names. It can help to create self-describing data.
Lists can have names too.
How to access variable in list.
R will try to determin what variable you want even if you not provide full name.
Matrix can have names too (dim names).
Last updated