1

I've got a data table, and instead of addressing a particular column (say column "x") by (a hardcoded) "dt$x", I'd like to get this done in a dynamic manner by facilitating a variable instead, ie. "dt${var}".

I've already tried "dt[, eval(quote(var)), with=FALSE)", as well as "dt[, c(var), with=FALSE)", but unfortunately that's not working in my particular case at hand.

What I'm trying to achieve is opening up a text connection to dynamically selected column of a data.table.

Working

dt_txt_con <- textConnection(as.character(DT$x))
dt_txt <- data.table(read.table(dt_txt_con, sep=","))
close(dt_txt_con)

Not working

#dt_txt_con <- textConnection(as.character(as.list(DT[,c(var), with=FALSE])))
dt_txt_con <- textConnection(as.character(DT[, eval(quote(var)), with=FALSE]))
dt_txt <- data.table(read.table(dt_txt_con, sep=","))
close(dt_txt_con)

Resulting in an error message

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 1 did not have 38 elements

How best to tackle this one?

-Sil68

EDIT

Sample data (excerpt from a hugh data table held in memory, no file)

DT <- data.table(
            "age,sex,geo\\time" = c("TOTAL,F,AD", "TOTAL,F,AL", "TOTAL,F,AM", "TOTAL,F,AT", "TOTAL,F,AZ"),
            "2014" = c(NA,    NA,      NA,      4351253, NA),
            "2013" = c(37408, NA,      NA,      4328238, 4707690),
            "2012" = c(38252, NA,      1684000, 4309977, 4651601),
            "2011" = c(38252, 1409931, 1679066, 4296293, 4594023),
            "2010" = c(40296, NA,      1673656, 4285442, 4542083)
        )

Variable

var <- "age,sex,geo\\time"
user4338
  • 173
  • 1
  • 12
  • give us the first lines of the file, something to work with – statquant May 09 '15 at 19:46
  • Does [this](http://stackoverflow.com/questions/27677283/evaluating-both-column-name-and-the-target-value-within-j-expression-within-d) help? – David Arenburg May 09 '15 at 19:46
  • @David Thanks, but I'm afraid this won't help in my case. – user4338 May 09 '15 at 21:22
  • @statquant Added sample data, which are held as a data table in memory, not as a file – user4338 May 09 '15 at 21:23
  • 1
    How about just `dt_txt_con <- textConnection(as.character(DT[, .SD[[var]]]))`? Or `dt_txt_con <- textConnection(as.character(DT[, get(var)]))`? (Both appeared in the link I've mentioned) – David Arenburg May 09 '15 at 21:29
  • Or `dt_txt_con <- textConnection(as.character(DT[, eval(as.name(var))]))` (also appeared there). How wasn't that link helpful? – David Arenburg May 09 '15 at 21:36
  • Some things should just not be done. Putting commas and backslashes in column names are two of those things. Use system editing facilities to repair violations of those rules – IRTFM May 10 '15 at 02:02
  • @David Thanks again! Worked like a charm! Must have been the time of night that this one slipped my attention. – user4338 May 10 '15 at 06:09
  • @BondedDust Well, that's the format the data is provided by Eurostat – user4338 May 10 '15 at 06:10
  • @user4338: Right. And it's not appropriate for R's syntactical rules. So then ... fix it. – IRTFM May 10 '15 at 06:17
  • @BondedDust That's one of the goals I'm trying to achieve here. :) – user4338 May 10 '15 at 10:07

1 Answers1

1

As indicated by David,

dt_txt_con <- textConnection(as.character(DT[, .SD[[var]]]))

is working like a charm.

user4338
  • 173
  • 1
  • 12