[R] Random number
Sundar Dorai-Raj
sundar.dorai-raj at PDF.COM
Wed Oct 27 20:37:57 CEST 2004
Kunal Shetty wrote:
> thank you Sundar,Andy, Partha for your prompt reply.
>
> as for the "%*%" is matrix multiplication problem sunder here is a sample of what i want...
>
> x <- rnorm(100,17,24) # X values
> y <- rnorm(100,7,11) # Y values
>
> # since X and Y values are independent to get their
> # covariance need to multiply each of them with a Matrix say A
>
> A <- matrix(c(10,25,8,40),nrow=2,ncol=2)
>
> further i would like to assign both x and y vectors to one variale
>
> say z <- c(x,y)
>
> but if i do this my matrix multiplication fail
>
> i.e w <- A%*%z
> Error in A %*% z : non-conformable arguments ??
>
>
> so it tired multiply the X and Y vector individually
>
> x<- (A%*%x)
> y<- (A%*%y)
>
>
> Error in A %*% z : non-conformable arguments ??
>
>
> the error persist...sunder...
>
> or anybody who could direct me..?
>
Of course this doesn't work. `A' is 2x2, `x' and `y' are 100x1, and `z'
is 200x1 (I think I even mentioned this previously). I *think* you want
something like:
z <- rbind(x, y) # now 2 x 100
w <- A %*% z # 2 x 100
If not, please provide an example of what you expect, possibly worked by
hand.
HTH,
--sundar
> regards
> Kunal
>
>
>
>
>
> Sundar Dorai-Raj <sundar.dorai-raj at PDF.COM> wrote:
>
>>
>>Kunal Shetty wrote:
>>
>>
>>>Dear R- User
>>>
>>> I created two random number sets using rnorm function and calcuted their respective means. However now I would like to randomly
>>>replace some values with NA. Thus create my own test data.
>>> Any help or suggestions on this ?
>>>
>>>
>>
>>Use ?sample:
>>n <- 100
>>x <- rnorm(n)
>>x[sample(n, 4)] <- NA
>>sum(is.na(x))
>># [1] 4
>>
>>
>>> Also wanted to confirm when multiplying two random number vectors x am y by matrix..is this how i do it.
>>>A is the matrix
>>>
>>> z <- c(x,y) # x and y the two set of vectors
>>>
>>> w <- A%*%Z # each element in each vector multipled by the matrix .
>>>
>>
>>(Be careful: R is case sensitive (z != Z).)
>>
>>I'm not really clear what you want here. "%*%" is matrix multiplication
>>and "*" is elementwise multiplication. Also using "c" makes a vector or
>>length "n = length(x) + length(y)". This implies that "A" above "p x n"
>>(i.e. p rows, n columns) and the result "w" would be "p x 1". Please
>>provide an example of what you expect to see by the multiplication.
>>
>>--sundar
>>
>>
>>>regards
>>>Kunal
>>>
>>>______________________________________________
>>>R-help at stat.math.ethz.ch mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list