r - Turning strings into command without using eval() -


i know eval() may lead codes hard maintain, run yet again case don't know else use.

below manual way of getting column a data , create new data frame.

data <- data.frame(a=1:5, b=6:10) new.data <- data.frame( = data[ , "a"] ) 

this works fine 1 column. however, in real application, have extract multiple columns multiple new data frames, , must refer column name (hence title, turning column name (i.e. string) command). how can without resorting eval(parse=text)?

remember data frames stored lists, , lists can subset vector of strings (if list named, column names names of list elements). can like:

> mydf <- data.frame( a=1:5, b=5:1, c=11:15, d=21:25 ) >  > mycols <- c('a','d') >  > mydf[ mycols ]    d 1 1 21 2 2 22 3 3 23 4 4 24 5 5 25 > str(.last.value) 'data.frame':   5 obs. of  2 variables:  $ a: int  1 2 3 4 5  $ d: int  21 22 23 24 25 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -