r - Convert a character variable into a logical expression in order to use it later inside the subset argument of the subset() function -


i'm trying convert character variable logical expression in order use later inside subset argument of subset() function, , of inside bigger function called early_prep() created. problem when execute

early_prep(file_name = "n44.txt", keep_rows = "block > 1") 

it deletes rows in raw_data data frame instead of deleting in block > 1.

any appreciated.

best, ayala

bellow relevant part of early_prep() function:

early_prep <- function(file_name,keep_rows = false){    read_data <- function(file_name){   extension <- substr(file_name, nchar(file_name) - 3, nchar(file_name))   if (extension == ".txt"){     raw_data <<- read.table(file_name, header = true)     # print console     print("#### reading txt file ####", quote = false)   } else if (extension == ".csv"){     raw_data <<- read.csv(file_name, header = true)     # print console      print("#### reading csv file ####", quote = false)   } else {     # stops running function     stop("#### file_name should end txt or csv extension ####", quote = false)        }  }     read_data(file_name)    if (keep_rows != false) {    keep_rows <- as.logical(keep_rows)    raw_data <<- subset(raw_data,  keep_rows)    # print console    print("#### deleting unnecessary rows in raw_data ####", quote = false)   } }   

try :

subset(raw_data,eval(parse(text=keep_rows))) 

test :

keep_rows <- "blok>1" raw_data<- data.frame(blok=c(1,2,3,0)) subset(raw_data,eval(parse(text=keep_rows)))    blok 2    2 3    3 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -