Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a consecutive range of data.frame columns using names of beginning and end columns

Tags:

dataframe

r

I am trying to subset the columns of a data.frame using the interval of column names.

For instance, the data.frame A:

A
ID1 ID2 ID3
1   5  01901
2   5  01902

For example, I want create variable b with the columns of A:

b=A[,"ID2":"ID3"]

Error in "ID1":"ID3" : NA/NaN argument In addition: Warning messages: 1: In [.data.frame(A, , "ID1":"ID3") : NAs introduced by coercion 2: In [.data.frame(A, , "ID1":"ID3") : NAs introduced by coercion

What I want how solution:

b
ID2 ID3
5  01901
5  01902

When I put the indexes of the columns, it works. But when I use the column name, as above, does not work.

like image 846
IGOR Avatar asked Dec 24 '22 05:12

IGOR


2 Answers

Two approaches in base R's data.frame:

  • Named vector column subset
  • Interval approach

Named vector column subset

First, subset by known name:

b = A[, c('ID2', 'ID3')]

Interval approach

Second, subset by an interval when it is known the columns are the same:

# Column Variables
colvars = names(A)

# Get the first ID
start_loc = match("ID1",colvars)

# Get the second ID
end_loc = match("ID3",colvars)

# Subset range
b = A[,start_loc:end_loc]
like image 75
coatless Avatar answered Dec 26 '22 18:12

coatless


If you are not restricted to data.frame, you can convert it to data.table and then your formula will work:

data.table::setDT(A)[, ID2:ID3, with=F]

   ID2  ID3
1:   5 1901
2:   5 1902
like image 33
Psidom Avatar answered Dec 26 '22 18:12

Psidom