当前位置:网站首页>tidyverse based on data.table?
tidyverse based on data.table?
2022-08-03 20:42:00 【A Yue 1229】
获取更多R语言知识,请关注公众号:医学和生信笔记
“医学和生信笔记,专注R语言在临床医学中的使用,R语言数据分析和可视化.主要分享R语言做医学统计学、meta分析、网络药理学、临床预测模型、机器学习、生物信息学等.
tidyverse作为RThe Swiss Army Knife in Linguistic Data Analysis,非常好用,A small downside is that it is slow,data.table速度快,So their team developed it againdtplyr,加快运行速度.
But today is another one,基于data.table的tidyverse:tidytable.
使用起来非常简单,Just add one after the original function.即可!!!
Below is a simple speed comparison of a common operation,It can be seen that the speed has been greatly improved~

安装
# 经典2选1
install.packages("tidytable")
# install.packages("devtools")
devtools::install_github("markfairbanks/tidytable")
一般使用
Just add one after the function.就可以了!!
library(tidytable)
## Warning: package 'tidytable' was built under R version 4.2.1
##
## Attaching package: 'tidytable'
## The following object is masked from 'package:stats':
##
## dt
df <- data.table(x = 1:3, y = 4:6, z = c("a", "a", "b"))
df %>%
select.(x, y, z) %>%
filter.(x < 4, y > 1) %>%
arrange.(x, y) %>%
mutate.(double_x = x * 2,
x_plus_y = x + y)
## # A tidytable: 3 × 5
## x y z double_x x_plus_y
## <int> <int> <chr> <dbl> <int>
## 1 1 4 a 2 5
## 2 2 5 a 4 7
## 3 3 6 b 6 9
分组汇总
和group_by()稍有不同,这里需要使用.by = 进行分组汇总.
df %>%
summarize.(avg_x = mean(x),
count = n(),
.by = z) # Grouping summary forms are different
## # A tidytable: 2 × 3
## z avg_x count
## <chr> <dbl> <int>
## 1 a 1.5 2
## 2 b 3 1
每次都要调用:
df <- data.table(x = c("a", "a", "a", "b", "b"))
df %>%
slice.(1:2, .by = x) %>% # .by
mutate.(group_row_num = row_number(), .by = x) # .by
## # A tidytable: 4 × 2
## x group_row_num
## <chr> <int>
## 1 a 1
## 2 a 2
## 3 b 1
## 4 b 2
支持tidyselect
常见的everything(), starts_with(), ends_with(), any_of(), where()等都是支持的.
df <- data.table(
a = 1:3,
b1 = 4:6,
b2 = 7:9,
c = c("a", "a", "b")
)
df %>%
select.(a, starts_with("b"))
## # A tidytable: 3 × 3
## a b1 b2
## <int> <int> <int>
## 1 1 4 7
## 2 2 5 8
## 3 3 6 9
df %>%
select.(-a, -starts_with("b"))
## # A tidytable: 3 × 1
## c
## <chr>
## 1 a
## 2 a
## 3 b
可以和.by连用:
df <- data.table(
a = 1:3,
b = c("a", "a", "b"),
c = c("a", "a", "b")
)
df %>%
summarize.(avg_a = mean(a),
.by = where(is.character))
## # A tidytable: 2 × 3
## b c avg_a
## <chr> <chr> <dbl>
## 1 a a 1.5
## 2 b b 3
支持data.table语法
借助dt()函数实现对data.table语法的支持.
df <- data.table(x = 1:3, y = 4:6, z = c("a", "a", "b"))
df %>%
dt(, .(x, y, z)) %>%
dt(x < 4 & y > 1) %>%
dt(order(x, y)) %>%
dt(, double_x := x * 2) %>%
dt(, .(avg_x = mean(x)), by = z)
## # A tidytable: 2 × 2
## z avg_x
## <chr> <dbl>
## 1 a 1.5
## 2 b 3
基本上tidyverseAll functions related to data analysis can be used,A detailed list of supported functions can be found here这里[1]找到.
获取更多R语言知识,请关注公众号:医学和生信笔记
“医学和生信笔记,专注R语言在临床医学中的使用,R语言数据分析和可视化.主要分享R语言做医学统计学、meta分析、网络药理学、临床预测模型、机器学习、生物信息学等.
参考资料
tidytable支持的函数: https://markfairbanks.github.io/tidytable/reference/index.html
边栏推荐
猜你喜欢

肝完 Alibaba 这份面试通关宝典,我成功拿下今年第 15 个 Offer

化算力为战力:宁夏中卫的数字化转型启示录
[email protected] 610/[email protected] 594/Alexa 56"/>染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56

Cesium 修改鼠标样式

error: C1083: 无法打开包括文件: “QString”: No such error: ‘QDir‘ file not found

tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)

倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即

李沐动手学深度学习V2-BERT微调和代码实现

史兴国对谈于佳宁:从经济模式到落地应用,Web3的中国之路怎么走?

双线性插值公式推导及Matlab实现
随机推荐
wordpress建立数据库连接时出错
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
双线性插值公式推导及Matlab实现
LeetCode 622. 设计循环队列
leetcode 16. 数值的整数次方(快速幂+递归/迭代)
火了十几年的零信任,为啥还不能落地
在树莓派上搭建属于自己的网页(4)
Cesium 修改鼠标样式
收藏-即时通讯(IM)开源项目OpenIM-功能手册
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
回忆三年浮沉
Leetcode sword refers to Offer 15. 1 in the binary number
nvm的使用 nodejs版本管理,解决用户名是汉字的问题
若依集成easyexcel实现excel表格增强
LeetCode 1374. 生成每种字符都是奇数个的字符串
Lecture topics and guest blockbuster, TDengine developers conference to promote data technology "broken"
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
收藏-即时通讯(IM)开源项目OpenIM-功能手册
敏捷交付的工程效能治理