Hi Starting to use R as a serious tool, I have come across a programming problem that I can't see the answer too yet. Can someone advise me plese. The problem is that I want to plot a series of lines which represent short term growths. All the data is in a single vector and I can indicate the index via a second vector. In GLIM, if the second vector is a factor, a single $GRA Size Year Item instruction will do it with a factor Example data: Item Size Year 1 0 1980 1 10 1981 1 14 1982 1 20 1983 1 25 1984 1 30 1985 2 0 1980 2 5 1981 2 6 1982 2 8 1984 3 0 1984 3 2 1985 4 0 1980 4 20 1981 4 30 1982 4 30 1984 4 35 1985 This should produce 4 jagged lines on a plot, some shorter than others. So far, I can see no simple equivalent of the $GRA instruction with the factors but presumably the trick is to fill up a matrix with the data in a rather ugly loop storing the row number and incrementing on a change in Item. Ensure that the matrix is NA-filled first and then use matplot to do the graph. So (a) is there an alternative and (b) is there a single assignment that will copy the Size vector into a matrix of some sort? The only way I can see to do the latter is equally messy! Sorry to bother - I have an antipathy to ugly coding where something elegant will do! Perhaps a coding therapist is in order? \John -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
John Logsdon <j.logsdon at lancaster.ac.uk> writes:> Example data: > Item Size Year > 1 0 1980 > 1 10 1981 > 1 14 1982 > 1 20 1983 > 1 25 1984 > 1 30 1985 > 2 0 1980 > 2 5 1981 > 2 6 1982 > 2 8 1984 > 3 0 1984 > 3 2 1985 > 4 0 1980 > 4 20 1981 > 4 30 1982 > 4 30 1984 > 4 35 1985 > > This should produce 4 jagged lines on a plot, some shorter than others. > > So far, I can see no simple equivalent of the $GRA instruction with the > factors but presumably the trick is to fill up a matrix with the data in a > rather ugly loop storing the row number and incrementing on a change in > Item. Ensure that the matrix is NA-filled first and then use matplot to > do the graph.Hmm... Best I could think of till now was: dfr<-read.table("/tmp/data",header=T) evalq(plot(Year,Size,type='n'),dfr) invisible(lapply(split(dfr,dfr$Item), function(d)evalq(lines(Year,Size,lty=Item),d))) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._