YouTip LogoYouTip

R Func Mapply

# R mapply() Function - Multivariate Application [![Image 3: R Language Examples](https://example.com/images/up.gif) R Language Examples](https://example.com/r/r-examples.html) R mapply() function is a multivariate version of sapply(), which can apply a function to multiple lists/vectors simultaneously. When you need to process multiple arguments element by element, mapply() can replace nested loops. The syntax of mapply() function is as follows: mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE) **Parameter Description:** * **FUN** The function to apply. * **...** Multiple parameters to pass to FUN (list or vector). * **MoreArgs** A list of fixed parameters (not involved in element-wise iteration). ## Example # Generate sequence using mapply result <-mapply(seq, from =c(1, 10, 100), to =c(3, 15, 105)) print("Generated sequence:") print(result) # Process multiple parameters element by element names<-c("Zhang San", "Li Si", "Wang Wu") math <-c(88, 92, 76) english <-c(90, 88, 82) # Calculate total score for each student total <-mapply(function(n, m, e){ paste(n, "Total:", m + e) }, names, math, english) print("Each person's total:") print(total) Executing the above code outputs the following result: "Generated sequence:"[] 1 2 3[] 10 11 12 13 14 15[] 100 101 102 103 104 105 "Each person's total:" "Zhang San Total: 178" "Li Si Total: 180" "Wang Wu Total: 158" [![Image 4: R Language Examples](https://example.com/images/up.gif) R Language Examples](https://example.com/r/r-examples.html)
← R Func MergeR Func Lm β†’