4

We can remove element from list using assignment of NULL value :

someList<-list(1,2,3)
someList[2]<-NULL

the same is possible for columns of data frame as it is list object:

someDf<-data.frame(a=1:4,b=2:5)
someDf$a<-NULL

is it possible to do the same for rows of data frame or matrix ? (I'm looking for some fast method for elimination of rows, I can't vectorize due too nature of my algorithm, which significiant part consist of rows deletation, I can't copy data due too there size)

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Qbik
  • 5,885
  • 14
  • 62
  • 93
  • so `someList <- someList[-2,]` won't work due to transient copying? See http://stackoverflow.com/questions/10790204/how-to-delete-a-row-by-reference-in-r-data-table for information about `data.table` – Ben Bolker Jun 15 '14 at 21:54
  • Does this code work? Or is this the vectorized solution you said does not work? Or does this copy the matrix which you cannot do due to the size? `aa <- matrix(c(1,2,3,4,2,3,4,5), nrow=4, byrow=FALSE); aa[-2,]` – Mark Miller Jun 15 '14 at 23:27
  • Hi, Take a bit of time and read the tag excerpt before tagging. [tag:dataframes] is for pandas, whereas you need [tag:data.frame] here. Be careful the next time. See this meta post. [Warn \[r\] users from adding \[dataframes\] tag instead of \[data.frame\] tag](http://meta.stackoverflow.com/q/318933) – Bhargav Rao Mar 14 '16 at 14:15

1 Answers1

3

When you assign NULL to an element of a list, it deletes the element. This behavior extends to a data frame, as the data frame is stored as a list of columns.

Storage as a list of columns also makes row deleting and creation expensive. Each column must be modified in turn. And in fact there is no way to modify the data frame in place to remove a row. You must create a new data frame which lacks the undesired row and assign it to the original variable.

A matrix poses a similar problem, but for both rows and columns. A matrix is stored as a pair of dimensions, and a single vector of data. To remove a row or column, again you must create a new matrix by extracting the desired remaining rows or columns.

That a matrix will hold your data, implies that the rows are all of the same mode. Perhaps you can create a list of rows holding your data, and remove those list elements that you do not want (by assigning NULL), then creating a data frame once you're done.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112