Q:

Paste a number as macro

Consider the following dataset:
d <- data.frame(replicate(2,sample(0:1,10,rep=TRUE)))
colnames(d) <- c("2000","2001")

I want to make the following subset:
d2 <- data.frame(d$`2000`)

However, instead of using $2000, I want to create a macro which I can use instead. For instance,
year <- 2000

How do I use this new value as a macro. I have tried with:
d2 <- data.frame(d$`formula(paste(year))`)

But this does not work. Can anyone help me?

A:

You could change the year value to character and then select the column.
d[as.character(year)]

#   2000
#1     1
#2     1
#3     1
#4     0
#5     1
#6     0
#7     1
#8     1
#9     1
#10    0

Or similarly with dplyr::select
dplyr::select(d, as.character(year))

