Skip to content

loop and sapply problem, help need

4 messages · Ram H. Sharma, Phil Spector, jim holtman

#
Ram -
    I think you'll have to explain what you're trying to do.
    First, you're creating a 10x10 matrix newd, which could
be done a bit more efficiently by using

     newd = matrix(sample(c(1,2,3,3,),10*length(pvec),replace=TRUE),ncol=10)

    Notice that calling sapply on a matrix applies the function to each
element of the matrix.

    But your fun3 function really doesn't make sense, since you are 
testing each scalar element for equality to a scalar, and then setting
it equal to a vector.  It seems to me that what you're trying to do is

        smpool = c(1,2,3,3)
        allvals = rbind(lcd1,lcd2,lcd3)
        pn = replicate(10,allvals[sample(smpool,1),])

but I can't be sure.
 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu
On Sat, 9 Apr 2011, Ram H. Sharma wrote:

            
#
try this:
+ mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
+ 1 11 22  12 1 2 3 1
+ 2 33 44  34 1 2 3 1
+ 3 22 44  24 3 1 2 1
+ 4 11 33  13 2 2 1 3
+ 5 22 11 12 3 2 2 1 "), header = TRUE)
mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
1    1   11   22   12   1   2   3   1
2    2   33   44   34   1   2   3   1
3    3   22   44   24   3   1   2   1
4    4   11   33   13   2   2   1   3
5    5   22   11   12   3   2   2   1
+     x[[i]] <- lcd[cbind(seq(nrow(x)), x[[i]])]
+ }
mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
1    1   11   22   12  11  22  12  11
2    2   33   44   34  33  44  34  33
3    3   22   44   24  24  22  44  22
4    4   11   33   13  33  33  11  13
5    5   22   11   12  12  11  11  22

        
On Sat, Apr 9, 2011 at 5:27 PM, Ram H. Sharma <sharma.ram.h at gmail.com> wrote: