Delete row/column from matrix/array and retain remaining column and row names in R

51 views Asked by At

I have the following code:

# Load BayesFactor package
library(BayesFactor)

# Call raceDolls data
data(raceDolls)

# Attribute data to matrix
raceDolls2 <- raceDolls

Calling raceDolls2 gives the following output:

                    White child Black child
Same-race doll               50          48
Different-race doll          21          41

class(raceDolls2) gives the following: [1] "matrix" "array"

Now I would like to "simply" remove a row, so I do:

# Remove row 'Different-race doll' from raceDolls2
raceDolls2 <- raceDolls2[ !rownames(raceDolls2) == 'Different-race doll']

By calling raceDolls2, I expected the following output:

                    White child Black child
Same-race doll               50          48

However I get this:

[1] 50 48

And class(raceDolls2) now gives the following: "numeric"

Alternatively, when I try to remove a column, I used:

# Load BayesFactor package
library(BayesFactor)

# Call raceDolls data
data(raceDolls)

# Attribute data to matrix
raceDolls3 <- raceDolls

# Remove column 'White child' from raceDools3
raceDolls3 <- raceDolls3[ !colnames(raceDolls3) == 'White child']

I expect this:

                    Black child
Same-race doll               48
Different-race doll          41

However, I get this:

[1] 21 41

I at least expected something like:

48 41

My question:

  • What function do I have to use so the data and attributes are preserved when removing a row
0

There are 0 answers