R包是多个函数的集合,具有详细的说明和示例,help(R包)
运行代码:
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
install.packages("包")
BiocManager::install("包")
install.packages("dplyr")#镜像设置后使用代码
library(dplyr)#使用require()命令也可加载包
示例数据:内置数据iris简化版
test <- iris[c(1:2,51:52,101:102),]
mutate(test, new = Sepal.Length*Sepal.Width)#在变量test的数据框新增列,列名是new,数值是Sepal.Length列的值和Sepal.Width列的值相乘。
问题和思考
在我练习select()时,想选择刚新增的列,发现报错。然后发现运行mutate(test, new = Sepal.Length*Sepal.Width)
后,查看test后发现test本身没有变。
因此我想新增列只是一个操作,不会使变量test本身多一列,若想要对test数据框真实多一列,需重新对test进行赋值,具体如下:
test <- mutate(test, new = Sepal.Length*Sepal.Width)
,然后查看变量test,test多了名为new的列。
select(test,6)#按列号筛选
# new
#1 17.85
#2 14.70
#51 22.40
#52 20.48
#101 20.79
#102 15.66
select(test,c(1,4))#按向量包含的列号筛选
# Sepal.Length Petal.Width
#1 5.1 0.2
#2 4.9 0.2
#51 7.0 1.4
#52 6.4 1.5
#101 6.3 2.5
#102 5.8 1.9
select(test,Species)#按列名筛选
# Species
#1 setosa
#2 setosa
#51 versicolor
#52 versicolor
#101 virginica
#102 virginica
select(test,Sepal.Length,Sepal.Width)
# Sepal.Length Sepal.Width
#1 5.1 3.5
#2 4.9 3.0
#51 7.0 3.2
#52 6.4 3.2
#101 6.3 3.3
#102 5.8 2.7
vars <- c("Petal.Length","Petal.Width")#赋值
vars#变量
select(test,one_of(vars))
select(test,vars)
select(test,c("Petal.Length","Petal.Width"))
filter(test,Species == "setosa")
# 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
filter(test,Species == "setosa"&Sepal.Length > 5)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
#1 5.1 3.5 1.4 0.2 setosa 17.85
filter(test,Species %in% c(c("setosa","virginica")))
# 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
#3 6.3 3.3 6.0 2.5 virginica 20.79
#4 5.8 2.7 5.1 1.9 virginica 15.66
##2.4 按某一列或某几列对整个表格进行排序
arrange(test,Sepal.Width)#默认从小到大排序
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
#1 5.8 2.7 5.1 1.9 virginica 15.66
#2 4.9 3.0 1.4 0.2 setosa 14.70
#3 7.0 3.2 4.7 1.4 versicolor 22.40
#4 6.4 3.2 4.5 1.5 versicolor 20.48
#5 6.3 3.3 6.0 2.5 virginica 20.79
#6 5.1 3.5 1.4 0.2 setosa 17.85
arrange(test,desc(Sepal.Width))#从大到小需要用到desc
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
#1 5.1 3.5 1.4 0.2 setosa 17.85
#2 6.3 3.3 6.0 2.5 virginica 20.79
#3 7.0 3.2 4.7 1.4 versicolor 22.40
#4 6.4 3.2 4.5 1.5 versicolor 20.48
#5 4.9 3.0 1.4 0.2 setosa 14.70
#6 5.8 2.7 5.1 1.9 virginica 15.66
对数据进行汇总操作,结合group_by体验强实用性
summarise(test,mean(Sepal.Width),sd(Petal.Width))
## mean(Sepal.Width) sd(Petal.Width)
##1 3.15 0.9239408
group_by(test,Species)
# A tibble: 6 × 6
# Groups: Species [3]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
# <dbl> <dbl> <dbl> <dbl> <fct> <dbl>
#1 5.1 3.5 1.4 0.2 setosa 17.8
#2 4.9 3 1.4 0.2 setosa 14.7
#3 7 3.2 4.7 1.4 versicolor 22.4
#4 6.4 3.2 4.5 1.5 versicolor 20.5
#5 6.3 3.3 6 2.5 virginica 20.8
#6 5.8 2.7 5.1 1.9 virginica 15.7
summarise(group_by(test,Species),mean(Sepal.Width),sd(Petal.Width))
# A tibble: 3 × 3
# Species `mean(Sepal.Width)` `sd(Petal.Width)`
# <fct> <dbl> <dbl>
#1 setosa 3.25 0
#2 versicolor 3.2 0.0707
#3 virginica 3 0.424
理解如下命令,以后多练习
test %>%
+ group_by(Species) %>%
+ summarise(mean(Sepal.Length), sd(Sepal.Length))
# A tibble: 3 × 3
# Species `mean(Sepal.Length)` `sd(Sepal.Length)`
# <fct> <dbl> <dbl>
#1 setosa 5 0.141
#2 versicolor 6.7 0.424
#3 virginica 6.05 0.354
count()
count(test,Species)#统计变量test的Species列中每个元素出现次数(unique)
#Species n
#1 setosa 2
#2 versicolor 2
#3 virginica 2
test1 <- data.frame(x = c("a","b","c","d"),z = c("1","2","3","4"))
test2 <- data.frame(x = c("a","c","d","x","y","z"),y = c("A","B","C","D","E","F"))
test1
# x z
#1 a 1
#2 b 2
#3 c 3
#4 d 4
test2
# x y
#1 a A
#2 c B
#3 d C
#4 x D
#5 y E
#6 z F
inner_join(test1,test2,by = "x")#提取出对变量test1、test2中列名相同的列中相同的元素的行,合并到一个数据框(test1在左,test2在右)。
# x z y
#1 a 1 A
#2 c 3 B
#3 d 4 C
> left_join(test1,test2,by = "x")#test1在左,test2取x列中和test1中x列有交集的置于test1右侧,无交集的现实<NA>
# x z y
#1 a 1 A
#2 b 2 <NA>
#3 c 3 B
#4 d 4 C
> left_join(test2,test1,by = "x")#test2在左,test1取x列中和test1中x列有交集的置于test2右侧,无交集的现实<NA>
# x y z
#1 a A 1
#2 c B 3
#3 d C 4
#4 x D <NA>
#5 y E <NA>
#6 z F <NA>
> full_join(test1,test2,by = "x")
# x z y
#1 a 1 A
#2 b 2 <NA>
#3 c 3 B
#4 d 4 C
#5 x <NA> D
#6 y <NA> E
#7 z <NA> F
> full_join(test2,test1,by = "x")#test1和test2前后互换一下看看区别
# x y z
#1 a A 1
#2 c B 3
#3 d C 4
#4 x D <NA>
#5 y E <NA>
#6 z F <NA>
#7 b <NA> 2
> semi_join(x = test1,y = test2,by = "x")
# x z
#1 a 1
#2 c 3
#3 d 4
> semi_join(x = test2,y = test1,by = "x")
# x y
#1 a A
#2 c B
#3 d C
> anti_join(x = test1,y = test2,by = "x")
# x z
#1 b 2
> anti_join(x = test2,y = test1,by = "x")
# x y
#1 x D
#2 y E
#3 z F
> test1 <- data.frame(x = c(11,12,13,14), y = c(11,22,33,44))
> test2 <- data.frame(x = c(33.33,66.66), y = c(88,99))
> test3 <- data.frame(x =c(22,23,24,25),y = c(55,66,77,88),z = c(99,100,111,122))
> test1
# x y
#1 11 11
#2 12 22
#3 13 33
#4 14 44
> test2
# x y
#1 33.33 88
#2 66.66 99
> test3
# x y z
#1 22 55 99
#2 23 66 100
#3 24 77 111
#4 25 88 122
> bind_cols(test1,test3)
#New names:
#• `x` -> `x...1`
#• `y` -> `y...2`
#• `x` -> `x...3`
#• `y` -> `y...4`
# x...1 y...2 x...3 y...4 z
#1 11 11 22 55 99
#2 12 22 23 66 100
#3 13 33 24 77 111
#4 14 44 25 88 122
> bind_rows(test1,test2)
# x y
#1 11.00 11
#2 12.00 22
#3 13.00 33
#4 14.00 44
#5 33.33 88
#6 66.66 99
> cbind(test1,test2)
# x y x y
#1 11 11 33.33 88
#2 12 22 66.66 99
#3 13 33 33.33 88
#4 14 44 66.66 99
> cbind(test1,test2,test3)
# x y x y x y z
1# 11 11 33.33 88 22 55 99
#2 12 22 66.66 99 23 66 100
#3 13 33 33.33 88 24 77 111
#4 14 44 66.66 99 25 88 122
内容参考微信公众号 生信星球,自己实践总结。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。