Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Subsetting a data.table with repeated column names with numerical positions

Tags:

r

data.table

I have a data.table that looks like this

> dput(DT)
   A B C A B C D
1: 1 2 3 3 5 6 7
2: 2 1 3 2 1 3 4

Here's the dput

DT <- structure(list(A = 1:2, B = c(2L, 1L), C = c(3L, 3L), A = c(3L, 
  2L), B = c(5L, 1L), C = c(6L, 3L), D = c(7L, 4L)), .Names = c("A", 
  "B", "C", "A", "B", "C", "D"), row.names = c(NA, -2L), class = c("data.table", 
  "data.frame"))

Basically, I want to subset them according to their headers. So for header "B", I would do this:

subset(DT,,grep(unique(names(DT))[2],names(DT)))
   B B
1: 2 2
2: 1 1

As you can see, the values are wrong as the second column is simply a repeat of the first. I want to get this instead:

   B B
1: 2 5
2: 1 1

Can anyone help me please?

like image 514
Wet Feet Avatar asked Nov 07 '13 06:11

Wet Feet


1 Answers

The following alternatives work for me:

pos <- grep("B", names(DT))
DT[, ..pos]
#    B B
# 1: 2 5
# 2: 1 1
DT[, .SD, .SDcols = patterns("B")]
#    B B
# 1: 2 5
# 2: 1 1
DT[, names(DT) %in% unique(names(DT))[2], with = FALSE]
#    B B
# 1: 2 5
# 2: 1 1
like image 187
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 08 '22 08:11

A5C1D2H2I1M1N2O1R2T1