首页 > > > tidyfst行操作
作者:黄天元,复旦大学博士在读,热爱数据科学与开源工具(R),致力于利用数据科学迅速积累行业经验优势和科学知识发现,涉猎内容包括但不限于信息计量、机器学习、数据可视化、应用统计建模、知识图谱等,著有《R语言数据高效处理指南》。知乎专栏:R语言数据挖掘。邮箱:huang.tian-yuan@qq.com.欢迎合作交流。
对标dplyr 1.0.0的行操作系列dplyr 1.0.0: working within rows - Tidyverse,我们继续来进行演示。
library(pacman)
p_load(tidyfst,tibble)
df <- tibble(
student_id = 1:4,
test1 = 10:13,
test2 = 20:23,
test3 = 30:33,
test4 = 40:43
)
df
#> # A tibble: 4 x 5
#> student_id test1 test2 test3 test4
#> <int> <int> <int> <int> <int>
#> 1 1 10 20 30 40
#> 2 2 11 21 31 41
#> 3 3 12 22 32 42
#> 4 4 13 23 33 43
df %>% mutate_dt(avg = mean(c(test1, test2, test3, test4)))
#> student_id test1 test2 test3 test4 avg
#> <int> <int> <int> <int> <int> <num>
#> 1: 1 10 20 30 40 26.5
#> 2: 2 11 21 31 41 26.5
#> 3: 3 12 22 32 42 26.5
#> 4: 4 13 23 33 43 26.5
df %>%
rowwise_dt(
mutate_dt(avg = mean(c(test1, test2, test3, test4)))
)
#> student_id test1 test2 test3 test4 avg
#> <int> <int> <int> <int> <int> <num>
#> 1: 1 10 20 30 40 25
#> 2: 2 11 21 31 41 26
#> 3: 3 12 22 32 42 27
#> 4: 4 13 23 33 43 28
df %>%
rowwise_dt(
mutate_dt(avg = (test1 + test2 + test3 + test4) / 4)
)
#> student_id test1 test2 test3 test4 avg
#> <int> <int> <int> <int> <int> <num>
#> 1: 1 10 20 30 40 25
#> 2: 2 11 21 31 41 26
#> 3: 3 12 22 32 42 27
#> 4: 4 13 23 33 43 28
df %>% mutate_dt(
min = pmin(test1, test2, test3, test4),
max = pmax(test1, test2, test3, test4),
string = paste(test1, test2, test3, test4, sep = "-")
)
#> student_id test1 test2 test3 test4 min max string
#> <int> <int> <int> <int> <int> <int> <int> <char>
#> 1: 1 10 20 30 40 10 40 10-20-30-40
#> 2: 2 11 21 31 41 11 41 11-21-31-41
#> 3: 3 12 22 32 42 12 42 12-22-32-42
#> 4: 4 13 23 33 43 13 43 13-23-33-43
## List-Columns
df <- tribble(
~id, ~ n, ~ min, ~ max,
1, 3, 0, 1,
2, 2, 10, 100,
3, 2, 100, 1000,
)
df %>%
rowwise_dt(
mutate_dt(data = list(runif(n, min, max)))
)
#> id n min max data
#> <num> <num> <num> <num> <list>
#> 1: 1 3 0 1 0.7871929,0.6681611,0.9175929
#> 2: 2 2 10 100 87.57919,97.41915
#> 3: 3 2 100 1000 656.4811,646.9626
df %>%
rowwise_dt(
summarise_dt(id,x = runif(n, min, max))
)
#> id x
#> <num> <num>
#> 1: 1 0.7491521
#> 2: 1 0.3214210
#> 3: 1 0.7622020
#> 4: 2 10.4449017
#> 5: 2 52.0840039
#> 6: 3 311.9701354
#> 7: 3 984.2869036
## Group-wise models
by_cyl <- mtcars %>% nest_dt(cyl)
by_cyl
#> cyl ndt
#> <num> <list>
#> 1: 6 <data.table>
#> 2: 4 <data.table>
#> 3: 8 <data.table>
by_cyl <- by_cyl %>%
mutate_dt(model = lapply(ndt,function(df) lm(mpg ~ wt, data = df)))
by_cyl
#> cyl ndt model
#> <num> <list> <list>
#> 1: 6 <data.table> <lm>
#> 2: 4 <data.table> <lm>
#> 3: 8 <data.table> <lm>
by_cyl %>%
mutate_dt(glance = lapply(model,broom::glance)) %>%
unnest_dt(glance)
#> cyl r.squared adj.r.squared sigma statistic p.value df logLik
#> <num> <num> <num> <num> <num> <num> <int> <num>
#> 1: 6 0.4645102 0.3574122 1.165202 4.337245 0.09175766 2 -9.82518
#> 2: 4 0.5086326 0.4540362 3.332283 9.316233 0.01374278 2 -27.74487
#> 3: 8 0.4229655 0.3748793 2.024091 8.795985 0.01179281 2 -28.65778
#> AIC BIC deviance df.residual
#> <num> <num> <num> <int>
#> 1: 25.65036 25.48809 6.788481 5
#> 2: 61.48974 62.68342 99.936983 9
#> 3: 63.31555 65.23272 49.163336 12
by_cyl %>%
mutate_dt(tidy = lapply(model,broom::tidy)) %>%
unnest_dt(tidy)
#> cyl term estimate std.error statistic p.value
#> <num> <char> <num> <num> <num> <num>
#> 1: 6 (Intercept) 28.408845 4.1843688 6.789278 1.054844e-03
#> 2: 6 wt -2.780106 1.3349173 -2.082605 9.175766e-02
#> 3: 4 (Intercept) 39.571196 4.3465820 9.103980 7.771511e-06
#> 4: 4 wt -5.647025 1.8501185 -3.052251 1.374278e-02
#> 5: 8 (Intercept) 23.868029 3.0054619 7.941551 4.052705e-06
#> 6: 8 wt -2.192438 0.7392393 -2.965803 1.179281e-02
演示中,我们可以发现,我们在tidyfst中用rowwise_dt来囊括需要按行操作的部分,在其中为所欲为。对于列表列(List-column),可以灵活使用nest系列函数。目前还是要采用lapply来对列表列进行操作,这样有助于大家直接去了解R语言的基本操作结构。
原文作者:;如有侵权,请联系管理员删除。