Day6-dplyr-S

作者: Sun506 | 来源:发表于2022-04-20 12:11 被阅读0次

安装R包

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) 
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") 
install.packages("dplyr")
library(dplyr)

读取iris数据

test <- iris[c(1:2,51:52,101:102),]
head(test)#有5列

1.新增列

  • 新增一列:方法一
    mutate(test, new = Sepal.Length * Sepal.Width)
  • 新增一列:方法二
    test$new<-test$Sepal.Length*test$Sepal.Width
    head(test)
  • 新增一列:方法三(transform)
test <- iris[c(1:2,51:52,101:102),]
transform(test,new=Sepal.Length * Sepal.Width)
  • 新增一列:方法四(transform)
attach(test)
test$new<-Sepal.Length*Sepal.Width 

2.按列筛选

  • 方法一:dplyr::select
#按照下标或者名字
select(test,1)
select(test,Sepal.Length)
select(test,c(1,5))
select(test, Petal.Length, Petal.Width)
#筛选并且重命名
test %>% select(Length=Petal.Length,Width=Petal.Width)#可以用 %>% 
select(test,Length=Petal.Length,Width=Petal.Width)
select(test,Length=1,Width=2)
#选择多列时可以用这个方法
vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
#提取Sepal 开头的列
select(test,starts_with("Sepal"))
#提取h结尾的列
select(test,ends_with("h"))
#提取包含.的列
select(test,contains("."))
#提取匹配数字的列:
test %>% select_if(is.numeric)
#匹配为因子的列:
test %>% select_if(is.factor)
#注意MASS包也有select,此时可以
library(tidyverse)
select = dplyr::select
  • 方法二
test[,c(1,5)]
t=test[,1]#此方法提取单列时会变为Values
class(t)
  • 方法三
    test$Sepal.Length

3.筛选行

test <- iris[c(1:2,51:52,101:102),]

  • 方法一:dplyr::filter
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )#两个条件都符合;
filter(test, Species %in% c("setosa","versicolor"))
#基于逻辑筛选
filter(condition1,condition2)#两个条件都符合;
filter(condition1,!condition2)#条件1是TRUE,条件2是FALSE;
filter(condition1 | condition2)#两个条件符合其中一个即可;
filter(xor(condition1, condition2)#只有一个条件符合!两个都符合不可以。注意使用
  • 方法二
    test[1,]

4.排序

  • 方法一:dplyr::arrange
arrange(test, Sepal.Length)#默认从小到大排序
arrange(test, desc(Sepal.Length))#用desc从大到小
  • 方法二:order返回的是下标排名
test[order(test$Sepal.Length),] 
test[order(test$Sepal.Length,test$Sepal.Width),] #先按照Sepal.Length排序,再Sepal.Width排序
  • 方法三:sort返回的是具体数值
sort(test$Sepal.Length)
test[sort(test$Sepal.Length),]#这样得不到想要的结果

5.summarise():汇总

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

6.管道操作 %>%

test %>% 
  group_by(Species) %>% 
  summarise(mean(Sepal.Length), sd(Sepal.Length))
#count统计某列的unique值
count(test,Species)

7.连接两个表

options(stringsAsFactors = F)
test1 <- data.frame(x = c('b','e','f','x'), 
                    z = c("A","B","C",'D'),
                    stringsAsFactors = F)
test1
test2 <- data.frame(x = c('a','b','c','d','e','f'), 
                    y = c(1,2,3,4,5,6),
                    stringsAsFactors = F)
test2
#1.內连inner_join,取交集
inner_join(test1,test2,by = "x")
#2.左连left_join
left_join(test1, test2, by = 'x')
left_join(test2, test1, by = 'x')
#3.全连full_join
full_join( test1, test2, by = 'x')
#4.半连接:返回能够与y表匹配的x表所有记录semi_join
semi_join(x = test1, y = test2, by = 'x')
#5.反连接:返回无法与y表匹配的x表的所记录anti_join
anti_join(x = test2, y = test1, by = 'x')

8.cbind():合并列,需要行相同。rbind():合并行,需要列相同

test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
test1
test2 <- data.frame(x = c(5,6), y = c(50,60))
test2
test3 <- data.frame(z = c(100,200,300,400))
test3
rbind(test1, test2)
cbind(test1, test3)

相关文章

  • Day6-dplyr-S

    安装R包 读取iris数据 1.新增列 新增一列:方法一mutate(test, new = Sepal.Leng...

网友评论

    本文标题:Day6-dplyr-S

    本文链接:https://www.haomeiwen.com/subject/lekvertx.html