[R] What's wrong with my code? (Edited version-added my data)
Sarah Goslee
sarah.goslee at gmail.com
Wed Jul 13 03:30:02 CEST 2011
Take a closer look at your R code. Each loop is returning only the
last value, so you're doing the same thing over and over for the last
value from the previous loop.
Write just the code for *one* set of graphs. Look at what needs to
change to do the next set. Write a function that takes those arguments
and makes the graphs. Don't try to cram everything into a whole bunch
of for() loops.
As you've found, that can be very error-prone.
Sarah
On Tue, Jul 12, 2011 at 9:12 PM, Susie <susiecrab_ling at hotmail.com> wrote:
> Hey Josh,
>
> Thank you so much for the help!
>
> Now I have edited my codes--breaking into smaller pieces:) R does not state
> the error again:)
>
> However, the problem comes, please see my codes below-- You've suggested
> earlier that I should move the "eight" part outside the loop; I changed the
> "eight" into "temp.dat", because I wanted the codes and functions to work on
> every file. In order to include it into the loop, I renamed it into this
> general name "temp.dat".
>
> The codes work--in the way that they generates plots and csv files-- BUT,
> they somehow produce exactly the same plots and csv files (for the
> number/length of my.files).
>
> I think the problem has to do with the way I named the files,
> i.e."temp.dat". Is it wrong to name it like that? How should I name it so
> that R produces the result desired? I also tried naming them "i", but it
> doesn't work.
>
> I believe it's just one last small step towards success, but I couldn't
> figure it out. Please help me:) Thanks a lot in advance!!
>
>
>
> my.files <- list.files()
> for (i in 1: length(my.files)) {
> temp.dat <- read.csv(my.files[i], header=TRUE, sep=",")}
>
> for (i in 1: length(my.files)){
> temp.datout <- subset(temp.dat, inout=="Outgoing from panel hh" &
> o_duration>0, select=c(inout, enc_callee, o_duration))
> }
>
> for (i in 1: length(my.files)){
> f <- function(temp.datoutf) nrow(temp.datoutf)
> temp.datnocalls <- ddply(temp.datout,.(enc_callee),f)
> colnames(temp.datnocalls)[2] <- "nocalls"
> temp.datout$nocalls <- temp.datnocalls$nocalls
> [match(temp.datout$enc_callee, temp.datnocalls$enc_callee)]
> temp.datout=data.frame(temp.datout,"time"=c(1:nrow(temp.datout)))
> }
>
>
> for (i in 1: length(my.files)){
> png(paste(Sys.Date(),"_",my.files[i],"_.png", sep=""))
> plot(temp.datout$time, temp.datout$nocalls, main=my.files[i])
> dev.off()
> write.csv(temp.datout, paste(Sys.Date(),"_",my.files[i],"_.csv", sep=""),
> row.names=FALSE)
> }
>
>
--
Sarah Goslee
http://www.stringpage.com
More information about the R-help
mailing list