[R] apply command

baptiste auguie baptiste.auguie at googlemail.com
Tue Jan 19 11:42:23 CET 2010


Hi,

I think you could use iapply (search the archives) or the plyr package
to save you from transposing the result.

HTH,

baptiste

2010/1/19 marco salvini <marco.salvini at gmail.com>:
> Can you please help on the issue?
> I using the apply command on a matrix below the example:
>
> Create a vector
> x =c(5, 3, 2:4, NA, 7, 3, 9, 2, 1, 5)
>
> create a matrix of 2 rows by 6 columns
>  b=matrix(x, 2,6)
>  print(b)
>     [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    5    2    4    7    9    1
> [2,]    3    3   NA    3    2    5
>
> using the command apply
>  print(apply(b, 1, function(y) sort(y, na.last=F)))
>
> the output is a matrix of 6 rows by 2 columns.
>     [,1] [,2]
> [1,]    1   NA
> [2,]    2    2
> [3,]    4    3
> [4,]    5    3
> [5,]    7    3
> [6,]    9    5
>
> As you can see in the example I start with a matrix of (2 by 6) and the
> output of apply is a mtraxi of (6 by 2).
> This is very strange because I was expecting as output a matrix of the same
> dim (2 by 6) of the input matrix. I can solve this issues using an if
> statment on the dim of the matrix but if I am using a square matrix I am not
> able to control if the result of the apply is correct.
>
> Do anyone find a solution to this issue?
> thanks
> Marco
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list