Asif H
Asif H

Reputation: 157

R - How to extract an element from a single column data frame?

I have a data frame and need to access the 1st row in the 1st column (Negative=16)

[[1]]
              data
Negative        16
Neutral         36
Positive        28
Very Negative    7
Very Positive   19

List of 1
 $ :'data.frame':   5 obs. of  1 variable:
  ..$ data: int [1:5] 16 36 28 7 19

I have tried the following:

x(1,1)
# Error in x(1, 1) : could not find function "x"

x[1,1]
# Error in x[1, 1] : incorrect number of dimensions

x['Negative',1]
# Error in x["Negative", 1] : incorrect number of dimensions

x['Negative']
# $<NA>
# NULL

Upvotes: 2

Views: 760

Answers (1)

Gabriel Mesquita
Gabriel Mesquita

Reputation: 2411

You can read only the first column from a data frame like this:

x <- df[1,, drop = FALSE]

Upvotes: 1

Related Questions