今天学习的主题是R包,内容多多。。
#是为了加快R包的安装下载速度,要用到R的配置文件.Rprofile
首先用file.edit()来编辑文件:file.edit('~/.Rprofile')
然后在其中添加好下面的两行options代码
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")

保存后重启Rstudio,再运行下面两行代码:
options()$repos
options()$BioC_mirror

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
install.packages("dplyr")
示例数据 test <- iris[c(1:2,51:52,101:102),]

mutate(test, new = Sepal.Length * Sepal.Width)

select(test,1)#筛选第1列内容
select(test,c(1,5))
select(test,Sepal.Length)#筛选列名为Sepal.Length的数据

select(test, Petal.Length, Petal.Width)
vars <- c("Petal.Length", "Petal.Width")#把"Petal.Length", "Petal.Width"数据命名为"vars"
select(test, one_of(vars))

filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))

arrange(test, Sepal.Length)#默认从小到大排序
arrange(test, desc(Sepal.Length))#用desc从大到小

group_by使用summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 先按照Species分组,计算Sepal.Length的平均值和标准差
group_by(test, Species)
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))


count(test,Species)

#将2个表进行连接
inner_join(test1, test2, by = "x")

left_join(test1, test2, by = 'x')#以test1的x列为列,取交集
left_join(test2, test1, by = 'x')#以test2的x列为列,取交集

full_join( test1, test2, by = 'x')#将test1和test2的x列合并为列

semi_join(x = test1, y = test2, by = 'x')

anti_join(x = test2, y = test1, by = 'x')

bind_rows()函数需要两个表格列数相同,而bind_cols()函数则需要两个数据框有相同的行数
bind_rows(test1, test2)
bind_cols(test1, test3)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。