0

How can I remove an entire column/row of an atomic vector in R?

Everything I found about this on Q&A sites won't work...

Everytime I get a different error:

  1. := is not an operator
  2. $ not working on atomic vectors
  3. number of items to replace is not a multiple of replacement length

This is my data

> print(nominalTable)

            MORTGAGE NONE OTHER OWN RENT
  36 months      854    1     4 165  928
  60 months      294    0     1  35  218

I want to remove the NONE and OTHER columns so that the mosaicplot would ignore them.

UPDATE

> dput(nominalTable)
structure(c(854L, 294L, 1L, 0L, 4L, 1L, 165L, 35L, 928L, 218L
), .Dim = c(2L, 5L), .Dimnames = structure(list(c("36 months", 
"60 months"), c("MORTGAGE", "NONE", "OTHER", "OWN", "RENT")), .Names = c("", 
"")), class = "table")
4

3 回答 3

4

您可以指定要保留的列的名称,例如:

> nominalTable[, c('MORTGAGE', 'OWN', 'RENT')]

            MORTGAGE OWN RENT
  36 months      854 165  928
  60 months      294  35  218

或者通过它们的 id 删除不需要的列:

> nominalTable[, -(2:3)]

            MORTGAGE OWN RENT
  36 months      854 165  928
  60 months      294  35  218

有关?'['更多详细信息,请参阅。

于 2013-05-26T19:49:12.647 回答
1

您也可以将 NULL 值分配给变量

Mort <- read.table(
  header=TRUE, text='
MORTGAGE NONE OTHER OWN RENT
36_months      854    1     4 165  928
60_months      294    0     1  35  218')
names(Mort) # 5 variables
# [1] "MORTGAGE" "NONE"     "OTHER"    "OWN"      "RENT"
Mort$NONE <- Mort$OTHER <- NULL # set both to NULL
names(Mort) # 3 variables now
#[1] "MORTGAGE" "OWN"      "RENT" 
于 2013-05-29T14:55:44.137 回答
0

这也有效:

subset.matrix(nominalTable,select=-c(NONE,OTHER))

(如果只是简单的subset工作会很好,但它不能正确理解表格)

于 2013-05-29T15:01:23.737 回答