2

I would like to get a vector but I get a data.table :

dt["AAPL", Return]
      symbol     Return
   1:   AAPL -0.3499903
   2:   AAPL  0.6496702
   3:   AAPL  1.0987923
   4:   AAPL -0.5235654
   5:   AAPL -0.2456037
  ---                  
2515:   AAPL  0.5715199
2516:   AAPL  0.4495800
2517:   AAPL  4.5469388
2518:   AAPL  1.2327416
2519:   AAPL  0.3210801

How to get a vector from data.table column with specific rows ?

1
  • 3
    In version 1.9.3 what you tried would work as you expected. Commented Sep 30, 2014 at 16:56

1 Answer 1

3

You can use $ as usual (if you are using a version earlier than version 1.9.3 (thanks @GSee, for the comment).

library(data.table)
DT <- data.table(v1 = c("A", "B"), v2 = 1:10, key = "v1")
DT["A", ]$v2
# [1] 1 3 5 7 9

If you are using the present development version, your approach would work:

## if required...
require(devtools)
install_github("Rdatatable/data.table")

packageVersion("data.table")
# [1] ‘1.9.3’

DT["A", v2]
# [1] 1 3 5 7 9
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need a comma in DT["A", ]$v2. Another possible way is to use [[, something like DT["A"][[2]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.