lapply()R 語言中的函數用於在元素列表上應用函數。
用法: lapply(list, func)
參數:
list:元素列表
func:要應用的操作
範例1:
# R program to illustrate 
# lapply() function 
    
# Creating a matrix 
A = matrix(1:9, 3, 3) 
    
# Creating another matrix 
B = matrix(10:18, 3, 3)  
    
# Creating a list 
myList = list(A, B) 
    
# applying lapply() 
determinant = lapply(myList, det) 
print(determinant) 輸出:
[[1]] [1] 0 [[2]] [1] 5.329071e-15
範例2:
# R program to illustrate 
# lapply() function 
    
# Creating a matrix 
A = matrix(1:9, 3, 3) 
    
# Creating another matrix 
B = matrix(10:18, 3, 3)  
    
# Creating a list 
myList = list(A, B) 
    
# applying lapply() 
sum = lapply(myList, sum) 
print(sum) 輸出:
     [, 1] [, 2] [, 3]
[1, ]    1    4    7
[2, ]    2    5    8
[3, ]    3    6    9
[1]  28  80 162
[1]   6 120 504
相關用法
- R語言 tapply()用法及代碼示例
- R語言 apply()用法及代碼示例
- R語言 rapply()用法及代碼示例
- R語言 sapply()用法及代碼示例
- R語言 qf()用法及代碼示例
- R語言 qweibull()用法及代碼示例
- R語言 qtukey()用法及代碼示例
- R語言 qsignrank()用法及代碼示例
- R語言 qwilcox()用法及代碼示例
- R語言 qunif()用法及代碼示例
- R語言 ptukey()用法及代碼示例
- R語言 dsignrank()用法及代碼示例
- R語言 psignrank()用法及代碼示例
- R語言 dwilcox()用法及代碼示例
- R語言 pwilcox()用法及代碼示例
- R語言 as.list()用法及代碼示例
- R語言 is.list()用法及代碼示例
- R語言 args()用法及代碼示例
- R語言 search()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Apply a Function over a List of elements in R Programming – lapply() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
