I am sorry if this is documented in apply's dcumentation or completely obvious, I could not find or work it out. Given an matricies Q: 2x3, R:1x3 and S:1x2 apply(Q, 1, '-', R) is 3x2 and apply(Q, 2, '-', S) is 2x3 Why? cheers Worik> Q[,1] [,2] [,3] [1,] 1 2 3 [2,] 10 11 12> R[1] 1 4 7> S[1] 1 4> apply(Q, 1, '-', R)[,1] [,2] [1,] 0 9 [2,] -2 7 [3,] -4 5> apply(Q, 2, '-', S)[,1] [,2] [,3] [1,] 0 1 2 [2,] 6 7 8>[[alternative HTML version deleted]]
Charles C. Berry
2010-Apr-23 06:11 UTC
[R] Why does 'apply(.., 1, .., ..)' transpose result
On Fri, 23 Apr 2010, Worik R wrote:> I am sorry if this is documented in apply's dcumentation or completely > obvious, I could not find or work it out.Well, try ?apply and focus on the 'Value:' section, first para. To me, that behavior seems consistent with other functions in the [tslm]apply family. Perhaps you want sweep(). See ?sweep HTH, Chuck> > Given an matricies Q: 2x3, R:1x3 and S:1x2 > > apply(Q, 1, '-', R) is 3x2 and apply(Q, 2, '-', S) is 2x3 > > Why? > > cheers > Worik > >> Q > [,1] [,2] [,3] > [1,] 1 2 3 > [2,] 10 11 12 >> R > [1] 1 4 7 >> S > [1] 1 4 >> apply(Q, 1, '-', R) > [,1] [,2] > [1,] 0 9 > [2,] -2 7 > [3,] -4 5 >> apply(Q, 2, '-', S) > [,1] [,2] [,3] > [1,] 0 1 2 > [2,] 6 7 8 >> > > [[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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901