R basics


这里是关于一些R语言的语法备忘 Learning Website

Unit 1 Assignment and basic calculation

myapples = 3


myapples <- 3

+, -, *, /, ^

%% means the remainder.

Unit 2 Vectors

combine function: c()

numeric_vector = c(1,2,3) #Or c(1:3)

sum()calculates the sum of all elements of a vector. mean() calculates the average of all elements of a vector. Selection by comparison: logical comparison operator: <, >, <=, >=, ==, !=.


Unit 3 Matrices

Useful functions: matrix(), colnames(),rownames, rbind, cbind. rowSums(), colSums(), e.g.

>matrix(1:9,byrow =TRUE, nrow = 3)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

>new_hope <- c(460.998, 314.4)
>empire_strikes <- c(290.475, 247.900)
>return_jedi <- c(309.306, 165.8)
>star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE)
>region <- c("US", "non-US")
>titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")

#Usage of colnames and rownames


                             US non-US
A New Hope              460.998  314.4
The Empire Strikes Back 290.475  247.9
Return of the Jedi      309.306  165.8

# Usage of cbind and rbind

big_matrix <- cbind(matrix1, matrix2, vector1 ...)
big_matrix  = rbind(matrix1, ...)


Unit 4 Factor

The term factor refers to a statistical data type used to store categorical variables. The difference between a categorical variable and a continuous variable is that a categorical variable can belong to a limited number of categories. A continuous variable, on the other hand, can correspond to an infinite number of values.

factor_speed_vector <-factor(speed_vector, ordered=TRUE, levels=c("slow","fast","insane"))

Unit 5 Data Frame

Useful fuctions show below: head(variables) shows the first observations of a data frame tail(variables) shows the last obseravations of a variables str() get a quick overview of data data.frame(vectors1, vectors2, ...) combine vectors into one data $sign: e.g. planets_df$diameter when data have names

subset(my_df, subset = some_condition), e.g. subset(planet_df, diameter<1) order()interesting function e.g.

>a = c(100,10,1000)
[1] 2 1 3

[1] 10 100 1000
#the comma is the solid brakets is crucial.

Unit 6 List

List can have kinds of components: vector, matrices and data frames.

My_list = list(my_vector, my_matrix, my_df)

Change the name of list

names(my_list)=c("vec", "mat", "df")


my_list = list(my_vec=vec, my_matrix=mat,...)

To conveniently add elements to lists you can use the c() function, that you also used to build vectors:

ext_list <- c(my_list , my_val)

This will simply extend the original list, my_list, with the component my_val. This component gets appended to the end of the list. If you want to give the new list item a name, you just add the name as you did before:

ext_list <- c(my_list, my_name = my_val)

Edit this page

Dr. Hui Lin (林辉)
Dr. Hui Lin (林辉)
Assistant Researcher

My research interests include Dissolved Organic Matter (DOM) in natural waters, marine chemistry using isotope methodology and freshwater sciences.