# Splitting a matrix

#### Taibo

##### New Member
Hi,

I have a matrix x with 97 rows and used the function sample like this :

rownumbers=sample(1:97,size=70)
data1=x[rownumbers,]

I'm having trouble to get the rows which are not inside data1 into another matrix.
Does anyone have an idea?

greets,
Taibo

#### Dason

##### Ambassador to the humans
You could use setdiff on rownumbers and 1:97 to find which rows aren't in rownumbers. Alternatively you could do something like

Code:
x$id = 0 x$id[rownumbers] = 1
split(x, x$id) Note that I'm on a phone so that code might need to be slightly modified. #### trinker ##### ggplot2orBust Hello. Welcome to talkstats. A couple of logistics to make question asking and response getting smoother: When you're posting code, dataframes or computer output it's helpful to wrap this information in code tags by: 1. either clicking the pound (#) sign icon or 2. wrap with [NOPARSE] Code: some code [/NOPARSE] which produces: Code: some code For more see this (LINK) Also... If you give us a dummy data set of what x looks like it is helpful in understanding the problem. maybe something like: Code: [COLOR="#696969"]## First scale the problem down to a MWE:[/COLOR] x <- matrix(rnorm(9*3), ncol=3) rownumbers <- sample(1:nrow(x), size=5) data1 <- x[rownumbers,] c(1:nrow(x))[!1:nrow(x) %in% rownumbers] [COLOR="#696969"]## or...[/COLOR] setdiff(1:nrow(x), rownumbers) [COLOR="#696969"]## so...[/COLOR] x[setdiff(1:nrow(x), rownumbers), ] [COLOR="#696969"]## [,1] [,2] [,3] ## [1,] -0.08743614 0.7005841 0.3234109 ## [2,] -1.52568263 -0.4110520 -0.3591293 ## [3,] -1.65364148 0.2565882 0.8732189 ## [4,] 1.54629539 -1.1694642 -1.3748573 [/COLOR] #### Taibo ##### New Member seems like this is going to work.. the vector x$id seems to be correctly specified.
I still get an error after that last command, however:
Error in split[x, x$id] : object of type 'closure' is not subsettable Do you know how to fix this error? #### Taibo ##### New Member I did not know this ! will keep this in mind ! #### trinker ##### ggplot2orBust Taibo, It's difficult if not impossible to diagnose error messages if we can't reproduce them. We don't know what x is even other than it's a matrix. Please post sample data that reproduces the error. #### Taibo ##### New Member fixed the error, I made a mistake! thank you very much! #### trinker ##### ggplot2orBust Maybe try: Code: id <- rep(0, nrow(x)) id[rownumbers] <- 1 split(x, id) #### trinker ##### ggplot2orBust It depends if x is actually a dataframe or a matrix whether Dason's solution will work as is. #### Dason ##### Ambassador to the humans seems like this is going to work.. the vector x$id seems to be correctly specified.
I still get an error after that last command, however:
Error in split[x, x\$id] :
object of type 'closure' is not subsettable

Do you know how to fix this error?
split is a function so use () and not []