Thomas
Thomas

Reputation: 19

Retrieving data in a cell using using row and column name in R

I am able to retrieve a cell value when using index of the column and row, but when I use the actual names, its returning NA for some reason. 9520.700195 is the actual value in the cell and its also what I get when I use [2,2].

here's the code.

stock_data = read.table("/Users/elisabethlussier-arpin/Documents/Besancon/R/real estate/Core RE/s&p_tsx_yreturn.csv", sep=",", header=TRUE)


index_begg = stock_data[ "2000-04-01" ,"Open"] #---> gives NA

index_begg = stock_data[ 2 ,2] #---->works

Upvotes: 1

Views: 2791

Answers (1)

DPH
DPH

Reputation: 4344

There are to parts to this:

  • first: you can select the columns by name just like you did with "OPEN"
  • second: to select rows you need to get the index that corresponds to your condition

Here is a simple example inspired by what you supplied:

df <- data.frame(date = c("2000-04-01","2000-04-02","2000-04-03"),
                 OPEN = c(TRUE, FALSE, TRUE))

        date  OPEN
1 2000-04-01  TRUE
2 2000-04-02 FALSE
3 2000-04-03  TRUE

# select by generatin the index on your condition of date
df[df$date == "2000-04-01", "OPEN"]

[1] TRUE

Note that I formated date as text in this example

Upvotes: 4

Related Questions