0

I have a dataframe: trainData. Running table(trainData$Survived) I obtain the following table.

  No  Yes 
1062  490 

How can I obtain just the integer value relative to the No column? Running this:

pNo = table(trainData$Survived)['No']

I still obtain a table in pNo:

  No 
1062 

While I would like to have only 1062! How to do that?

Robb1
  • 4,587
  • 6
  • 31
  • 60
  • 1
    Try `unname(table(trainData$Survived)['No'])` or `table(trainData$Survived)[['No']]` – markus Jun 05 '19 at 10:14
  • Thanks @markus ! That solved my problem: if you make ti an answer to my question I will mark it as the correct one :) – Robb1 Jun 05 '19 at 10:16

2 Answers2

2

Use [[:

Example:

table(iris$Species)[["setosa"]]
# [1] 50
s_baldur
  • 29,441
  • 4
  • 36
  • 69
0

You got a named vector. If you don't want the name 'No', try the as.numeric() method to convert it:

pNo <- as.numeric(table(trainData$Survived)['No'])
pNo
Alan
  • 16
  • 3
  • A small technical note: table() output is not a vector but an array in this case one dimensional. See for example: `dim(table(iris$Species))`, vectors don't have a dim attritbute. – s_baldur Jun 05 '19 at 10:31