# An R Question

 Felicien Kanyamibw... Posted 9/14/12 11:20 AM Link to discussion user 62386012 San Francisco, CAPost #: 1 For R experts, I stumbled at this in one's R code and could not figure out what it does.There are two matrices:matrix X with dimensions nrows= n and ncols= pand Y with dimension nrows=n ncols = m;what would this do Y[n,] = X?Thanks for your help. Kevin Posted 12/7/12 5:45 PM Link to reply Flanksteak San Francisco, CAPost #: 5 Y[n,] = X will only work if the total number of elements of X (i.e. n*p) = the number of elements in each row of Y. Otherwise, it will send out an error.basically, you are replacing the nth row of Y with the elements of X. In order to do that, R converts X into a vector of length n*p before overwriting the nth row of Y.See example code here:> x = matrix(1:12, nrow=3)> x [,1] [,2] [,3] [,4][1,] 1 4 7 10[2,] 2 5 8 11[3,] 3 6 9 12> y = matrix(1:18, nrow=3)> y [,1] [,2] [,3] [,4] [,5] [,6][1,] 1 4 7 10 13 16[2,] 2 5 8 11 14 17[3,] 3 6 9 12 15 18> y[3,][1] 3 6 9 12 15 18> y[3,] = xError in y[3, ] = x : number of items to replace is not a multiple of replacement length> as.numeric(x) [1] 1 2 3 4 5 6 7 8 9 10 11 12> x = matrix(1:6, nrow=2)> x [,1] [,2] [,3][1,] 1 3 5[2,] 2 4 6> y[3,] = x> y [,1] [,2] [,3] [,4] [,5] [,6][1,] 1 4 7 10 13 16[2,] 2 5 8 11 14 17[3,] 1 2 3 4 5 6>

#### San Francisco, CA

Founded Jul 17, 2008

• ##### Revolution Analytics

Financial support for meetings

• ##### RStudio

Financial support for meetings

#### People in this Meetup are also in:

• ##### San Francisco Big Data Science

613 Makers

Next Meetup:
Thursday, Dec 12

• ##### SF Bayarea Machine Learning

1,571 Members

• ##### Open Data Bay Area

561 Members

Next Meetup:
Friday, Jan 17

• ##### Bay Area HBase User Group

2,208 HBasers

• ##### Spark User Meetup

1,399 Spark Enthusiasts

• ##### San Francisco Python Meetup Group

3,450 Python Programmers