Apply which r




















The lapply function in R is short for list apply. This works in a manner similar to the apply function above, but uses lists instead of matrices. The list mylist is a list of 3 vectors. We wish to apply a mean function to each one of the vectors. This is done by calling lapply mylist,mean that returns the mean values of the three constituent vectors.

Similarly, sapply function in R is short for for simplified apply. Instead of obtaining a separate mean value for each vector, sapply returns a vector containing the mean values. Since data frames can be treated as a special case of lists, the functions lapply and sapply work in both cases. Let us look at an example. Let us create a data frame first and then apply a sort function on it using the lapply function in R. The tapply function also belongs to the same family but used only in case of factors.

This is best explained with an example. Any function can be passed into apply. Apply function in R is primarily used to avoid explicit uses of loop constructs.

It is the most basic of all collections can be used over a matrice. The simplest example is to sum a matrice over all the columns. Lapply in R takes list, vector or data frame as input and gives output in list. The difference between lapply and apply lies between the output return. The output of lapply is a list. A very easy example can be to change the string value of a matrix to lower case with tolower function. We construct a matrix with the name of the famous movies.

The name is in upper case format. It is useful for operations on list objects and returns a list object of same length of original set. Sapply function in R does the same job as lapply function but returns a vector. We can use a user built-in function into lapply or sapply. We create a function named avg to compute the average of the minimum and maximum of the vector.

Sapply in R is more efficient than lapply in the output returned because sapply store values direclty into a vector. In the next example, we will see this is not always the case. The apply command in R allows you to apply a function across an array, matrix or data frame.

You can do this in several ways, depending on the value you specify to the MARGIN argument, that can be normally set to 1 , 2 or c 1, 2. Through this tutorial we are going to use the following example data , so make sure you have it loaded in your workspace.

Note that we are going to use a data frame, but it could also be a matrix or an array instead. For this first example we are going to apply the sum function over the data frame.

Note that in this function it is usual to not specify the argument names due to the simplicity of the function, but remember the order of the arguments. You can also apply at specific indices or cells, subsetting the data you want from your data frame. In this case, the output is a vector containing the sum of each column of the sample data frame. You can also use the apply function to specific columns if you subset the data. It should be noticed that this is more efficient than applying the function to all the data frame and then subsetting the output.

You can set the MARGIN argument to c 1, 2 or, equivalently, to to apply the function to each value of the data frame.



0コメント

  • 1000 / 1000