以字符向量的形式传递一个参数,以便稍后在`subset()`函数的select参数中使用它

时间:2015-06-17 08:57:22

标签: r subset

我试图以字符向量的形式传递一个参数(称为keep_col),以便稍后在subset()函数的select参数中使用它,所有这些都是在我创建的名为early_prep()的更大函数中。 贝娄是我的代码的相关部分。

early_prep <- function(file_name, results_name, id = NULL ,keep_rows =  FALSE, keep_col = FALSE, within_vars = c(), reaction_time = NULL, accuracy = NULL, clear_all = FALSE, decimal_places = 4){

  if (clear_all %in% TRUE){
    # Removes all objects form the console
    rm(list = ls())
  }

  # Call read_data() function
  read_data(file_name)

  if (keep_rows != FALSE) {
    raw_data <<- subset(raw_data, eval(parse(text = keep_rows)))
    # Print to console
    print("#### Deleting unnecesarry rows in raw_data ####", quote = FALSE)
  }

  if (keep_col != FALSE) {
    raw_data <<- subset(raw_data, select = keep_col)
   # Print to console
    print("#### Deleting unnecesarry columns in raw_data ####", quote = FALSE)
  }
}

问题是当我拨打early_prep(file_name ="n44.txt", keep_col = c("subject", "soa", "congruency"))时收到以下警告信息:

> early_prep(file_name = "n44.txt", keep_col = c("subject", "soa", "congruency"))
[1] #### Reading txt file ####
[1] #### Deleting unnecesarry columns in raw_data ####
Warning message:
In if (keep_col != FALSE) { :
  the condition has length > 1 and only the first element will be used

有没有人知道如何解决这个问题?

非常感谢任何帮助

最佳,

Ayala

0 个答案:

没有答案