Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
College aantekeningen

Class notes 1451 (1432) Student Study Guide and Solutions Manual t/a Organic Chemistry, 1st Edition Binder Ready Version COmp Set

Beoordeling
-
Verkocht
-
Pagina's
5
Geüpload op
15-04-2025
Geschreven in
2024/2025

It's about all based computers and ai technology subject

Instelling
Vak

Voorbeeld van de inhoud

Loop Functions

7.1 Looping on the Command Line
Writing for and while loops is useful when programming but not particularly easy when working
interactively on the command line. Multi-line expressions with curly braces are just not that easy to
sort through when working on the command line. R has some functions which implement looping
in a compact form to make your life easier.

 lapply(): Loop over a list and evaluate a function on each element
 sapply(): Same as lapply but try to simplify the result
 apply(): Apply a function over the margins of an array
 tapply(): Apply a function over subsets of a vector
 mapply(): Multivariate version of lapply
An auxiliary function split is also useful, particularly in conjunction with lapply.
In R, the apply() function and its variants (lapply(), sapply(), tapply(), etc.) are used to apply a function over data
structures such as vectors, matrices, and data frames. These functions help simplify loops and make your code
more concise and efficient.

1. apply()
The apply() function is used to apply a function to the rows or columns of a matrix or 2D array.

Syntax:
apply(X, MARGIN, FUN, ...)
X: The matrix or array.
MARGIN: An integer value. Use 1 to apply the function to rows and 2 to apply it to columns.
FUN: The function to apply.

Example: Sum the rows and columns of a matrix
matrix_data <- matrix(1:9, nrow = 3)

# Sum each row
row_sum <- apply(matrix_data, 1, sum)
print(row_sum)
# Output: 6 15 24 (sum of each row)

# Sum each column
col_sum <- apply(matrix_data, 2, sum)
print(col_sum)
# Output: 12 15 18 (sum of each column)

2. lapply()
The lapply() function applies a function to each element of a list or vector and returns a list.

Syntax:
lapply(X, FUN, ...)

, X: The list or vector.
FUN: The function to apply.

Example: Applying a function to a list
my_list <- list(a = 1:5, b = 6:10)

# Apply sum to each element in the list
result <- lapply(my_list, sum)
print(result)
# Output: $a 15, $b 40 (sum of each vector in the list)

3. sapply()
The sapply() function is similar to lapply(), but it simplifies the output. It attempts to return a vector or matrix
when possible.

Syntax:
sapply(X, FUN, ...)

X: The list or vector.
FUN: The function to apply.

Example: Simplifying results to a vector
my_list <- list(a = 1:5, b = 6:10)

# Apply sum to each element and simplify the result
result <- sapply(my_list, sum)
print(result)
# Output: a 15, b 40 (vector output)

4. tapply()
The tapply() function applies a function over subsets of a vector, defined by a factor (grouping variable).

Syntax:
tapply(X, INDEX, FUN, ...)
X: The vector to apply the function to.
INDEX: A factor or list of factors that define the groups.
FUN: The function to apply.

Example: Calculate the mean of values by group

data <- c(10, 20, 30, 40, 50)
group <- factor(c('A', 'A', 'B', 'B', 'A'))

# Apply mean to each group
result <- tapply(data, group, mean)
print(result)
# Output: A 26.66667, B 35 (mean for each group)

5. mapply()

Gekoppeld boek

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
15 april 2025
Aantal pagina's
5
Geschreven in
2024/2025
Type
College aantekeningen
Docent(en)
Pavan
Bevat
Alle colleges

Onderwerpen

$8.49
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
monishchowdary999

Ook beschikbaar in voordeelbundel

Maak kennis met de verkoper

Seller avatar
monishchowdary999 Surya university
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
1 jaar
Aantal volgers
0
Documenten
5
Laatst verkocht
-

0.0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen