Skip to Content
高效R语言编程
book

高效R语言编程

by Colin Gillespie, Robin Lovelace
August 2018
Intermediate to advanced
227 pages
4h 16m
Chinese
China Electric Power Press Ltd.
Content preview from 高效R语言编程
66
3
我们使用常见 sort(m) 的方式来排序 m,得到了标准字母排序结果;将
December 放在第一个。技术上正确的,但毫无用处。我们可以使用因子指定
合理排序,从而解决问题:
# month.name
包含了
12
个月
fac_m = factor(m, levels = month.name)
sort(fac_m)
#> [1] January March December
#> 12 Levels: January February March April May June July August ... December
练习
1.
相比字符,因子稍微节约空间,创建一个字符向量与相应因子,使用
pryr::object_size() 计算各自空间占用情况。
Apply
函数族
apply
函数可看作循环的代替。通常想法是将一个函数分别应用(或者映射)
于一个对象的每个元素。例如,你可以将一个函数应用到矩阵的每行或者每
列上。
3-2
给出了这些函数以及相应说明。通常所有的
apply
函数具有相
似特征:
每个函数至少有两个参数:对象与另一函数。函数作为参数传入。
每个
apply
函数有
+...+
点参数,用于将参数传递给
FUN
参数中的函数。
例如,
sapply(list((1:3)^2, 2:7), mean, trim = 0.4)
trip
参数传递给
mean
函数,并应用到列表中每个向量。
尽可能地使用
apply
函数,这样会写出简短明了、地道的
R
语言代码。本节
我们讲解三个主要的 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据科学之编程技术:使用R进行数据清理、分析与可视化

数据科学之编程技术:使用R进行数据清理、分析与可视化

迈克尔 弗里曼, 乔尔 罗斯
R数据科学

R数据科学

Hadley Wickham, Garrett Grolemund

Publisher Resources

ISBN: 9787519820855