将为单个文件准备的R脚本应用于目录中的多个文件

时间:2012-04-02 19:03:24

标签: r loops

我准备了一个r脚本,并尝试在目前使用5个示例文件的多个文件中应用相同的代码(但尝试学习使用超过100的文件)以了解如何使用多个文件。很抱歉我写的代码质量,因为我正在学习R,我相信有更好,更有条理的方式来编写我准备的内容。 (请参阅下面的示例数据和我的代码)

我想要实现的是在所有文件中运行我的代码并将它们写回到同一目录或名称略有改动的不同目录。

我尝试使用以下内容读取每个文件,并在{}支架中添加所有代码:

filenames = dir(pattern=".csv") 

for( i in 1:length(filenames) ){}

但是它不起作用,我做这一步的事情都错了,我只是想知道你是否可以给我一些指导我应该如何处理多个文件?

我准备了一个样本数据集,以便我可以向您显示我所拥有的代码,以下两张图片显示了我读取后的数据集以及运行所有代码后数据集的外观:

just after reading the file

After running the codes

我的示例数据文件:

> dput (df)
structure(list(X = structure(c(3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "w", "wo"), class = "factor"), 
    X.1 = structure(c(1L, 11L, 18L, 9L, 26L, 30L, 22L, 5L, 14L, 
    15L, 6L, 23L, 27L, 19L, 2L, 10L, 16L, 7L, 24L, 28L, 20L, 
    3L, 12L, 17L, 8L, 25L, 29L, 21L, 4L, 13L), .Label = c("Fri 1 Jan", 
    "Fri 15 Jan", "Fri 22 Jan", "Fri 29 Jan", "Fri 8 Jan", "Mon 11 Jan", 
    "Mon 18 Jan", "Mon 25 Jan", "Mon 4 Jan", "Sat 16 Jan", "Sat 2 Jan", 
    "Sat 23 Jan", "Sat 30 Jan", "Sat 9 Jan", "Sun 10 Jan", "Sun 17 Jan", 
    "Sun 24 Jan", "Sun 3 Jan", "Thu 14 Jan", "Thu 21 Jan", "Thu 28 Jan", 
    "Thu 7 Jan", "Tue 12 Jan", "Tue 19 Jan", "Tue 26 Jan", "Tue 5 Jan", 
    "Wed 13 Jan", "Wed 20 Jan", "Wed 27 Jan", "Wed 6 Jan"), class = "factor"), 
    X1 = c(322L, 89L, 242L, NA, 136L, 113L, 70L, 134L, 232L, 
    NA, 127L, 124L, 120L, 162L, 179L, 374L, 477L, NA, 147L, 136L, 
    152L, 196L, 384L, 491L, 136L, NA, 143L, 172L, 226L, 509L), 
    X2 = c(409L, 71L, 206L, NA, 104L, 57L, NA, 98L, 201L, NA, 
    74L, 94L, 69L, 98L, 117L, 277L, 445L, NA, 131L, 90L, 83L, 
    NA, 329L, 473L, 73L, NA, 104L, 113L, 136L, 427L), X3 = c(367L, 
    54L, 211L, NA, 107L, 69L, 51L, 63L, 157L, NA, 56L, 115L, 
    96L, 100L, 118L, 250L, 388L, NA, 124L, 85L, 96L, 118L, 313L, 
    431L, 79L, NA, 93L, 135L, 134L, 290L), X4 = c(343L, 60L, 
    183L, NA, 110L, 53L, 32L, 77L, 123L, NA, 37L, 100L, 64L, 
    68L, 99L, 199L, 333L, NA, 107L, 71L, 81L, 89L, 219L, 393L, 
    43L, NA, 72L, 96L, 127L, NA), X5 = c(231L, 42L, 79L, NA, 
    74L, 58L, 48L, 59L, 78L, NA, 62L, 74L, 63L, 59L, 74L, 110L, 
    134L, NA, 74L, 82L, 59L, 73L, 135L, 170L, 53L, NA, 61L, 72L, 
    67L, 186L), X6 = c(140L, 41L, 57L, NA, 104L, 92L, 89L, 94L, 
    68L, NA, 116L, 131L, NA, 110L, 125L, 89L, 89L, NA, 113L, 
    124L, 115L, 116L, 95L, 77L, 126L, NA, 110L, 122L, 119L, 122L
    ), X7 = c(90L, 104L, 82L, NA, 368L, 258L, NA, 289L, 117L, 
    NA, 395L, 416L, 397L, 391L, 400L, 132L, 101L, NA, 397L, 426L, 
    418L, 411L, 151L, 66L, 396L, NA, 457L, 437L, 428L, 128L)), .Names = c("X", 
"X.1", "X1", "X2", "X3", "X4", "X5", "X6", "X7"), class = "data.frame", row.names = c(NA, 
-30L))

