R: 基础学习

1. Arithmetic with R

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: ^
  • Modulo: %% (模数:指的是a/n的余数)

2. 数据类型

2.1 Vector

-- one dimensional array. The elements in a vector all have the same data type.

2.2 Matrix

-- two dimensional array. The elements in a matrix all have the same data type.

 1) Construct a matrix (with 3 rows that contain the numbers 1 up to 9)

matrix(1:9, byrow = TRUE, nrow = 3)

2) Add a name for row and column

rownames, colnames

rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector

3) Calculate the total of row (create a new vector)

rowSums, colSums

rowSums(my_matrix)

4) Add a column / row

cbind, rbind

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

5) Selection of matrix elements

my_matrix[ , n], my_matirx[n, ]

2.3 Factor

-- two dimensional objects. Within a column all elements have the same data tyoe, but different columns can be of different data type.

encode the vector as a factor

factor( )

create an ordered factor

factor(some_vector,
       ordered = TRUE,
       levels = c("lev1", "lev2" ...))

设置 factors' level

levels(factor_vector) <- c("name1", "name2",...)

总结: summary()

2.4 data frame

1) have a look at the head/tail: head(), tail() 

2) look the structure: str() 

3) construct a data frame: data.frame()

4) selection of data frame elements

df[n, m] # 和定坐标差不多

df[1:5, m] #选择第m列的1到5行

subset(my_df, some_condition)

5) sort the data frame

2.5 List

1) creat a list

my_list <- list(comp1, comp2, ...)

2) create a named list

my_list <- list(name11 = your_comp1, name2 = your_comp2)
my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")

3) select elements from a list

we can use [[ ]] or $ sign to select

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值