logo

Örneklerle R'de Apply(), lapply(), sapply(), tapply() İşlevi

R'deki application() İşlevi:

Bir işlevin bir matrise, diziye veya listeye uygulanması, R'de application() işlevi kullanılarak yapılır. Bu yapılarda tutulan veriler üzerinde işlem yapmak için son derece yararlı bir fonksiyondur.

Apply() fonksiyonunun sözdizimi aşağıdadır:

uygula(X, KENAR, EĞLENCE, ...)

Burada;

  • Fonksiyonun uygulanacağı marj, MARGIN parametresi ile belirtilir. Bu değerlerin 1, 2 gibi bir vektörü veya satırlar ve sütunlar için her ikisi de olabilir.
  • X, üzerinde çalışılan matris, dizi veya listedir.
  • EĞLENCE arzu edilen sonuçtur.
  • Fonksiyonun isteğe bağlı argümanları '...' içinde bulunur.

Aşağıda application()'un birkaç örneği verilmiştir:

Örnek 1: Bir Matrise Satırlara Göre Bir Fonksiyon Uygulamak

tamsayılara dize

Diyelim ki aşağıda gösterilen matrisimiz var:

 m <- matrix(1:12, nrow="3)" < pre> <p>Apply() can be used to determine the mean of each row:</p> <pre> apply(m, 1, mean) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p> <strong>Example 2: Applying a Function to a Matrix by Columns</strong> </p> <p>Apply() can be used to determine the standard deviation for each column of a matrix m:</p> <pre> apply(m, 2, sd) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-2.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p> <strong>Example 3: Applying a Function to a List</strong> </p> <p>Consider a list of vectors:</p> <pre> lst <- list(a="1:5," b="6:10," c="11:15)" < pre> <p>Apply() can be used to determine the sum of each vector:</p> <pre> apply(lst, 1, sum) </pre> <p> <strong>Output:</strong> </p> <p>The sum of each vector in the list will be reported in this way:</p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-3.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <h4>Note: Due to the fact that we want to apply the function to each vector in the list, we use 1 as the value of MARGIN in this instance (i.e., along the first margin).</h4> <p> <strong>Example 4: Applying a User-Defined Function to a Matrix by Rows</strong> </p> <p>Let&apos;s say we have the matrix shown below:</p> <pre> m <- matrix(1:12, nrow="3)" < pre> <p>Apply() allows us to apply a user-defined function to each row:</p> <pre> f <- function(x) sum(x^2) apply(m, 1, f) < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-4.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The f() function computes the sum of the squares for each row in this example.</p> <p>All things considered, apply() is a fairly strong function that can be applied in a number of different ways to manipulate matrices, arrays, and lists.</p> <h2>lapply() Function in R:</h2> <p>A useful feature of the R programming language is the lapply() function, which enables you to apply a specific function to each element in a list or vector. A list with the same length as the input is produced as the output, with each entry representing the outcome of applying the specified function to its corresponding input element.</p> <p>The lapply() function&apos;s underlying syntax is as follows:</p> <pre> lapply(X, FUN, ...) </pre> <p>In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. You can add more parameters to the FUN function by passing them as the &apos;... argument.&apos;</p> <h3>Some Examples of lapply():</h3> <p> <strong>Example 1:</strong> </p> <p>Let&apos;s look at an illustration of how to employ R&apos;s lapply() function.</p> <p>Let&apos;s say we want to determine the square root of each number in a list of numbers. Each element of the list can have the sqrt() function applied to it using the lapply() method. Here is the key:</p> <p> <strong>Code:</strong> </p> <pre> # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using lapply() result <- lapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-5.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sqrt() function was applied to each element of the list by the lapply() function, which then returned a list that had the same length as the input and contained elements that were the square roots of the corresponding input elements.</p> <p> <strong>Example 2:</strong> </p> <p>The lapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using lapply() result <- lapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-6.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the add 10() function was applied to each element of the list by the lapply() function, which resulted in a list that had the same length as the input and contained each element as the result of adding 10 to its corresponding input element.</p> <p>It&apos;s significant to remember that, regardless of the input, the lapply() method always produces a list. For instance, the result of using the lapply() method on a vector is still a list: Here is an example to show this.</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using lapply() result <- lapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-7.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sqrt() function was used after the lapply() method transformed the input vector into a list.</p> <h2>sapply() Function in R:</h2> <p>A helpful feature of the R programming language is the sapply() function, which may be used to streamline the code for applying a specified function to each element of a list or vector. A vector or matrix with the same length or dimensions as the input is produced as the output, with each element the outcome of applying the specified function to its corresponding input element.</p> <p>The sapply() function&apos;s syntax is as follows:</p> <pre> sapply(X, FUN, simplify = TRUE, ...) </pre> <p>In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. By default, the simplified parameter is set to TRUE, meaning that if the function&apos;s output is a vector or matrix, the result will also be a vector or matrix. To send more arguments to the FUN function, use the &apos;... argument.&apos;</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s sapply() function.</p> <p>Let&apos;s say we want to determine the square root of each number in a list of numbers. The sqrt() function can be applied to each element of the list using the sapply() method. Here is the key:</p> <p> <strong>Code:</strong> </p> <pre> # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using sapply() result <- sapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-8.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sapply() method took each element of the list and applied the sqrt() function to it. It then returned a vector with the same length as the input, each element of which was the square root of its corresponding input element.</p> <p>The sapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using sapply() result <- sapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-9.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sapply() function added 10 to each element of the list using the add 10() function, returning a vector with the same length as the input where each element was the result of the addition of 10.</p> <p>It&apos;s crucial to remember that the sapply() function also works with vectors. In this situation, a vector will still be the result:</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using sapply() result <- sapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-10.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The sapply() method in this situation treated the input vector as a list and applied the sqrt() function to each element, returning a vector with the same length as the input and each element being the square root of its corresponding input element.</p> <h2>tapply() Function in R:</h2> <p>For applying a specified function to subsets of a vector or array based on the values of another variable, the R language&apos;s tapply() function is a helpful tool. Depending on the function used, the output is a vector, array, or list.</p> <p>The tapply() function&apos;s fundamental syntax is as follows:</p> <p>tapply(X, INDEX, FUN, ...)</p> <p>Here, FUN is the function to be applied to each subset, X is the input vector or array, INDEX is a factor or list of factors denoting the subsets, and... are optional parameters that can be supplied to the function FUN.</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s tapply() function.</p> <p>Assume we have a vector of categories and a vector of numbers. The average of the values for each category is what we are looking for.</p> <p>The mean() function can be applied to each subset of the vector based on the category using the tapply() function. The code is here;</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers numbers <- c(23, 18, 25, 32, 20, 19, 27, 31, 22, 24) # create a vector of categories <- c('a', 'b', 'a', 'a') apply the mean() function to each subset based on category using tapply() result tapply(numbers, categories, mean) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-11.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the tapply() function used the mean() function to analyze each subset of the vector according to the category, returning a named vector with the results for each category.</p> <h2>Conclusion:</h2> <p>Simplified vectors or matrices with the same lengths or dimensions as the input can be obtained by applying a given function to each element of a list or vector using R&apos;s sapply() function.</p> <p>Developing code can save a lot of time and effort, especially when working with large datasets. Moreover, the sapply() method is a flexible function that may be used with lists, vectors, and built-in and user-defined functions.</p> <p>Everyone working with R and dealing with data processing and analysis should be familiar with this function.</p> <hr></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></->

Çıktı:

Örneklerle R'de Apply(), lapply(), sapply(), tapply() İşlevi

Örnek 2: Bir Matrise Sütunlarla Bir Fonksiyon Uygulamak

Apply(), m matrisinin her sütununun standart sapmasını belirlemek için kullanılabilir:

c++ dizesinde bul
 apply(m, 2, sd) 

Çıktı:

Örneklerle R'de Apply(), lapply(), sapply(), tapply() İşlevi

Örnek 3: Bir Listeye Fonksiyon Uygulamak

Vektörlerin bir listesini düşünün:

 lst <- list(a="1:5," b="6:10," c="11:15)" < pre> <p>Apply() can be used to determine the sum of each vector:</p> <pre> apply(lst, 1, sum) </pre> <p> <strong>Output:</strong> </p> <p>The sum of each vector in the list will be reported in this way:</p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-3.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <h4>Note: Due to the fact that we want to apply the function to each vector in the list, we use 1 as the value of MARGIN in this instance (i.e., along the first margin).</h4> <p> <strong>Example 4: Applying a User-Defined Function to a Matrix by Rows</strong> </p> <p>Let&apos;s say we have the matrix shown below:</p> <pre> m <- matrix(1:12, nrow="3)" < pre> <p>Apply() allows us to apply a user-defined function to each row:</p> <pre> f <- function(x) sum(x^2) apply(m, 1, f) < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-4.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The f() function computes the sum of the squares for each row in this example.</p> <p>All things considered, apply() is a fairly strong function that can be applied in a number of different ways to manipulate matrices, arrays, and lists.</p> <h2>lapply() Function in R:</h2> <p>A useful feature of the R programming language is the lapply() function, which enables you to apply a specific function to each element in a list or vector. A list with the same length as the input is produced as the output, with each entry representing the outcome of applying the specified function to its corresponding input element.</p> <p>The lapply() function&apos;s underlying syntax is as follows:</p> <pre> lapply(X, FUN, ...) </pre> <p>In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. You can add more parameters to the FUN function by passing them as the &apos;... argument.&apos;</p> <h3>Some Examples of lapply():</h3> <p> <strong>Example 1:</strong> </p> <p>Let&apos;s look at an illustration of how to employ R&apos;s lapply() function.</p> <p>Let&apos;s say we want to determine the square root of each number in a list of numbers. Each element of the list can have the sqrt() function applied to it using the lapply() method. Here is the key:</p> <p> <strong>Code:</strong> </p> <pre> # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using lapply() result <- lapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-5.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sqrt() function was applied to each element of the list by the lapply() function, which then returned a list that had the same length as the input and contained elements that were the square roots of the corresponding input elements.</p> <p> <strong>Example 2:</strong> </p> <p>The lapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using lapply() result <- lapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-6.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the add 10() function was applied to each element of the list by the lapply() function, which resulted in a list that had the same length as the input and contained each element as the result of adding 10 to its corresponding input element.</p> <p>It&apos;s significant to remember that, regardless of the input, the lapply() method always produces a list. For instance, the result of using the lapply() method on a vector is still a list: Here is an example to show this.</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using lapply() result <- lapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-7.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sqrt() function was used after the lapply() method transformed the input vector into a list.</p> <h2>sapply() Function in R:</h2> <p>A helpful feature of the R programming language is the sapply() function, which may be used to streamline the code for applying a specified function to each element of a list or vector. A vector or matrix with the same length or dimensions as the input is produced as the output, with each element the outcome of applying the specified function to its corresponding input element.</p> <p>The sapply() function&apos;s syntax is as follows:</p> <pre> sapply(X, FUN, simplify = TRUE, ...) </pre> <p>In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. By default, the simplified parameter is set to TRUE, meaning that if the function&apos;s output is a vector or matrix, the result will also be a vector or matrix. To send more arguments to the FUN function, use the &apos;... argument.&apos;</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s sapply() function.</p> <p>Let&apos;s say we want to determine the square root of each number in a list of numbers. The sqrt() function can be applied to each element of the list using the sapply() method. Here is the key:</p> <p> <strong>Code:</strong> </p> <pre> # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using sapply() result <- sapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-8.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sapply() method took each element of the list and applied the sqrt() function to it. It then returned a vector with the same length as the input, each element of which was the square root of its corresponding input element.</p> <p>The sapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using sapply() result <- sapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-9.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sapply() function added 10 to each element of the list using the add 10() function, returning a vector with the same length as the input where each element was the result of the addition of 10.</p> <p>It&apos;s crucial to remember that the sapply() function also works with vectors. In this situation, a vector will still be the result:</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using sapply() result <- sapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-10.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The sapply() method in this situation treated the input vector as a list and applied the sqrt() function to each element, returning a vector with the same length as the input and each element being the square root of its corresponding input element.</p> <h2>tapply() Function in R:</h2> <p>For applying a specified function to subsets of a vector or array based on the values of another variable, the R language&apos;s tapply() function is a helpful tool. Depending on the function used, the output is a vector, array, or list.</p> <p>The tapply() function&apos;s fundamental syntax is as follows:</p> <p>tapply(X, INDEX, FUN, ...)</p> <p>Here, FUN is the function to be applied to each subset, X is the input vector or array, INDEX is a factor or list of factors denoting the subsets, and... are optional parameters that can be supplied to the function FUN.</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s tapply() function.</p> <p>Assume we have a vector of categories and a vector of numbers. The average of the values for each category is what we are looking for.</p> <p>The mean() function can be applied to each subset of the vector based on the category using the tapply() function. The code is here;</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers numbers <- c(23, 18, 25, 32, 20, 19, 27, 31, 22, 24) # create a vector of categories <- c(\'a\', \'b\', \'a\', \'a\') apply the mean() function to each subset based on category using tapply() result tapply(numbers, categories, mean) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-11.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the tapply() function used the mean() function to analyze each subset of the vector according to the category, returning a named vector with the results for each category.</p> <h2>Conclusion:</h2> <p>Simplified vectors or matrices with the same lengths or dimensions as the input can be obtained by applying a given function to each element of a list or vector using R&apos;s sapply() function.</p> <p>Developing code can save a lot of time and effort, especially when working with large datasets. Moreover, the sapply() method is a flexible function that may be used with lists, vectors, and built-in and user-defined functions.</p> <p>Everyone working with R and dealing with data processing and analysis should be familiar with this function.</p> <hr></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></->

Çıktı:

sütun ekle Oracle'ı değiştir

Listedeki her vektörün toplamı şu şekilde rapor edilecektir:

Örneklerle R'de Apply(), lapply(), sapply(), tapply() İşlevi

Not: Fonksiyonu listedeki her vektöre uygulamak istediğimizden dolayı, bu örnekte MARGIN'in değeri olarak (yani ilk kenar boşluğu boyunca) 1'i kullanıyoruz.

Örnek 4: Kullanıcı Tanımlı Bir Fonksiyonun Bir Matrise Satırlara Göre Uygulanması

Diyelim ki aşağıda gösterilen matrisimiz var:

 m <- matrix(1:12, nrow="3)" < pre> <p>Apply() allows us to apply a user-defined function to each row:</p> <pre> f <- function(x) sum(x^2) apply(m, 1, f) < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-4.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The f() function computes the sum of the squares for each row in this example.</p> <p>All things considered, apply() is a fairly strong function that can be applied in a number of different ways to manipulate matrices, arrays, and lists.</p> <h2>lapply() Function in R:</h2> <p>A useful feature of the R programming language is the lapply() function, which enables you to apply a specific function to each element in a list or vector. A list with the same length as the input is produced as the output, with each entry representing the outcome of applying the specified function to its corresponding input element.</p> <p>The lapply() function&apos;s underlying syntax is as follows:</p> <pre> lapply(X, FUN, ...) </pre> <p>In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. You can add more parameters to the FUN function by passing them as the &apos;... argument.&apos;</p> <h3>Some Examples of lapply():</h3> <p> <strong>Example 1:</strong> </p> <p>Let&apos;s look at an illustration of how to employ R&apos;s lapply() function.</p> <p>Let&apos;s say we want to determine the square root of each number in a list of numbers. Each element of the list can have the sqrt() function applied to it using the lapply() method. Here is the key:</p> <p> <strong>Code:</strong> </p> <pre> # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using lapply() result <- lapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-5.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sqrt() function was applied to each element of the list by the lapply() function, which then returned a list that had the same length as the input and contained elements that were the square roots of the corresponding input elements.</p> <p> <strong>Example 2:</strong> </p> <p>The lapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using lapply() result <- lapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-6.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the add 10() function was applied to each element of the list by the lapply() function, which resulted in a list that had the same length as the input and contained each element as the result of adding 10 to its corresponding input element.</p> <p>It&apos;s significant to remember that, regardless of the input, the lapply() method always produces a list. For instance, the result of using the lapply() method on a vector is still a list: Here is an example to show this.</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using lapply() result <- lapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-7.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sqrt() function was used after the lapply() method transformed the input vector into a list.</p> <h2>sapply() Function in R:</h2> <p>A helpful feature of the R programming language is the sapply() function, which may be used to streamline the code for applying a specified function to each element of a list or vector. A vector or matrix with the same length or dimensions as the input is produced as the output, with each element the outcome of applying the specified function to its corresponding input element.</p> <p>The sapply() function&apos;s syntax is as follows:</p> <pre> sapply(X, FUN, simplify = TRUE, ...) </pre> <p>In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. By default, the simplified parameter is set to TRUE, meaning that if the function&apos;s output is a vector or matrix, the result will also be a vector or matrix. To send more arguments to the FUN function, use the &apos;... argument.&apos;</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s sapply() function.</p> <p>Let&apos;s say we want to determine the square root of each number in a list of numbers. The sqrt() function can be applied to each element of the list using the sapply() method. Here is the key:</p> <p> <strong>Code:</strong> </p> <pre> # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using sapply() result <- sapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-8.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sapply() method took each element of the list and applied the sqrt() function to it. It then returned a vector with the same length as the input, each element of which was the square root of its corresponding input element.</p> <p>The sapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using sapply() result <- sapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-9.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sapply() function added 10 to each element of the list using the add 10() function, returning a vector with the same length as the input where each element was the result of the addition of 10.</p> <p>It&apos;s crucial to remember that the sapply() function also works with vectors. In this situation, a vector will still be the result:</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using sapply() result <- sapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-10.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The sapply() method in this situation treated the input vector as a list and applied the sqrt() function to each element, returning a vector with the same length as the input and each element being the square root of its corresponding input element.</p> <h2>tapply() Function in R:</h2> <p>For applying a specified function to subsets of a vector or array based on the values of another variable, the R language&apos;s tapply() function is a helpful tool. Depending on the function used, the output is a vector, array, or list.</p> <p>The tapply() function&apos;s fundamental syntax is as follows:</p> <p>tapply(X, INDEX, FUN, ...)</p> <p>Here, FUN is the function to be applied to each subset, X is the input vector or array, INDEX is a factor or list of factors denoting the subsets, and... are optional parameters that can be supplied to the function FUN.</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s tapply() function.</p> <p>Assume we have a vector of categories and a vector of numbers. The average of the values for each category is what we are looking for.</p> <p>The mean() function can be applied to each subset of the vector based on the category using the tapply() function. The code is here;</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers numbers <- c(23, 18, 25, 32, 20, 19, 27, 31, 22, 24) # create a vector of categories <- c(\'a\', \'b\', \'a\', \'a\') apply the mean() function to each subset based on category using tapply() result tapply(numbers, categories, mean) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-11.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the tapply() function used the mean() function to analyze each subset of the vector according to the category, returning a named vector with the results for each category.</p> <h2>Conclusion:</h2> <p>Simplified vectors or matrices with the same lengths or dimensions as the input can be obtained by applying a given function to each element of a list or vector using R&apos;s sapply() function.</p> <p>Developing code can save a lot of time and effort, especially when working with large datasets. Moreover, the sapply() method is a flexible function that may be used with lists, vectors, and built-in and user-defined functions.</p> <p>Everyone working with R and dealing with data processing and analysis should be familiar with this function.</p> <hr></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></->

Bu durumda FUN, X'in her üyesine uygulanacak fonksiyondur. X, giriş listesi veya vektördür. FUN işlevine, bunları '... argümanı' olarak ileterek daha fazla parametre ekleyebilirsiniz.

Bazı lapply() Örnekleri:

Örnek 1:

R'nin lapply() fonksiyonunun nasıl kullanılacağını gösteren bir resme bakalım.

Java dilimi

Diyelim ki bir sayı listesindeki her sayının karekökünü belirlemek istiyoruz. Listenin her bir öğesine lapply() yöntemi kullanılarak sqrt() işlevi uygulanabilir. İşte anahtar:

Kod:

 # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using lapply() result <- lapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-5.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sqrt() function was applied to each element of the list by the lapply() function, which then returned a list that had the same length as the input and contained elements that were the square roots of the corresponding input elements.</p> <p> <strong>Example 2:</strong> </p> <p>The lapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using lapply() result <- lapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-6.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the add 10() function was applied to each element of the list by the lapply() function, which resulted in a list that had the same length as the input and contained each element as the result of adding 10 to its corresponding input element.</p> <p>It&apos;s significant to remember that, regardless of the input, the lapply() method always produces a list. For instance, the result of using the lapply() method on a vector is still a list: Here is an example to show this.</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using lapply() result <- lapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-7.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sqrt() function was used after the lapply() method transformed the input vector into a list.</p> <h2>sapply() Function in R:</h2> <p>A helpful feature of the R programming language is the sapply() function, which may be used to streamline the code for applying a specified function to each element of a list or vector. A vector or matrix with the same length or dimensions as the input is produced as the output, with each element the outcome of applying the specified function to its corresponding input element.</p> <p>The sapply() function&apos;s syntax is as follows:</p> <pre> sapply(X, FUN, simplify = TRUE, ...) </pre> <p>In this case, FUN is the function that will be applied to each member of X. X is the input list or vector. By default, the simplified parameter is set to TRUE, meaning that if the function&apos;s output is a vector or matrix, the result will also be a vector or matrix. To send more arguments to the FUN function, use the &apos;... argument.&apos;</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s sapply() function.</p> <p>Let&apos;s say we want to determine the square root of each number in a list of numbers. The sqrt() function can be applied to each element of the list using the sapply() method. Here is the key:</p> <p> <strong>Code:</strong> </p> <pre> # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using sapply() result <- sapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-8.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sapply() method took each element of the list and applied the sqrt() function to it. It then returned a vector with the same length as the input, each element of which was the square root of its corresponding input element.</p> <p>The sapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using sapply() result <- sapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-9.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sapply() function added 10 to each element of the list using the add 10() function, returning a vector with the same length as the input where each element was the result of the addition of 10.</p> <p>It&apos;s crucial to remember that the sapply() function also works with vectors. In this situation, a vector will still be the result:</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using sapply() result <- sapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-10.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The sapply() method in this situation treated the input vector as a list and applied the sqrt() function to each element, returning a vector with the same length as the input and each element being the square root of its corresponding input element.</p> <h2>tapply() Function in R:</h2> <p>For applying a specified function to subsets of a vector or array based on the values of another variable, the R language&apos;s tapply() function is a helpful tool. Depending on the function used, the output is a vector, array, or list.</p> <p>The tapply() function&apos;s fundamental syntax is as follows:</p> <p>tapply(X, INDEX, FUN, ...)</p> <p>Here, FUN is the function to be applied to each subset, X is the input vector or array, INDEX is a factor or list of factors denoting the subsets, and... are optional parameters that can be supplied to the function FUN.</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s tapply() function.</p> <p>Assume we have a vector of categories and a vector of numbers. The average of the values for each category is what we are looking for.</p> <p>The mean() function can be applied to each subset of the vector based on the category using the tapply() function. The code is here;</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers numbers <- c(23, 18, 25, 32, 20, 19, 27, 31, 22, 24) # create a vector of categories <- c(\'a\', \'b\', \'a\', \'a\') apply the mean() function to each subset based on category using tapply() result tapply(numbers, categories, mean) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-11.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the tapply() function used the mean() function to analyze each subset of the vector according to the category, returning a named vector with the results for each category.</p> <h2>Conclusion:</h2> <p>Simplified vectors or matrices with the same lengths or dimensions as the input can be obtained by applying a given function to each element of a list or vector using R&apos;s sapply() function.</p> <p>Developing code can save a lot of time and effort, especially when working with large datasets. Moreover, the sapply() method is a flexible function that may be used with lists, vectors, and built-in and user-defined functions.</p> <p>Everyone working with R and dealing with data processing and analysis should be familiar with this function.</p> <hr></-></pre></-></pre></-></pre></-></pre></-></pre></-></pre></->

Bu durumda FUN, X'in her üyesine uygulanacak fonksiyondur. X, giriş listesi veya vektördür. Varsayılan olarak basitleştirilmiş parametre TRUE olarak ayarlanmıştır; bu, fonksiyonun çıktısı bir vektör veya matris ise sonucun da bir vektör veya matris olacağı anlamına gelir. EĞLENCELİ işlevine daha fazla bağımsız değişken göndermek için '... bağımsız değişkeni' kullanın.

R'nin sapply() fonksiyonunun nasıl kullanılacağını gösteren bir resme bakalım.

Java'da str.replace

Diyelim ki bir sayı listesindeki her sayının karekökünü belirlemek istiyoruz. Sqrt() işlevi, sapply() yöntemi kullanılarak listenin her öğesine uygulanabilir. İşte anahtar:

Kod:

 # Create a list of numbers my_list <- list(4, 9, 16, 25) # apply the sqrt() function to each element of list using sapply() result <- sapply(my_list, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-8.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the sapply() method took each element of the list and applied the sqrt() function to it. It then returned a vector with the same length as the input, each element of which was the square root of its corresponding input element.</p> <p>The sapply() function can also be used with user-defined functions. Let&apos;s make a function that adds 10 to a given integer, for instance, and then use this function on each item in the list:</p> <p> <strong>Code:</strong> </p> <pre> # Define a function that adds 10 to a given number add_10 <- 10 function(x) { x + } # apply the add_10() function to each element of list using sapply() result <- sapply(my_list, add_10) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-9.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>In this instance, the sapply() function added 10 to each element of the list using the add 10() function, returning a vector with the same length as the input where each element was the result of the addition of 10.</p> <p>It&apos;s crucial to remember that the sapply() function also works with vectors. In this situation, a vector will still be the result:</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers my_vector <- c(4, 9, 16, 25) # apply the sqrt() function to each element of vector using sapply() result <- sapply(my_vector, sqrt) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-10.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>The sapply() method in this situation treated the input vector as a list and applied the sqrt() function to each element, returning a vector with the same length as the input and each element being the square root of its corresponding input element.</p> <h2>tapply() Function in R:</h2> <p>For applying a specified function to subsets of a vector or array based on the values of another variable, the R language&apos;s tapply() function is a helpful tool. Depending on the function used, the output is a vector, array, or list.</p> <p>The tapply() function&apos;s fundamental syntax is as follows:</p> <p>tapply(X, INDEX, FUN, ...)</p> <p>Here, FUN is the function to be applied to each subset, X is the input vector or array, INDEX is a factor or list of factors denoting the subsets, and... are optional parameters that can be supplied to the function FUN.</p> <p>Let&apos;s look at an illustration of how to employ R&apos;s tapply() function.</p> <p>Assume we have a vector of categories and a vector of numbers. The average of the values for each category is what we are looking for.</p> <p>The mean() function can be applied to each subset of the vector based on the category using the tapply() function. The code is here;</p> <p> <strong>Code:</strong> </p> <pre> # Create a vector of numbers numbers <- c(23, 18, 25, 32, 20, 19, 27, 31, 22, 24) # create a vector of categories <- c(\\'a\\', \\'b\\', \\'a\\', \\'a\\') apply the mean() function to each subset based on category using tapply() result tapply(numbers, categories, mean) print < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/r-tutorial/91/apply-lapply-sapply-11.webp" alt="Apply(), lapply(), sapply(), tapply() Function in R with Examples"> <p>As you can see, the tapply() function used the mean() function to analyze each subset of the vector according to the category, returning a named vector with the results for each category.</p> <h2>Conclusion:</h2> <p>Simplified vectors or matrices with the same lengths or dimensions as the input can be obtained by applying a given function to each element of a list or vector using R&apos;s sapply() function.</p> <p>Developing code can save a lot of time and effort, especially when working with large datasets. Moreover, the sapply() method is a flexible function that may be used with lists, vectors, and built-in and user-defined functions.</p> <p>Everyone working with R and dealing with data processing and analysis should be familiar with this function.</p> <hr></-></pre></-></pre></-></pre></->