[R] What's wrong with my code? (Edited version-added my data)

Joshua Wiley jwiley.psych at gmail.com
Tue Jul 12 20:26:55 CEST 2011


Dear Susie,

See inline for some suggestions, but generally, I think you would
benefit from breaking this down into smaller pieces.  The error you
are getting indicates the problem has to do with the plotting, but
that will be trickier to isolate while also dealing with reading in
data, looping, etc.

On Tue, Jul 12, 2011 at 10:11 AM, Susie <susiecrab_ling at hotmail.com> wrote:
> 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.
>
>
> For example, a file "8.csv" would look like such:
>
> enc_callee      inout   o_duration      type
> A       out     342     de
> B       in      234     de
> C       out     132     de
> E       in      111     de
> A       in      13      cf
> H       in      15.7    cf
> G       out     32      de
> A       out     32      cf
> I       in      14      de
> K       in      189     de
> J       out     34.1    cf
> B       in      98.7    de
> H       out     23      de
> C       out     43      cf
> H       in      567     cf
> I       out     12      de
> E       out     12      de
> K       out     12      cf
> B       in      1       cf
> A       out     29      de
> D       out     89      cf
> J       in      302     de
> H       in      12      cf
> A       in      153     cf
> C       out     233     de
>
>
> My command to deal with this simple file would be:
>
> eight <- read.csv(file="8.csv", header=TRUE, sep=",")
> eightout <- subset(eight, inout=="out" & 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)))
> plot(eightout$time,eightout$nocalls)
> write.csv(eightout, "eightM.csv", row.names=FALSE)
>
>
> And then, R will produce "eightM.csv" as such:
>
>   inout enc_callee o_duration nocalls time
> 1    out          A      342.0       3        1
> 3    out          C      132.0       3        2
> 7    out          G       32.0       1        3
> 8    out          A       32.0       3        4
> 11   out          J       34.1       1        5
> 13   out          H       23.0       1       6
> 14   out          C       43.0       3       7
> 16   out          I       12.0       1       8
> 17   out          E       12.0       1      9
> 18   out          K       12.0       1      10
> 20   out          A       29.0       3      11
> 21   out          D       89.0       1     12
> 25   out          C      233.0       3     13
>
> I will also get a plot
> http://r.789695.n4.nabble.com/file/n3662910/eightM.png
>
>
> What I want to do now, is that I have a few hundred similar files, and I
> want to generate the same type of plots and files, so I've written the
> following codes, however, R states that there's some error. I've tried
> editing many times but wasn't successful.
>
>
> my.files <- list.files()

> for (i in 1: length(my.files)) {
> temp.dat <- read.csv(my.files[i])

Maybe I"m missing something, but starting here, I do not see anything
that changes with each iteration of your loop.  It will just keep
reading in, editing and writing out 8.csv over and over.  If I'm
right, then you should just move this part outside of the loop so it
is just done once.

> eight <- read.csv(file="8.csv", header=TRUE, sep=",")
> eightout <- subset(eight, inout=="out" & 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)))
> plot(eightout$time,eightout$nocalls)
> write.csv(eightout, "eight.csv", row.names=FALSE)

{end part that does not seem to change}

> pdf(paste(Sys.Date(),"_",my.files[i],"_.pdf", sep=""))
> plot(temp.dat$time, temp.dat$nocalls, main=my.files[i])

>From the error, my guess is that the problem is right here.  Try
looking at temp.dat$time and temp.dat$nocalls to see if the data are
appropriate for plotting.  Are any of the pdfs and files getting
produced? If yes, this would strongly suggest that your code is
working, but some of your data files are not plottable.

Something else you could try would be to add str(temp.dat) right after
you read in the data in your loop, this should print out the basic
structure of the data and might give you some clues.

HTH,

Josh

> dev.off()
> write.csv(temp.dat, paste(Sys.Date(),"_",my.files[i],"_.csv", sep=""),
> row.names=FALSE)
> }
>
>
> R says:
> 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
>
>
>
> I wonder what went wrong with my codes, please help me!
> Thank you very much!!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/What-s-wrong-with-my-code-Edited-version-added-my-data-tp3662910p3662910.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list