options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源其次,在Rstudio→Tools→Global options→Packages里面也能设置。
install.packages(“package”) or BiocManager::install(“package”) 前者是安装来源于CRAN网站的包,后者是来源于Bioconductor网站的包。
一般用library("package")即可
test <- iris[c(1:2,51:52,101:102),]
> head(test) #head()函数观察下数据的前6行
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
101 6.3 3.3 6.0 2.5 virginica
102 5.8 2.7 5.1 1.9 virginicamutate(test, new = Sepal.Length * Sepal.Width)
> mutate(test, new = Sepal.Length * Sepal.Width)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
1 5.1 3.5 1.4 0.2 setosa 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70
51 7.0 3.2 4.7 1.4 versicolor 22.40
52 6.4 3.2 4.5 1.5 versicolor 20.48
101 6.3 3.3 6.0 2.5 virginica 20.79
102 5.8 2.7 5.1 1.9 virginica 15.66select(test,1) #列号
> select(test,1)
Sepal.Length
1 5.1
2 4.9
51 7.0
52 6.4
101 6.3
102 5.8
select(test, Petal.Length, Petal.Width) #列名
> select(test, Petal.Length, Petal.Width)
Petal.Length Petal.Width
1 1.4 0.2
2 1.4 0.2
51 4.7 1.4
52 4.5 1.5
101 6.0 2.5
102 5.1 1.9filter(test, Species == "setosa"&Sepal.Length > 5 ) #此处结合了逻辑取值
> filter(test, Species == "setosa"&Sepal.Length > 5 )
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosaarrange(test, desc(Sepal.Length)) #此处利用desc更改了排序方向
> arrange(test, desc(Sepal.Length))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7.0 3.2 4.7 1.4 versicolor
2 6.4 3.2 4.5 1.5 versicolor
3 6.3 3.3 6.0 2.5 virginica
4 5.8 2.7 5.1 1.9 virginica
5 5.1 3.5 1.4 0.2 setosa
6 4.9 3.0 1.4 0.2 setosa此处需要详细的视频进行理解!
强烈推荐珍藏视频,来自张敬信老师的语法串讲
https://www.bilibili.com/video/BV1HQ4y1v75M
tidyverse包中还有一个非常重要的工具:管道。%>%,在视频中也有讲解!
设置练习数据
test1 <- data.frame(x = c('red','blue','yellow','green'), z = c("A","B","C",'D'))
test2 <- data.frame(x = c('pink','orange','red','blue'), y = c(1,2,3,4))inner_join(test1, test2, by = "x") #test1和test2按照x列中都有的取
> inner_join(test1, test2, by = "x")
x z y
1 red A 3
2 blue B 4left_join(test1, test2, by = 'x') #以test1为主要的,按照x列合并test2
> left_join(test1, test2, by = 'x')
x z y
1 red A 3
2 blue B 4
3 yellow C NA
4 green D NAfull_join( test1, test2, by = 'x') #取全集
> full_join( test1, test2, by = 'x')
x z y
1 red A 3
2 blue B 4
3 yellow C NA
4 green D NA
5 pink <NA> 1
6 orange <NA> 2semi_join(x = test1, y = test2, by = 'x') #注意是谁匹配谁
> semi_join(x = test1, y = test2, by = 'x')
x z
1 red A
2 blue Banti_join(x = test2, y = test1, by = 'x')
> anti_join(x = test2, y = test1, by = 'x')
x y
1 pink 1
2 orange 2bind_rows(test1, test2)
> bind_rows(test1, test2)
x z y
1 red A NA
2 blue B NA
3 yellow C NA
4 green D NA
5 pink <NA> 1
6 orange <NA> 2
7 red <NA> 3
8 blue <NA> 4
#还有bind_cols()原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。