Use of aaply function from plyr package in R

r

#1

Hello,

I am currently trying to learn about the plyr package of R and I used the aaply function on a data set that I downloaded from the internet.

library(plyr)
nba=read.csv(“NBA_train.csv”)
str(nba)
aaply(nba,1,mean)

and I am getting this error->

Error in 1:n : result would be too long a vector

What should I do to get rid of this error?


#2

Hi @adityashrm21,
Consider the following data frame x:

> x <- data.frame(a = 1:3,b = 3:5,c = 8:10)
> x
  a b  c
1 1 3  8
2 2 4  9
3 3 5 10 

Now if we want the mean of the columns:

 > apply(x,2,mean)
        a b c 
        2 4 9 

For mean of the rows:

> apply(x,1,mean)
[1] 4 5 6

So,for applying the function to rows,the second argument should be 1,for columns it should be 2.
Hope this helps!!