我准备的代码:

# example codes for sample data

## step 1 - read file

filename <- '101_E45_N66.csv'
df <- read.csv(filename, header = TRUE,skip =5, nrow =
    length(count.fields(filename)) - 12)

## step 2 - Change coloumn name

colnames(df) = c("type","date","v1","v2","v3","v4","v5","v6","v7")

## step 3 - spliting name "101_E45_N66.csv" to create 3 new coloumn within dataframe

s = strsplit(filename,"_",,fixed=TRUE)[[1]] 
df1= cbind(df[,c("type","date")],ID=s[1],name1=s[2],name2=s[3],df[,3:ncol(df)])

## step 4 - changing type coloumn for weekday/weekend

f = c("wd", "we", "we", "wd", "wd", "wd", "wd") 
df1$type = rep(f,52, length.out = 30)

## creating a backup file 

df2 = df1

## step 5 - subsetting for weekday and weekend

df3 = df2[df2$type == "wd",] ## weekday
df4 = df2[df2$type == "we",] ## weekend

## step 16 - adding new rows in df1 with total, weekday and weeknd sum and number of missing values

df2[31,(6:12)] <- colSums(df1[,6:12], na.rm = T) ## all
df2[32,(6:12)] <- colSums(df3[,6:12], na.rm = T) ## weekday
df2[33,(6:12)] <- colSums(df4[,6:12], na.rm = T) ## weekend

df2[34,(6:12)] = colSums(is.na(df1[,6:12])) ## all missing
df2[35,(6:12)] = colSums(is.na(df3[,6:12]))## weekday missing
df2[36,(6:12)] = colSums(is.na(df4[,6:12]))## weekend missing
df2

2 个答案:

答案 0 :(得分:8)

要将一组csv文件读入data.frame,我经常使用plyr包中的ldply

library(plyr)
all_data = ldply(list.files(pattern = "csv"), function(fname) {
    dum = read.csv(fname)
    dum$fname = fname  # adds the filename it was read from as a column
    return(dum)
  })

如果您需要更具体的内容,可以使用ldply扩展您调用的功能。

答案 1 :(得分:5)

这个玩具示例是否符合您的要求?

# clean up:
rm(list = ls())
setwd(tempdir())
unlink(dir(tempdir()))

# create some files in tempdir:
a <- data.frame(x = 1:3, y = 4:6)
b <- data.frame(x = 10:13, y = 14:15)
write.csv(a, "file1.csv", row.names = F)
write.csv(b, "file2.csv", row.names = F)

# now read all files to list:
mycsv = dir(pattern=".csv")

n <- length(mycsv)
mylist <- vector("list", n)

for(i in 1:n) mylist[[i]] <- read.csv(mycsv[i])

# now change something in all dfs in list:
mylist <- lapply(mylist, function(x) {names(x) <- c("a", "b") ; return(x)})

# then save back dfs:
for(i in 1:n) 
   write.csv(file = paste("file", i, ".csv", sep = ""),
   mylist[i], row.names = F)