[R] help using outer function
Dan Davison
davison at stats.ox.ac.uk
Sun Aug 10 19:08:06 CEST 2008
On Sun, Aug 10, 2008 at 06:00:21PM +0100, Dan Davison wrote:
> On Sun, Aug 10, 2008 at 09:02:59AM -0700, warthog29 wrote:
> >
> > Hi,
> > I would like to use the R's outer function on y below so that I can subtract
> > elements from each other. The resulting dataframe is symmetric, save for the
> ^^^^^^
> outer() returns a matrix, not a data frame.
>
> > negative signs on the other half of the numbers. I would like to get only
> > half of the dataframe. Here is the code I wrote (it is returning only the
> > first line of the all elements I want. Please help).
> > y<-c(4,4,3.9,3.8,3.7,3.6,3.5,3.5,3.5,3.3,3.2,3.2)
> >
> > b<-outer(y,y,"-")
>
> > b<-as.matrix(by)
>
> I assume that line was supposed to be b<-as.matrix(by). In any case
Hmm, I didn't really clarify things there. I meant
b<-as.matrix(b). But anyway, not needed.
> you don't need it; b is a matrix already.
>
> > # I want to keep the elements:
> > #b[1,2:12],
> > #b[2,3:12],
> > #.........until
> > #b[11,12:12].
>
> Use upper.tri() to get the upper-triangle:
>
> b[upper.tri(b, diag=FALSE)]
> [1] 0.0 0.1 0.1 0.2 0.2 0.1 0.3 0.3 0.2 0.1 0.4 0.4 0.3 0.2 0.1 0.5 0.5 0.4 0.3
> [20] 0.2 0.1 0.5 0.5 0.4 0.3 0.2 0.1 0.0 0.5 0.5 0.4 0.3 0.2 0.1 0.0 0.0 0.7 0.7
> [39] 0.6 0.5 0.4 0.3 0.2 0.2 0.2 0.8 0.8 0.7 0.6 0.5 0.4 0.3 0.3 0.3 0.1 0.8 0.8
> [58] 0.7 0.6 0.5 0.4 0.3 0.3 0.3 0.1 0.0
>
> Or perhaps you want to knock out the negative entries, but still keep the matrix structure:
>
> b[lower.tri(b)] <- NA
>
> or perhaps you wanted
>
> b <- abs(outer(y,y,"-"))
>
> in the first place?
>
> > #Here is the function I wrote to get half of matrix:
> >
> > wk<-function(p){
> > for (i in 2:p){
> > ri<-b[i-1,i:p]
> > return(ri)
> > }
> > }
> > wk(12)
> > #[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.5 0.5 0.7 0.8 0.8
>
> I think you were intending this function to be something like this
>
> wk<-function(p){
> ri <- NULL
> for (i in 2:p){
> ri<-c(ri, b[i-1,i:p])
> }
> return(ri)
> }
>
> Note that this function will give a different result from upper.tri(),
> because you are concatenating elements in the *rows* of the matrix,
> whereas the way matrices are represented in R has consecutive elements
> running down the columns. I.e. look at
>
> > A <- matrix(nrow=2,ncol=2)
> > A
> [,1] [,2]
> [1,] NA NA
> [2,] NA NA
> > A[] <- 1:4
> > A
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
> Dan
>
> >
> > As you can see, it is only returning the first line. I would like other
> > corresponding elements too, to be found in row 2 to 12. Thanks.
> > --
> > View this message in context: http://www.nabble.com/help-using-outer-function-tp18914432p18914432.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > 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