2019/09/15 开始学习R语言(链接:https://www.bilibili.com/video/av6268508?from=search&seid=6572374907001663901)
-
查看已安装的包: installed.packages()
-
移除单个变量: rm(object) #object = 变量名字
-
移除所有变量: rm(list = ls())
-
查看所有变量: list(ls())
-
R语言不支持多行注释,但可以实现多行注释:
if(FALSE) { “This is a demo for multi-line comments and it should
be put inside either a single
OR double quote” }myString <- “Hello, World!” print ( myString)
-
R的数据类型
6.1 经常使用的数据类型: 向量vector、 列表list、 矩阵matrix、数组array、 因子factor、 数据帧dataframe;
6.2 最基本 逻辑性logical、 数字Numeric、 整型integer、复合型complex、 字符character、原型Raw -
列表list 中的元素可以是不同的 可以是向量、列表、函数等
-
注意list列表的创建时 list1 <- list()
-
注意矩阵的创建格式是: matrix1 <- matrix(c(),nrow= , ncol = , byrow = TRUE)
-
矩阵被限制为二维, array(数组)则打破了这个限制,使用dim属性,注意数组的创建,是array(c(), dim=c())
#create a array
array1 <- array(c("a", "b"),dim = c(3,3,3))
class(array1)
print(array1)
- 因子factor及其创建
#create a factor
apple_colors <- c("green","red","green","green", "yellow","red")
factor_apple <- factor(apple_colors)
print(factor_apple)
print(nlevels(factor_apple)
-
DATAFrame 数据帧: 表格数据对象,同一列数据类型相同,不同列数据之间类型可以不同
-
%/% : 两个向量相除求商
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)
- 所有大于1的数字被认为是逻辑值TRUE;
- &:逻辑and,两个向量对应位置进行运算;
- !:逻辑not ,两个向量对应位置进行运算;
- | : 逻辑or, 两个向量对应位置进行运算;
- && : 取两个向量的第一个元素进行 & 运算;
- || : 取两个向量的第一个元素进行 | 运算;
- %in% : 此标识符用来判断 与元素是否属于向量;
v1 <- 10
v3 <- 44
t <- 1:20
print(v1 %in% t)
print(v3 %in% t)
> print(v1 %in% t)
[1] TRUE
> print(v3 %in% t)
[1] FALSE
- 获取r包的库位置: .libpaths()
- 获取在当前r环境中加载的所有包: search()
- 查看r下载的所有包: library()
- repeat循环框架: 循环体必然至少执行一次
repeat {
commands
if(conditions) {
break
}
}
25.while循环 可以做到一次都不循环,因为他是先判断再执行
while(conditions){
commands
}
- 可以用cbind(c1, c2, c3)函数将c1, c2, c3 三个向量黏合成一个dataframe。
- 函数的创建
函数名 <- function([参数]){
commands
}
28. 连接字符串函数: paste()>>>>>>>>>>paste(... , sep = ' ', collapse = NULL)
... : 表示要接合的任意数量的自变量;
sep: 表示接合之后自变量之间的分割,‘ ’ 即表示空格分隔;
collapse: 消除接合后变量与变量之间的空格;
c1 <- “hello”
c2 <- “how are you”
c3 <- “i am fine”
c <- paste(c1, c2, c3, sep = ‘’,collapse = ‘’)
print©
[1] “hellohow are youi am fine”
29. List item