1

I am using some R code that uses a data table class, instead of a data frame class.

How would I do the following operation in R without having to transform map.dt to a map.df?

map.dt = data.table(chr = c("chr1","chr1","chr1","chr2"), ref = c(1,0,3200,3641), pat = c(1,3020,3022, 3642), mat = c(1,0,3021,0))
parent = "mat"
chrom = "chr1"
map.df<-as.data.frame(map.dt);
parent.block.starts<-map.df[map.df$chr == chrom & map.df[,parent] > 0,parent];

Note: parent needs to be dynamically allocated, its an input from the user. In this example I chose "mat" but it could be any of the columns.

Note1: parent.block.starts should be a vector of integers.

Note2: map.dt is a data table where the column names are c("chr","ref","pat","mat").

The problem is that in data tables I cannot access a given column by name, or at least I couldn't figure out how.

Please let me know if you have some suggestions!

Thanks!

Dnaiel
  • 7,622
  • 23
  • 67
  • 126

1 Answers1

3

It's a little unclear what the end goal is here, especially without sample data, but if you want to access rows by character name there are two ways to do this:

Columns = c("A", "B")
# .. means "look up one level"
dt[,..Columns]

dt[,get("A")]
dt[,list(get("A"), get("B"))]

But if you find yourself needing to use this technique often, you're probably using data.table poorly.

EDIT

Based on your edit, this line will return the same result, without having to do any as.data.frame conversion:

> map.dt[chr==chrom & get(parent) > 0, get(parent)]
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Señor O
  • 17,049
  • 2
  • 45
  • 47
  • 1
    There's also the eval-parse route: http://stackoverflow.com/questions/10675182/in-r-data-table-how-do-i-pass-variable-parameters-to-an-expression/10676138#10676138 – Frank Sep 20 '13 at 15:01
  • @SEnorO Thanks!, I just added an example of map.dt. – Dnaiel Sep 20 '13 at 15:10
  • @SenorO, thanks, the problem is that I need to access them as a character, it's not always mat or pat, it changes. I did map.dt[chr == chrom & map.dt[,get(parent)] > 0,get(parent)]; and it works, but I am curious to know why you think it's not a good use of a data table... – Dnaiel Sep 20 '13 at 15:22
  • @SenorO I just added a note about the dynamic allocation of parent in the Q. – Dnaiel Sep 20 '13 at 15:25
  • 2
    I don't think it's a bad use of `data.table` - I think if you find yourself doing it all the time it's a sign you're probably not using `data.table` well. – Señor O Sep 20 '13 at 15:25
  • @SeñorO awesome u re the master. – Dnaiel Sep 20 '13 at 15:28
  • Glad I could help. The link @Frank posted is a good one to check out if you find using `get` is slow on data.tables with lots of columns. – Señor O Sep 20 '13 at 15:28