Is it an interactive system for data analysis or is it a sophisticated programming language for software developers? The ability of R to cater to users who do not see themselves as programmers, but then allow them to slide gradually into programming, is an enduring quality of the language and is what has allowed it to gain significance over time. The intentional ambiguity of the R language, inherited from the S language, is one of its defining features. # The first is the list of weights and the second is the list of square of weights. The 4 cylinder entry itself has two sub lists. #The function can also return a list insteadĪ=tapply(mtcars$wt,mtcars$cyl,FUN=function(x)(list(x,x^2))) #the first 11 values are the weight of cars and the next element are the sqaure of the weights If you look at the first group (4 cylinders), > a=tapply(mtcars$wt,mtcars$cyl,FUN=function(x)(c(x,x^2))) Lets see another example where the apply function returns more than one value for each element # for each element return two values, the wt and the square of wt. In this example we used a summary function. The second variable gives the factors on which the function is applied. The first variable is the vector to which we want to apply the function. Here’s how we would do it > tapply(mtcars$wt,mtcars$cyl,mean) Lets say we want to calculate the average weight of the car for each category of number of cylinders (what is the average weight for 4 cylinder etc.). Two columns that we are interested in this example is the cyl(Number of cylinders) and wt (Weight). It contains information about certain cars. In the example below we use the mtcars data frame which is available in the R default installation. The easiest way to understand this is to use an example. The tapply function can be used to apply a function to a category of items. It uses the as.matrix function to coerce the data frame to a matrix (or as.array to an array) > x=array(1:20,dim=c(5,3,2))Īpply works for a data frame too. If there are 3 dimensions use 3 as the second argument to apply the function over the third dimension. Of course we can extend this to more dimensions too. Since there are 5 columns the return value is a vector of 5. The second argument is 2 which instructs R to apply the function(sum) to columns. We could also have applied the function to the columns > apply(x,2,sum) So in our example the value returned is a vector with two elements giving the sum of the first and the second row. Each application returns one value, and the result is the vector of all returned values. There are two rows so the function is applied twice. So in this case R sums all the elements row wise. The second argument instructs R to apply the function to a Row. The first argument in apply is the input matrix x that we just created. Lets look at an example #create a matrix xĮasy right! Needs an explanation though. The result is a vector, list or another array. The apply function can be used apply a function over specific elements of an array (or matrix).
0 Comments
Leave a Reply. |