美文网首页
R :apply()函数的学习

R :apply()函数的学习

作者: jiarf | 来源:发表于2022-02-23 11:33 被阅读0次

apply() function

apply() takes Data frame or matrix as an input and gives output in vector, list or array.

apply(X, MARGIN, FUN)
Here:
-x: an array or matrix
-MARGIN:  take a value or range between 1 and 2 to define where to apply the function:
-MARGIN=1`: the manipulation is performed on rows
-MARGIN=2`: the manipulation is performed on columns
-MARGIN=c(1,2)` the manipulation is performed on rows and columns
-FUN: tells which function to apply. Built functions like mean, median, sum, min, max and even user-defined functions can be applied>
m1 <- matrix(C<-(1:10),nrow=5, ncol=6)
m1
a_m1 <- apply(m1, 2, sum)
a_m1
image.png

lapply() function

Lapply in R takes list, vector or data frame as input and gives output in list.

lapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element of x
movies <- c("SPYDERMAN","BATMAN","VERTIGO","CHINATOWN")
movies_lower <-lapply(movies, tolower)
str(movies_lower)
output:
## List of 4
## $:chr"spyderman"
## $:chr"batman"
## $:chr"vertigo"
## $:chr"chinatown"

We can use unlist() to convert the list into a vector.

movies_lower <-unlist(lapply(movies,tolower))
str(movies_lower)
output
##  chr [1:4] "spyderman" "batman" "vertigo" "chinatown"

sapply() function

sapply() function takes list, vector or data frame as input and gives output in vector or matrix. Sapply function in R does the same job as lapply() function but returns a vector.

sapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element of x
dt <- cars
lmn_cars <- lapply(dt, min)
smn_cars <- sapply(dt, min)
lmn_cars
## $speed
## [1] 4
## $dist
## [1] 2
smn_cars
## speed  dist 
##     4     2
avg <- function(x) {  
  ( min(x) + max(x) ) / 2}
fcars <- sapply(dt, avg)
fcars
## speed  dist
##  14.5  61.0

summarize

image.png

Slice vector 这俩函数的应用

We can use lapply() or sapply() interchangeable to slice a data frame. We create a function, below_average(), that takes a vector of numerical values and returns a vector that only contains the values that are strictly above the average. We compare both results with the identical() function.

below_ave <- function(x) {  
    ave <- mean(x) 
    return(x[x > ave])
}
dt_s<- sapply(dt, below_ave)
dt_l<- lapply(dt, below_ave)
identical(dt_s, dt_l)
## [1] TRUE

tapply()

we can compute the median of the length for each species. Tapply in R is a quick way to perform this computation.

data(iris)
tapply(iris$Sepal.Width, iris$Species, median)
##     setosa versicolor  virginica 
##        3.4        2.8        3.0

相关文章

网友评论

      本文标题:R :apply()函数的学习

      本文链接:https://www.haomeiwen.com/subject/djcolrtx.html