- R Data Visualization Cookbook
- Atmajitsinh Gohil
- 334字
- 2021-08-06 19:21:08
The apply, lapply, sapply, and tapply functions
R has some very handy functions such as apply
, sapply
, tapply
, and mapply
, that can be used to reduce the task of writing complicated statements. Also, using them makes our code look cleaner. The apply()
function is similar to writing a loop statement.
The lapply()
function is very similar to the apply()
function but can be used on lists; this will return a list. The sapply()
function is very similar to lapply()
but returns a vector and not a list.
How to do it…
The apply()
function can be used as follows:
mat= matrix(1:25, 5,5) apply(mat,1,sd)
The lapply()
function can be used in the following way:
j = list(x = 1:4, b = rnorm(100,1,2)) lapply(j,mean)
The tapply()
function is useful when we have broken a vector into factors, groups, or categories:
tapply(mtcars$mpg,mtcars$gear,mean)
How it works…
The first argument in the apply()
function is the data. The second argument takes two values: 1 and 2; if we state 1, R will perform a row-wise computation; if we mention 2, R will perform a column-wise computation. The third argument is the function. We would like to calculate the standard deviation of each row in R; hence we use the sd
function as the third argument. Note that we can define our own function and replace it with the sd
function.
With regard to the lapply()
function, we have defined J as a list and would like to calculate the mean. The first argument in the lapply()
function is the data and the second argument is the function used to process the data.
The first argument in the tapply()
function is the data; in our case it is mpg
. The second argument is the factor or the grouping; in this case it would be gears
. The last argument is the function used to process the data. We would like to calculate the mean of mpg
for each unique gear (3, 4, and 5 gears) in the mtcars data.
- Kibana Essentials
- 數字媒體應用教程
- Practical Internet of Things Security
- Linux核心技術從小白到大牛
- UI智能化與前端智能化:工程技術、實現方法與編程思想
- 區塊鏈:以太坊DApp開發實戰
- Mastering Yii
- 21天學通C++(第6版)
- JS全書:JavaScript Web前端開發指南
- 從學徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Learning Data Mining with R
- Learning Unreal Engine Android Game Development
- uni-app跨平臺開發與應用從入門到實踐
- Java程序設計與項目案例教程