使用R

时间:2017-12-08 15:10:07

标签: r loops subset

我有多个具有完全相同结构的数据框。我想循环遍历所有这些数据帧并删除变量“Hat”= D的行。下面是我对单个实例的看法,但是想为多个数据帧编写一个循环来执行此操作。

final_prices_designations_20161101_20161114.csv <- subset.data.frame(final_prices_designations_20161101_20161114.csv, 
    final_prices_designations_20161101_20161114.csv$Hat == "D")

2 个答案:

答案 0 :(得分:0)

管理此方法的最简单方法是使用apply()函数读取数据和子集项,以删除Hat == 'D'的位置。

# first, read list of files from disk 
theCSVFiles <- list.files(...)  
# read each file and subset to eliminate Hat == D 
fileList <- lapply(theCSVFiles,function(x) read.csv(x)[x$Hat!='D',])
# if necessary to combine to single file
aResult <- do.call(rbind,fileList) 

答案 1 :(得分:0)

#Install the library 'zonator' for function 'file_path_sans_ext'
install.packages("zonator")
library(zonator)
#Change filesPath with location for your data
filesPath <- "D:/R/Import files/"
#file_path_sans_ext : to read files without extension
files <- file_path_sans_ext(list.files(filesPath))
#loop : I chose a loop to create a physicals tables
for (i in 1:length(files)){
  tempTable=read.csv(paste0(filesPath,files[1],".csv"),sep=';')
  assign(files[i] ,tempTable[tempTable$Hat!='D',])
}
remove(tempTable)# to delete temporary table