1. Introduction
R is an object-oriented programming language. Almost anything in R could be an object.
2. Datatype in R
a. Summary
NB:
(1) numeric class in R should be double/floating point.
(2) We would say factor is something between class and structure. It is a structure but behave more like a class.
(3) Different with python, we call string as character in R.
(4) Some useful function to check the type of an object:
Function | Application |
---|---|
typeof() | what is it? i.e. how is it saved in memory? (not common used) |
class() | what is it? i.e. how is it used in functions? |
str() | what is the structure? (frequently used, also tell you class) |
(5)
Conversion between different classes/structures: as.numeric()
, as.logical()
, as.integer()
, as.factor()
, as.character()
, as.matrix()
, as.list
, as.data.frame()
…
Logical test for class/structures of object: is.numeric()
, is.logical()
, is.integer()
, is.factor()
, is.character()
, is.matrix()
, is.list()
, is.data.frame()
…
b. Logical values
Logical values are created by logical expressions. When coerced to numeric, TRUE = 1
, FALSE = 0
.
logical operators:
Operators | Use |
---|---|
> | less than |
>= | less than or equal |
< | greater than |
<= | greater than or equal |
== | equal to |
!= | not equal |
! | not |
& | and |
| | or |
c. factor
Factors used for classification - categories
Take gender as an example:
d. special values
Missing numbers are NA
Undefined numbers (e.g. division by zero) are NaN
3. Frequently-used function:
Function | application |
---|---|
c(x1, x2) | combine things together |
min(x) | minimum value |
max(x) | maximum value |
range(x) | get the range.same as c(min(x),max(x)) |
length(x) | get the length of x |
sort(x) ; order(x, …) | sorting |
sum() | sum |
prod() | product |
mean(), median(), sd(), var() | statistical functions to get mean, median, standard derivation, variance |
summary(x) | statistical summary |
paste() | see example below (detail see in ?paste()) |
paste() 函数: