Skip to content
Prev 311027 / 398506 Next

Deleting rows with special character

On Nov 16, 2012, at 8:26 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:

            
Using something like rowSums() might be faster in this case, based upon brief testing. 

Since using a boolean returns TRUE/FALSE, which have numeric equivalent values of 1/0, respectively, you can subset the matrix based upon the rowSums() values being equal to the number of columns in the matrix, which indicates that all values in the row match your desired value.


# Create a 230000 * 220 matrix with random values.
set.seed(1)
testdata <- matrix(sample(c("A", "B", "C"), 23000*220, replace = TRUE), ncol = 220)

# Set 100 random rows to all "A"s
set.seed(2)
testdata[sample(23000, 100), ] <- rep("A", 220)
user  system elapsed 
  0.454   0.047   0.503
user  system elapsed 
  0.089   0.001   0.090
chr [1:100, 1:220] "A" "A" "A" "A" "A" "A" "A" "A" ...
chr [1:100, 1:220] "A" "A" "A" "A" "A" "A" "A" "A" ...
[1] TRUE


See ?rowSums, which uses a .Internal, so is fast code.

Regards,

Marc Schwartz