学习小组Day6-杨咩咩

  • 学习R包

    • 安装和加载R包

      • 1.镜像设置

      • 2.安装

        • install.packages(“包”)

        • BiocManager::install(“包”)

      • 3.加载

        • library(包)

        • require(包)

      • 实操

        • test <- iris[c(1:2,51:52,101:102),]
          image
    • dplyr五个基本函数

      • 1.mutate(),新增列

        • mutate(test, new = Sepal.Length * Sepal.Width)
          image
      • 2.select(),按列筛选

        • (1)按列号筛选

          • select(test,1)

          • select(test,c(1,5))

          • select(test,Sepal.Length)

          • image
        • (2)按列名筛选

        • select(test, Petal.Length, Petal.Width)

        • vars <- c("Petal.Length", "Petal.Width")
          select(test, one_of(vars))

        • image
      • 3.filter()筛选行

        • filter(test, Species == "setosa")

        • filter(test, Species == "setosa"&Sepal.Length > 5 )

        • filter(test, Species %in% c("setosa","versicolor"))

        • image
      • 4.arrange(),按某1列或某几列对整个表格进行排序

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

        • arrange(test, desc(Sepal.Length))

        • image
      • 5.summarise():汇总

        • 对数据进行汇总操作,结合group_by使用实用性强

        • summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 计算Sepal.Length的平均值和标准差

        • group_by(test, Species)

        • summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))

        • image
    • dplyr两个实用技能

      • 1:管道操作 %>% (cmd/ctr + shift + M)

        • image
      • 2:count统计某列的unique值

        • count(test,Species)

        • image
    • dplyr处理关系数据

      • 两表链接

        • 方法一:
          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
        • image
      • 1.內连inner_join,取交集

        • inner_join(test1, test2, by = "x")

          • image
      • 2.左连left_join

        • left_join(test1, test2, by = 'x')

        • left_join(test2, test1, by = 'x')

        • image
      • 3.全连full_join

        • full_join( test1, test2, by = 'x')

        • full_join( test1, test2, by = 'y')报错原因是test1:x z,test2:x y
          image
      • 4.半连接:返回能够与y表匹配的x表所有记录semi_join

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

          • image
      • 5.反连接:返回无法与y表匹配的x表的所记录anti_join

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

          • image
      • 6.简单合并

        • 在相当于base包里的cbind()函数和rbind()函数;注意,bind_rows()函数需要两个表格列数相同,而bind_cols()函数则需要两个数据框有相同的行数

        • 代码
          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
          bind_rows(test1, test2)
          bind_cols(test1, test3)

        • image

          思维导图:
          学习R包.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容