Jason Geller
Jason Geller

Reputation: 7

Deleting column values within a matrix

I have been at it for a while with this one. I hope one of you can help me. I have a large matrix: 122 rows and 6005 columns. One column [,1] lists items codes. Within this column are 25 practice trials I want to get rid of. I tried using this code:

  x1=nw[,1][-c(1:25), 1:6005]

But it produces an incorrect dimension error. If I isolate this column I get the results that I want. Why will this not generalize to the whole matrix? Any help is appreciated.

Upvotes: 0

Views: 474

Answers (2)

Eric Fail
Eric Fail

Reputation: 7928

Deos this solve your problem,

m <- matrix(1:732610, 122 , 6005)
z <- m[-c(1:25),-1]

Upvotes: 1

Mikko
Mikko

Reputation: 7755

You can't just remove values from a matrix, because it has a set dimension (number of rows x number of columns). Instead, try replacing the values with missing values (NA's).

nw <- matrix(rnorm(122*6005, 5, 1), nrow = 122, ncol = 6005)
nw[,1][1:25] <- NA
nw[,1:4]

Then you can treat NA's with na.omit/na.rm functions. For example

mean(nw[,1], na.rm = T)

Upvotes: 0

Related Questions