use the same code to generate same graphs for other files at once
I've written out codes for one particular file, and now I want to generate the same kind of graphs and files for the rest of similar data files, I wrote commands as the following, however, it doesn't work... When I plugged in these codes, R produced only one plot for the file "eight", and it states my error(see below) I have edited and checked my codes so many times but still couldn't figure out what's wrong with it...would you please help me?
I really really appreciate your help!
my.files <- list.files()
for (i in 1: length(my.files)) {
temp.dat <- read.csv(my.files[i])
eight <- read.csv(file="8.csv", header=TRUE, sep=",")
eightout <- subset(eight, ino开发者_Python百科ut=="Outgoing from panel hh" & o_duration>0, select=c(inout, enc_callee, o_duration))
f <- function(eightoutf) nrow(eightoutf)
eightnocalls <- ddply(eightout,.(enc_callee),f)
colnames(eightnocalls)[2] <- "nocalls"
eightout$nocalls <- eightnocalls$nocalls [match(eightout$enc_callee, eightnocalls$enc_callee)]
eightout=data.frame(eightout,"time"=c(1:nrow(eightout)))
M <- plot(eightout$time,eightout$nocalls)
pdf(paste(Sys.Date(),"_",my.files[i],"_.pdf", sep=""))
plot(temp.dat$time, temp.dat$nocalls, main=my.files[i])
dev.off() }
This is what R says Error in plot.window(...) : need finite 'xlim' values In addition: Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf 3: In min(x) : no non-missing arguments to min; returning Inf 4: In max(x) : no non-missing arguments to max; returning -Inf
Try inserting dev.off()
after your plot
command ... ? (If you want a bunch of separate files. If you one want one big file of plots, then open your PDF file before you start the loop and close it after the end of the loop.)
Trying again with a cleaned-up version of code above.
my.files <- list.files("/E/Data")
for (i in seq_along(my.files)) {
temp.dat <- read.csv(my.files[i])
seven <- subset(alldata, aserno==7, select=c(I,C,D),
I=="o" & D>0)
sevennumber <- ddply(sevenout,.(C), nrow)
colnames(sevennumber)[2] <- "number"
sevenout$number <- sevennumber$number[match(sevenout$C,sevennumber$C)]
sevenout$time <- seq(nrow(sevenout))
pdf(paste(Sys.Date(), my.files[i], "M.pdf", sep="_"))
with(sevenout,plot(time, number, main=my.files[i])
dev.off() ## CLOSE pdf
write.csv(sevenout,paste(Sys.Date(),
my.files[i],"new_data.pdf”,sep="_"), row.names=FALSE)
}
Remaining weirdnesses/questions:
tmp.datout
is referred to but not defined. Should it have beensevenout
?- the plot was made with
type="n"
, which would have led to an empty plot frame. ??? - I'm assuming that the manipulations are correct, since I can't reconstruct them.
- if in doubt try setting
i <- 1
and execute the code within your loop manually to see what happens ...
精彩评论