在for循环中创建变量并分配值

时间:2020-09-14 18:38:05

标签: r

我有一个包含多个工作表的excel文件,并将每个工作表分配给一个单独的变量。我决定尝试这样的for语句:

  x = 2
for (i in 1:6){
  x <- x + 1
  assign(paste0(i,"_file"), read.xlsx(file.path(path,"Template.xlsx"), sheetIndex = x, colIndex = 1:5, startRow = 6, stringsAsFactors=FALSE)
) 

#remove rows where the entire row is NA (do not remove rows that have some values and/or NA, has to be completely NA)
  paste0(i,"_file")<- paste0(i,"_file") %>% filter_at(colnames(paste0(i,"_file")), any_vars(!is.na(.)))

}

问题是我不知道在使用assign()之后如何删除行。将每个工作表分配给变量后,我想进行一些清理并删除所有不适用的行。我尝试使用paste0和<-运算符,但不起作用。


更新:

使用列表:]但是如何删除每个数据帧的最后4行?

  x = 2
for (i in 1:6){
  x <- x + 1
  scoring_raw[[i]] <- read.xlsx(file.path(path,"Template.xlsx"), sheetIndex = x, colIndex = 1:5, startRow = 6, stringsAsFactors=FALSE) %>%
    filter_all(any_vars(!is.na(.))) %>% #want to remove last 4 rows of dataframe


}

1 个答案:

答案 0 :(得分:1)

尽管@Parfait完全正确,您应该更喜欢使用列表,但是可以使用evalparse来回答您的问题。但是您需要将索引i放在对象名称的末尾,而不是在开头,以免出现问题:

x = 2
for (i in 1:6){
  x <- x + 1
  assign(paste0("file_",i), read.xlsx(file.path(path,"Template.xlsx"), sheetIndex = x, colIndex = 1:5, startRow = 6, stringsAsFactors=FALSE)
  ) 
  
  #remove rows where the entire row is NA (do not remove rows that have some values and/or NA, has to be completely NA)
  assign(paste0("file_",i), 
         eval(parse(text = paste0("file_",i))) %>% 
           filter_all(any_vars(!is.na(.)))
  
}

在选择所有列时,可以通过filter_at来更改filter_all。但您实际上可以这样做:

for (i in 1:6){
  x <- x + 1
  assign(paste0("file_",i), 
         read.xlsx(file.path(path,"Template.xlsx"), sheetIndex = x, colIndex = 1:5, startRow = 6, stringsAsFactors=FALSE) %>%
           filter_all(any_vars(!is.na(.)))
  ) 
}