Re: [R] [Rd] How do I modify an exported function in a locked environment?
As others have mentioned its not really a good idea to modify the namespace of a package and writing a wrapper as Duncan suggested is much preferable. An intermediate approach that is not as good as the wrapper but better than modifying the namespace is to copy the objects of interest to your workspace, change their environments appropriately and then modify their functionality: library(zoo) # copy objects of interest and set their environment rollmean - zoo:::rollmean environment(rollmean) - .GlobalEnv rollmean.zoo - zoo:::rollmean.zoo environment(rollmean.zoo) - .GlobalEnv rollmean.default - zoo:::rollmean.default environment(rollmean.default) - .GlobalEnv # modify functionality rollmean.default0 - rollmean.default rollmean.default - function(x, ...) 100 * rollmean.default0(x, ...) # test rollmean(1:5, 3) # 100* used rollmean(zoo(1:5), 3) # 100* used On 7/20/06, Steven McKinney [EMAIL PROTECTED] wrote: Running R.app on Mac OS X 10.4 version _ platform powerpc-apple-darwin8.6.0 arch powerpc os darwin8.6.0 system powerpc, darwin8.6.0 status major 2 minor 3.1 year 2006 month 06 day01 svn rev38247 language R version.string Version 2.3.1 (2006-06-01) I am trying to learn how to modify functions in a locked environment. For an example, suppose I'm using the package zoo. zoo contains function rollmean.default rollmean.default function (x, k, na.pad = FALSE, align = c(center, left, right), ...) { x - unclass(x) n - length(x) y - x[k:n] - x[c(1, 1:(n - k))] y[1] - sum(x[1:k]) rval - cumsum(y)/k if (na.pad) { rval - switch(match.arg(align), left = { c(rval, rep(NA, k - 1)) }, center = { c(rep(NA, floor((k - 1)/2)), rval, rep(NA, ceiling((k - 1)/2))) }, right = { c(rep(NA, k - 1), rval) }) } return(rval) } environment: namespace:zoo Suppose for whatever reason I want output to be in percent, so I'd like to modify the result to be rval - 100 * cumsum(y)/k I cannot just copy the function and change it, as the namespace mechanism ensures the rollmean.default in 'zoo' continues to be used. If I use fixInNamespace(rollmean.default, ns = zoo) I can edit the rval - cumsum(y)/k line to read rval - 100 * cumsum(y)/k save the file and exit the R.app GUI editor. But this does not update the exported copy of the function (the documentation for fixInNamespace says this is the case) - how do I accomplish this last step? If I list the function after editing, I see the original copy. But if I reinvoke the editor via fixInNamespace(), I do see my modification. Where is my copy residing? How do I push it out to replace the exported copy? Is this the proper way to modify a package function? Are there other ways? I've searched webpages, R news, help files and have been unable to find out how to get this process fully completed. Any guidance appreciated. Steven McKinney Statistician Molecular Oncology and Breast Cancer Program British Columbia Cancer Research Centre email: [EMAIL PROTECTED] tel: 604-675-8000 x7561 BCCRC Molecular Oncology 675 West 10th Ave, Floor 4 Vancouver B.C. V5Z 1L3 Canada __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-help@stat.math.ethz.ch 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.
Re: [R] Reading data with blank elements
See if this works: read.csv(datafile.csv, row.names = 1, fill = TRUE) On 7/21/06, Ahamarshan jn [EMAIL PROTECTED] wrote: Hi, I have a dataset saved in *.csv format, that contains 13 columns (the first column being the title name and the rest experiments) and about 2500 rows. Not all columns in the row have data in it i.e for eg BS00,-0.084,0.0136,-0.1569,-0.6484,1.103,1.7859,0.40287,0.5368,0.08461,-0.1935,-0.147974,0.30685 BS01,0.491270283,0.875826172,, BS02,0.090794476,0.225858954,,,0.32643,0.34317,0.133145295,,,0.115832599,0.47636458, BS03,0.019828221,-0.095735935,-0.122767219,-0.0676,0.002533,-0.1510361,0.736247,2.053192,-0.423658,0.4591219,1.1245015, BS04,-0.435189342,-0.041595955,-0.781281128,-1.923036,-3.2301671020.152322609,-1.495513519,, I am using R to perform a correlation, but I am getting an error while trying to read the data as person.data-read.table(datafile.csv,header=TRUE,sep=',',row.names=1) Error in scan (file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1919 did not have 13 elements Execution halted The error looks as though there is a problem with the last element being not read when it is blank. I could introduce terms like na to the blank elements but I donot want to do that because this will hinder my future analysis. Can some one suggest me a solution to overcome this problem while reading the data? , or is there something that I have missed to make the data readable. Thank you in advance, PS: The data was imported from a experiment and saved in excel sheet as a *.csv and then used. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] intersect of list elements
The following assumes that within each component of vectorlist the vector elements are unique. In that case the first two lines define vectorlist and perform the grep, as in your post. Elements of the intersection must occur n times where n is the number of components of vectorlist that match the grep and those elements are extracted in the last line. # from your post vectorlist - list(vector.a.1 = c(a, b, c), vector.a.2 = c(a, b, d), vector.b.1. = c(e, f, g)) idx - grep(vector.a, names(vectorlist)) # get intersection names(which(table(unlist(vectorlist[idx])) == length(idx))) On 7/21/06, Georg Otto [EMAIL PROTECTED] wrote: Hi, i have a list of several vectors, for example: vectorlist $vector.a.1 [1] a b c $vector.a.2 [1] a b d $vector.b.1 [1] e f g I can use intersect to find elements that appear in $vector.a.1 and $vector.a.2: intersect(vectorlist[[1]], vectorlist[[2]]) [1] a b I would like to use grep to get the vectors by their names matching an expression and to find the intersects between those vectors. For the first step: vectorlist[grep (vector.a, names(vectorlist))] $vector.a.1 [1] a b c $vector.a.2 [1] a b d Unfortunately, I can not pass the two vectors as argument to intersect: intersect(vectorlist[grep (vector.a, names(vectorlist))]) Error in unique(y[match(x, y, 0)]) : argument y is missing, with no default I am running R Version 2.3.1 (2006-06-01) Could somone help me to solve this? Cheers, Georg __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] (no subject)
Check out this recent thread: https://www.stat.math.ethz.ch/pipermail/r-help/2006-July/109731.html On 7/22/06, ahmed el kenawy [EMAIL PROTECTED] wrote: hi i created two files in excel with a dbf format in a similar way. the first is opened in R, however, when i try to open the second. i received the following message: sites.can - read.csv(SITES.csv) Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names The second file works with the same command larynx.can - read.csv(LARYNX.csv) SO, WHAT IS THE SOLUTIO Regards Ahmed - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] multcomp plotting Please help :)
On 7/22/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: I REALLY NEED HELP WITH THIS PLEASE [] __ R-help@stat.math.ethz.ch 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. Suggest you follow the instructions to get better chance of a response. __ R-help@stat.math.ethz.ch 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.
Re: [R] Multcomp
On 7/22/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: Here it is again, hope this is more clear I am using the following data (only a small subset is given): Habitat Fungus.yield Birch 20.83829053 Birch 22.9718181 Birch 22.28216829 Birch 24.23136797 Birch 22.32147961 Birch 20.30783598 Oak 27.24047258 Oak 29.7730014 Oak 30.12608508 Oak 25.76088669 Oak 30.14750974 Hornbeam 17.05307949 Hornbeam 15.32805111 Hornbeam 18.26920177 Hornbeam 21.30987049 Hornbeam 21.7173223 I am using the multcomp package to do multiple comparisons as follows library(multcomp) # loads the package fungus-read.table(fungi.txt, Header=T)# Reads the data from file saved as fungi.txt fungus.cirec-simint(Fungus.yield~Habitat, data=fungus,conf.level=0.95,type =c(Tukey)) # Computes cimultaneous intervals using Tukey's method plot(fungus.cirec) # plots the data The plot function plots all the comparisons, I want to plot only part of the data since it clutters the graph. How do I plot only part of the data ? Don't understand what part of the data means. Use data = fungus[1:10,] in the simint call to just process the first 10 data rows. To eliminate a portion of the plot note in ?plot.hmtest that there is a ... argument and its description is that its passed to plot which in turn passes them to plot.default so you could use ylim = 1:2, say, to show only part of the plot vertically. How do I tell it to mark the significant comparisons? # after your plot statement: pp - locator() # now click on a spot on the plot # and then right click and choose stop text(pp$x, pp$y, some text) How do I get rid of the field names in the plot? For eg. The plot labels are HabitatBirch-HabitatOak, I want it to be labeled as Birch-Oak. # change rownames of the estimates which has the effect # of changing the y axis labels rownames(fungus.cirec$estimate) - LETTERS[1:3] plot(fungus.cirec) __ R-help@stat.math.ethz.ch 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.
Re: [R] Multcomp
On 7/22/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Saturday, July 22, 2006 1:37 PM To: Nair, Murlidharan T Cc: R-help@stat.math.ethz.ch Subject: Re: [R] Multcomp On 7/22/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: Here it is again, hope this is more clear I am using the following data (only a small subset is given): Habitat Fungus.yield Birch 20.83829053 Birch 22.9718181 Birch 22.28216829 Birch 24.23136797 Birch 22.32147961 Birch 20.30783598 Oak 27.24047258 Oak 29.7730014 Oak 30.12608508 Oak 25.76088669 Oak 30.14750974 Hornbeam 17.05307949 Hornbeam 15.32805111 Hornbeam 18.26920177 Hornbeam 21.30987049 Hornbeam 21.7173223 I am using the multcomp package to do multiple comparisons as follows library(multcomp) # loads the package fungus-read.table(fungi.txt, Header=T)# Reads the data from file saved as fungi.txt fungus.cirec-simint(Fungus.yield~Habitat, data=fungus,conf.level=0.95,type =c(Tukey)) # Computes cimultaneous intervals using Tukey's method plot(fungus.cirec) # plots the data The plot function plots all the comparisons, I want to plot only part of the data since it clutters the graph. How do I plot only part of the data ? Don't understand what part of the data means. Use data = fungus[1:10,] in the simint call to just process the first 10 data rows. To eliminate a portion of the plot note in ?plot.hmtest that there is a ... argument and its description is that its passed to plot which in turn passes them to plot.default so you could use ylim = 1:2, say, to show only part of the plot vertically. I have to use all the data for my computation. Since the number of comparisons are many I want to plot it in different graphs so that the graph does not look cluttered. So part of the data means a subset of the comparisons in one graph and another subset in another and so on Can you give me an example of the ylim parameter. I tried plot(fungus.cirec, ylim=1:2) and it was not happy with it. I am not completely comfortable with the ... argument. I am trying to read up on it. How do I tell it to mark the significant comparisons? # after your plot statement: pp - locator() # now click on a spot on the plot # and then right click and choose stop text(pp$x, pp$y, some text) This is very useful How do I get rid of the field names in the plot? For eg. The plot labels are HabitatBirch-HabitatOak, I want it to be labeled as Birch-Oak. # change rownames of the estimates which has the effect # of changing the y axis labels rownames(fungus.cirec$estimate) - LETTERS[1:3] plot(fungus.cirec) This only labels it a A,B or C. What I wanted was to remove the field name Habitat in this case and out put the label as Birch-Oak. See ?gsub __ R-help@stat.math.ethz.ch 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.
Re: [R] RfW 2.3.1: regular expressions to detect pairs of identical word-final character sequences
The following requires more than just a single gsub but it does solve the problem. Modify to suit. The first gsub places ... around the first occurrence of any duplicated suffixes. We use the (?=...) zero width regexp to circumvent the nesting problem. Then we use strapply from the gsubfn package to extract the suffixes so marked and paste them together to pass to a second gsub which locates them in the original string appending an r to each. Uncomment the commented pat if you only want to match 2+ character suffixes. library(gsubfn) # places ... around first occurrences of repeated suffixes text - And this is the second sentence pat - (\\w+)(?=\\b.+\\1\\b) # pat - (\\w\\w+)(?=\\b.+\\1\\b) out - gsub(pat, 1\\, text, perl = TRUE) suff - strapply(out, ([^]+), function(x,y)y)[[1]] gsub(paste((, paste(suff, collapse = |), )\\b, sep = ), \\1r, text) On 7/22/06, Stefan Th. Gries [EMAIL PROTECTED] wrote: Dear all I use R for Windows 2.3.1 on a fully updated Windows XP Home SP2 machine and I have two related regular expression problems. platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 3.1 year 2006 month 06 day01 svn rev38247 language R version.string Version 2.3.1 (2006-06-01) I would like to find cases of words in elements of character vectors that end in the same character sequences; if I find such cases, I want to add r to both potentially rhyming sequences. An example: INPUT:This is my dog. DESIRED OUTPUT: Thisr isr my dog. I found a solution for cases where the potentially rhyming words are adjacent: text-This is my dog. gsub((\\w+?)(\\W\\w+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) However, with another text vector, I came across two problems I cannot seem to solve and for which I would love to get some input. (i) While I know what to do for non-adjacent words in general gsub((\\w+?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, This not is my dog, perl=TRUE) # I know this is not proper English ;-) this runs into problems with overlapping matches: text-And this is the second sentence gsub((\\w+?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) [1] Andr this is the secondr sentence It finds the nd match, but since the is match is within the two nd's, it doesn't get it. Any ideas on how to get all pairwise matches? (ii) How would one tell R to match only when there are 2+ characters matching? If the above expression is applied to another character string text-this is an example sentence. gsub((\\w+?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) it also matches the e's at the end of example and sentence. It's not possible to get rid of that by specifying a range such as {2,} text-this is an example sentence. gsub((\\w{2,}?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) because, as I understand it, this requires the 2+ cases of \\w to be identical characters: text-doo yoo see mee? gsub((\\w{2,}?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) Again, any ideas? I'd really appreciate any snippets of codes, pointers, etc. Thanks so much, STG -- Stefan Th. Gries --- University of California, Santa Barbara http://www.linguistics.ucsb.edu/faculty/stgries __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] diff, POSIXct, POSIXlt, POSIXt
Moving this to r-devel. Looking at the diff.POSIXt code we see the problem is that it takes the length of the input using length which is wrong since in the case of POSIXlt the length is always 9 (or maybe length should be defined differently for POSIXlt?). Try this which gives the same problem: dts[-1] - dts[-length(dts)] We get a more sensible answer if length is calculated correctly: dts[-1] - dts[-length(dts[[1]])] On 7/23/06, Patrick Giraudoux [EMAIL PROTECTED] wrote: Try converting to POSIXct: That's what I did finally (see the previous e-mail). dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006) dts - as.POSIXct(strptime(dts, %d/%m/%Y)) diff(dts) Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days What is the problem you are trying to solve? Actually, I don't understand why using diff() and POSIXct provides the expected result and not using POSIXlt. Both POSIXct and POSIXlt are of class POSIXt. The doc of diff() stresses that 'diff' is a generic function with a default method and ones for classes 'ts', 'POSIXt' and 'Date'. It does not mention differences between POSIXct and POSIXlt. Moreover, using diff() with POSIXlt has provided (wrong) numbers... and not an error. This may be difficult to detect sometimes along programme lines. Must one keep in mind that diff() is reliably applicable only on POSIXct? In this case, should not it bve mentionned in the documentation? All the best, Patrick jim holtman a écrit : Try converting to POSIXct: str(dts) 'POSIXlt', format: chr [1:10] 2003-04-15 2003-07-15 2003-10-15 2004-04-15 2004-07-15 2004-10-15 2005-04-15 ... dts [1] 2003-04-15 2003-07-15 2003-10-15 2004-04-15 2004-07-15 2004-10-15 2005-04-15 2005-07-15 [9] 2005-10-15 2006-04-15 dts - as.POSIXct(dts) dts [1] 2003-04-15 EDT 2003-07-15 EDT 2003-10-15 EDT 2004-04-15 EDT 2004-07-15 EDT 2004-10-15 EDT [7] 2005-04-15 EDT 2005-07-15 EDT 2005-10-15 EDT 2006-04-15 EDT diff(dts) Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days On 7/23/06, *Patrick Giraudoux* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Dear Listers, I have encountered a strange problem using diff() and POSIXt: dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006) dts - strptime(dts, %d/%m/%Y) class(dts) [1] POSIXt POSIXlt diff(dts) Time differences of 7862400, 7948800, 15811200, 7862400, 7948800, 15724800, 7862400, 7948800,0 secs In this case the result is not the one expected: expressed in seconds and not in days, and the difference between the two last dates is not 0. Now, if one use a vector of 9 dates only (whatever the date removed), things come well: diff(dts[-1]) Time differences of 92, 183, 91, 92, 182, 91, 92, 182 days Also if one contrains dts to POSIXct dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006) dts - as.POSIXct(strptime(dts, %d/%m/%Y)) diff(dts) Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days Any rational in that? Patrick __ R-help@stat.math.ethz.ch mailto:R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Is there anywhere recycle()?
Try: foo2 - function(x, a) cbind(x,a)[,2] On 7/23/06, Gregor Gorjanc [EMAIL PROTECTED] wrote: Hello! I am writting a function, which should recycle one of its arguments if length of the argument is approprate i.e. something like foo - function(x, a) { n - length(x) if(length(a) n) { # recycle a oldA - a a - vector(length=n) a[1:n] - oldA } ## ... return(a) } foo(c(1, 2), a=c(1, 2)) foo(c(1, 2), a=c(1)) I am now wondering if there is any general/generic functions for such task. Thanks! -- Lep pozdrav / With regards, Gregor Gorjanc -- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc at bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europefax: +386 (0)1 72 17 888 -- One must learn by doing the thing; for though you think you know it, you have no certainty until you try. Sophocles ~ 450 B.C. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Is there anywhere recycle()?
Here is another possibility: rep(a, length = length(x)) On 7/23/06, Gregor Gorjanc [EMAIL PROTECTED] wrote: Hi, Gabor Grothendieck wrote: Try: foo2 - function(x, a) cbind(x,a)[,2] thank you for this. It does work to some extent, but not much better than mine foo. foo2(c(1, 2, 3), a=1) [1] 1 1 1 18:14:08 R foo2(c(1, 2, 3), a=c(1,2,3,4)) [1] 1 2 3 4 Warning message: number of rows of result is not a multiple of vector length (arg 1) in: cbind(1, x, a) 18:14:13 R foo2(c(1, 2, 3), a=c(1,2,3)) [1] 1 2 3 18:14:18 R foo2(c(1, 2, 3), a=c(1,2)) [1] 1 2 1 Warning message: number of rows of result is not a multiple of vector length (arg 2) in: cbind(1, x, a) On 7/23/06, Gregor Gorjanc [EMAIL PROTECTED] wrote: Hello! I am writting a function, which should recycle one of its arguments if length of the argument is approprate i.e. something like foo - function(x, a) { n - length(x) if(length(a) n) { # recycle a oldA - a a - vector(length=n) a[1:n] - oldA } ## ... return(a) } foo(c(1, 2), a=c(1, 2)) foo(c(1, 2), a=c(1)) I am now wondering if there is any general/generic functions for such task. Thanks! -- Lep pozdrav / With regards, Gregor Gorjanc -- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc at bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europefax: +386 (0)1 72 17 888 -- One must learn by doing the thing; for though you think you know it, you have no certainty until you try. Sophocles ~ 450 B.C. __ R-help@stat.math.ethz.ch 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. -- Lep pozdrav / With regards, Gregor Gorjanc -- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc at bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europefax: +386 (0)1 72 17 888 -- One must learn by doing the thing; for though you think you know it, you have no certainty until you try. Sophocles ~ 450 B.C. -- __ R-help@stat.math.ethz.ch 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.
Re: [R] Saving R objects
It depends on what information you want to save and how the program on the other end needs it. For the save version I would at least use ascii = TRUE to get it in a more readable fashion. Look at file.show(mult_test.dat) file.show(mult.out) # but use ascii=TRUE on your save statement. to see what you are getting. Other possibilities are to use R2HTML or XML packages to output to HTML or XML. You might want to handle the various components of Dcirec separately. To see what's inside: unclass(Dcirec) str(Dcirec) dput(Dcirec) and use cat statements to output the components in the format of your choice possibly in conjunction with sprintf. On 7/23/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: I am trying to find the best way to save the follwoing object I am creating library(multcomp) data(recovery) Dcirec-simint(minutes~blanket, data=recovery, conf.level=0.9, alternative=less) I am probably not doing it the most efficient way I think. Here is what I am doing a-print(Dcirec) write(a,file=mult_test.dat, append=T) or save(Dcirec, file=mult.out) Which is the best way to save it, so that I can access its contents outside the R environment? __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] diff, POSIXct, POSIXlt, POSIXt
Just one more comment. It is possible to define length.POSIXlt yourself in which case diff works with POSIXlt objects. length.POSIXlt - function(x) length(x[[1]]) diff(dts) Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days On 7/23/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Moving this to r-devel. Looking at the diff.POSIXt code we see the problem is that it takes the length of the input using length which is wrong since in the case of POSIXlt the length is always 9 (or maybe length should be defined differently for POSIXlt?). Try this which gives the same problem: dts[-1] - dts[-length(dts)] We get a more sensible answer if length is calculated correctly: dts[-1] - dts[-length(dts[[1]])] On 7/23/06, Patrick Giraudoux [EMAIL PROTECTED] wrote: Try converting to POSIXct: That's what I did finally (see the previous e-mail). dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006) dts - as.POSIXct(strptime(dts, %d/%m/%Y)) diff(dts) Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days What is the problem you are trying to solve? Actually, I don't understand why using diff() and POSIXct provides the expected result and not using POSIXlt. Both POSIXct and POSIXlt are of class POSIXt. The doc of diff() stresses that 'diff' is a generic function with a default method and ones for classes 'ts', 'POSIXt' and 'Date'. It does not mention differences between POSIXct and POSIXlt. Moreover, using diff() with POSIXlt has provided (wrong) numbers... and not an error. This may be difficult to detect sometimes along programme lines. Must one keep in mind that diff() is reliably applicable only on POSIXct? In this case, should not it bve mentionned in the documentation? All the best, Patrick jim holtman a écrit : Try converting to POSIXct: str(dts) 'POSIXlt', format: chr [1:10] 2003-04-15 2003-07-15 2003-10-15 2004-04-15 2004-07-15 2004-10-15 2005-04-15 ... dts [1] 2003-04-15 2003-07-15 2003-10-15 2004-04-15 2004-07-15 2004-10-15 2005-04-15 2005-07-15 [9] 2005-10-15 2006-04-15 dts - as.POSIXct(dts) dts [1] 2003-04-15 EDT 2003-07-15 EDT 2003-10-15 EDT 2004-04-15 EDT 2004-07-15 EDT 2004-10-15 EDT [7] 2005-04-15 EDT 2005-07-15 EDT 2005-10-15 EDT 2006-04-15 EDT diff(dts) Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days On 7/23/06, *Patrick Giraudoux* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Dear Listers, I have encountered a strange problem using diff() and POSIXt: dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006) dts - strptime(dts, %d/%m/%Y) class(dts) [1] POSIXt POSIXlt diff(dts) Time differences of 7862400, 7948800, 15811200, 7862400, 7948800, 15724800, 7862400, 7948800,0 secs In this case the result is not the one expected: expressed in seconds and not in days, and the difference between the two last dates is not 0. Now, if one use a vector of 9 dates only (whatever the date removed), things come well: diff(dts[-1]) Time differences of 92, 183, 91, 92, 182, 91, 92, 182 days Also if one contrains dts to POSIXct dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006) dts - as.POSIXct(strptime(dts, %d/%m/%Y)) diff(dts) Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days Any rational in that? Patrick __ R-help@stat.math.ethz.ch mailto:R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Correlations by group
On 7/24/06, Peter J. Lee [EMAIL PROTECTED] wrote: I'm aware that S N Krishna asked the same question. However, I have failed to implement the posted solution for running rank order correlations on multiple subsets of data using the by() function. Here is my problem: Take a set of data from two subjects, who provided numerical infant mortality (IM) estimates for five countries: sub - c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2) #grouping variable = 5 rows x 2 subjects est - c(60, 20, 260, 160, 42, 2, 1, 3, 7, 12) #response variable = 5 estimates x 2 subjects im - c(4, 5, 7, 8, 10, 4, 5, 7, 8, 10) #actual IM values x 2 subjects data - cbind(sub, est, im) data Using the by() function: by(data, sub, function(x) cor(est, im, method = spearman)) The calculation in your function does not depend on x so its giving a constant return value. Try: by(data, sub, function(x) cor(x[,2], x[,3], method = spearman)) or tapply(1:length(sub), sub, function(i) cor(est[i], im[i], method = spearman)) or either the following which returns correlation matrices instead of the correlations: by(data[,2:3], sub, function(x) cor(x, method = spearman)) by(data[,2:3], sub, cor, method = spearman) does result in two correlation coefficients. But instead of by subject, the est x im correlation for the entire set is reported, and then assigned to both subjects. This can be checked using: cor(est, im, method = spearman) Nevertheless, the true coeff's and p-values should be: sub[1] cor.coef = 0.1 p .1 sub[2] cor.coef = 0.9 p .05 I find it peculiar that running a simple regression by groups does work: by(data, sub, function(x) lm(est ~ im, data = x)) indicating that perhaps I'm using the wrong grouping function for correlations. I'm using a fairly standard Pentium 4 running Windows XP. On occasion I am required to calculate up to a quarter of a million individual correlations, so any help would be very much appreciated. Best wishes, Peter James Lee _ Peter James Lee Assistant Professor Psikoloji Bölümü Bilkent University Bilkent Ankara Turkey 06800 e-mail: [EMAIL PROTECTED] office: (90) 312 290 1807 home: (90) 312 290 3447 website: http://www.bilkent.edu.tr/~peterjl/index.html _ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] trim function in R
A . (dot) matches any character and $ matches the end of string so this replaces the last two characters with the empty string: sub(..$, , x) On 7/24/06, Wade Wall [EMAIL PROTECTED] wrote: Hi all, I am looking for a function in R to trim the last two characters of an 8 character string in a vector. For example, I have the codes 37-079-2, 370079-3,37-079-8 and want to trim them to 37-079 by removing the last two characters. Is sub the correct function to use, and if so how can I specify trimming the last 2 characters? I have read the help file, but can't quite figure out how to do it. Thanks, Wade __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Identifying peaks (or offsets) in a time series
Try: RSiteSearch(finding peaks) On 7/24/06, Tauber, Dr E. [EMAIL PROTECTED] wrote: Dear R-users, We are monitoring the activity of animals during a few days period. The data from each animal (crossing of infra-red beam) are collected as a time series (in 30 min bins). An example is attached below. y - c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,3,28,27,46,76,77,60,19,35,55,59,48 ,87,20,38,82,62,60,85,105,69,109,102,100,101,116,126,119,63,27,25,15,8,0 ,0,3,0,0,3,0,0,5,3,0,0,6,1,29,73,56,56,57,92,34,51,30,76,30,38,47,87,22, 0,68,76,94,101,119,114,115,111,116,134,125,76,23,19,30,2,8,0,3,0,0,0,7,0 ,0,0,0,4,0,7,0,21,4,49,51,56,43,55,55,34,48,16,0,61,22,94,63,102,47,100, 96,113,93,109,123,120,124,115,94,96,76,36,3,0,0,0,0,0,0,2,5,0,0,0,0,2,10 ,33,34,15,0,47,22,20,33,52,4,41,45,0,21,18,38,32,21,78,82,72,102,103,118 ,116,118,114,82,18,5,21,4,0,14,0,5,2,0,0,2,2,0,0,3,0,2,7,16,13,17,50,0,4 8,16,19,34,39,33,3,67,0,68,34,65,84,61,100,85,108,124,141,139,134,96,54, 91,54,12,0,0,0,0,0,0,0,0,0,0,0,4,11,0,19,27,15,12,20) We would like to have an automatic way, using R, to identify the time point of offset of each bout of activity (i.e. when activity goes down to a minimum value, for a defined duration). In the example above the offset times (the element number) should be approximately: 53, 99, 146, 191, 239, 283, 330 (the last bout of activity can be ignores). Any help or advice will be greatly appreciated, Many thanks, Eran Eran Tauber (PhD) Lecturer in Molecular Evolution Dept. of Genetics University of Leicester University Rd, Leicester LE1 7RH England Phone: 44 (0)116 252-3455, 252-3421 (lab) Fax: 44 (0)116 252-3378 www.le.ac.uk/genetics/et22 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Saving R objects
out.data.mat can be created compactly using with: out.data.mat - with(mult.comp, cbind(estimate, conf.int, p.value.raw = c(p.value.raw), p.value.bon, p.value.adj) ) On 7/24/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: I explicitly did is this way isoforms-as.vector(rownames(mult.comp$estimate)) estimate-as.vector(mult.comp$estimate) lower-as.vector(mult.comp$conf.int[,1]) upper-as.vector(mult.comp$conf.int[,2]) p.val.raw-as.vector(mult.comp$p.value.raw) p.val.bon-as.vector(mult.comp$p.value.bon) p.val.adj-as.vector(mult.comp$p.value.adj) out.data.mat-cbind(isoforms,estimate,lower,upper,p.val.raw,p.val.bon,p.val.adj) write.table(out.data.mat, file=filename.csv, sep=,, qmethod=double, col.name=NA) Thanks ../Murli From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Sun 7/23/2006 10:11 PM To: Nair, Murlidharan T Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Saving R objects It depends on what information you want to save and how the program on the other end needs it. For the save version I would at least use ascii = TRUE to get it in a more readable fashion. Look at file.show(mult_test.dat) file.show(mult.out) # but use ascii=TRUE on your save statement. to see what you are getting. Other possibilities are to use R2HTML or XML packages to output to HTML or XML. You might want to handle the various components of Dcirec separately. To see what's inside: unclass(Dcirec) str(Dcirec) dput(Dcirec) and use cat statements to output the components in the format of your choice possibly in conjunction with sprintf. On 7/23/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: I am trying to find the best way to save the follwoing object I am creating library(multcomp) data(recovery) Dcirec-simint(minutes~blanket, data=recovery, conf.level=0.9, alternative=less) I am probably not doing it the most efficient way I think. Here is what I am doing a-print(Dcirec) write(a,file=mult_test.dat, append=T) or save(Dcirec, file=mult.out) Which is the best way to save it, so that I can access its contents outside the R environment? __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Overplotting: plot() invocation looks ugly ... suggestions?
Try: matplot(levels(data$Quarter), matrix(data$Consumption, 4), type = o) On 7/24/06, John McHenry [EMAIL PROTECTED] wrote: Hi WizaRds, I'd like to overplot UK fuel consumption per quarter over the course of five years. Sounds simple enough? Unless I'm missing something, the following seems very involved for what I'm trying to do. Any suggestions on simplifications? The way I did it is awkward mainly because of the first call to plot ... but isn't this necessary, especially to set limits for the plot? The second call to plot(), in conjunction with by(), seems to be natural enough, and, IMHO, seems to be readable and succinct. data- read.table(textConnection(YearQuarterConsumption 19651874 19652679 19653616 19654816 19661866 19662700 19663603 19664814 19671843 19672719 19673594 19674819 19681906 19682703 19683634 19684844 19691952 19692745 19693635 19694871), header=TRUE) data$Quarter- as.factor(data$Quarter) # # what follows is only marginally less involved than using a for loop # (the culprit is, in part, the need to make the first, type=n, call to plot()): windows(width=12,height=6) with(data, plot(levels(Quarter), Consumption[Year==Year[1]], ylim=c(min(Consumption), max(Consumption)), type=n)) with(data, by(Consumption, Year, function(x) lines(levels(Quarter), x, type=o))) Thanks, Jack. - Groups are talking. We´re listening. Check out the handy changes to Yahoo! Groups. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Overplotting: plot() invocation looks ugly ... suggestions?
And if lattice is ok then try this: library(lattice) xyplot(Consumption ~ Quarter, group = Year, data, type = o) On 7/24/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Try: matplot(levels(data$Quarter), matrix(data$Consumption, 4), type = o) On 7/24/06, John McHenry [EMAIL PROTECTED] wrote: Hi WizaRds, I'd like to overplot UK fuel consumption per quarter over the course of five years. Sounds simple enough? Unless I'm missing something, the following seems very involved for what I'm trying to do. Any suggestions on simplifications? The way I did it is awkward mainly because of the first call to plot ... but isn't this necessary, especially to set limits for the plot? The second call to plot(), in conjunction with by(), seems to be natural enough, and, IMHO, seems to be readable and succinct. data- read.table(textConnection(YearQuarterConsumption 19651874 19652679 19653616 19654816 19661866 19662700 19663603 19664814 19671843 19672719 19673594 19674819 19681906 19682703 19683634 19684844 19691952 19692745 19693635 19694871), header=TRUE) data$Quarter- as.factor(data$Quarter) # # what follows is only marginally less involved than using a for loop # (the culprit is, in part, the need to make the first, type=n, call to plot()): windows(width=12,height=6) with(data, plot(levels(Quarter), Consumption[Year==Year[1]], ylim=c(min(Consumption), max(Consumption)), type=n)) with(data, by(Consumption, Year, function(x) lines(levels(Quarter), x, type=o))) Thanks, Jack. - Groups are talking. We´re listening. Check out the handy changes to Yahoo! Groups. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] RfW 2.3.1: regular expressions to detect pairs of identical word-final character sequences
Regarding having to do a lot of backtracking one can just look at the relative comparison of speeds and we see that they are comparable in speed. In fact the bottleneck is not the backtacking but strapply. I had coded the regexp version for compactness of code but if we replace the strapply with custom gsub/strapply code for speed, the new rexexp version is twice as fast as the for loop version. Below f1 is the for loop version, f2 is the original regexp version with strapply and f3 is the revised version using gsub/strsplit instead. f1 - function() { tmp1 - strsplit(text, ' ')[[1]] tmp2 - nchar(tmp1) tmp3 - substr(tmp1,tmp2-1,tmp2) tmp4 - which(lower.tri(diag(length(tmp3))), arr.ind=TRUE) tmp5 - tmp3[ tmp4[,1] ] == tmp3[ tmp4[,2] ] tmp6 - rep('', length(tmp1)) count - 1 for( i in which(tmp5) ){ tmp6[ tmp4[i,1] ] - paste(tmp6[ tmp4[i,1] ], 'r',count,'',sep='') tmp6[ tmp4[i,2] ] - paste(tmp6[ tmp4[i,2] ], 'r',count,'',sep='') count - count + 1 } out.text - paste( tmp1,tmp6, sep='',collapse=' ') } # places ... around first occurrences of repeated suffixes library(gsubfn) f2 - function() { text - And this is the second sentence pat - (\\w+)(?=\\b.+\\1\\b) # pat - (\\w\\w+)(?=\\b.+\\1\\b) out - gsub(pat, 1\\, text, perl = TRUE) suff - strapply(out, ([^]+), function(x,y)y)[[1]] gsub(paste((, paste(suff, collapse = |), )\\b, sep = ), \\1r, text) } f3 - function() { text - And this is the second sentence pat - (\\w+)(?=\\b.+\\1\\b) # pat - (\\w\\w+)(?=\\b.+\\1\\b) out - gsub(pat, 1\\, text, perl = TRUE) # redo this strapply by hand for speed purposes # suff - strapply(out, ([^]+), function(x,y)y)[[1]] suff - gsub([^]*|[^]*|[^]*$, , out) suff - gsub(^|$, , suff) suff - strsplit(suff, )[[1]] gsub(paste((, paste(suff, collapse = |), )\\b, sep = ), \\1r, text) } # for loop version system.time(for (i in 1:100) f1()) # 0.32 0.00 0.36 NA NA # original regexp version with strapply system.time(for (i in 1:100) f2()) # 0.36 0.00 0.38 NA NA # regexp version with strapply replaced with gsub/strsplit system.time(for (i in 1:100) f3()) # 0.15 0.00 0.16 NA NA On 7/25/06, Greg Snow [EMAIL PROTECTED] wrote: Using regular expression matching for this case may be overkill (the RE engine will be doing a lot of backtracking looking at a lot of non-matches). Here is an alternative that splits the text into a vector of words, extracts the last 2 letters of each word (remember if the last 3 letters match, then the last 2 have to match, so we only need to consider the last 2), then looks at all pairwise comparisons for matches, then pastes everything back together with the marked matches: text-And this is a second rand sentence tmp1 - strsplit(text, ' ')[[1]] tmp2 - nchar(tmp1) tmp3 - substr(tmp1,tmp2-1,tmp2) tmp4 - which(lower.tri(diag(length(tmp3))), arr.ind=TRUE) tmp5 - tmp3[ tmp4[,1] ] == tmp3[ tmp4[,2] ] tmp6 - rep('', length(tmp1)) count - 1 for( i in which(tmp5) ){ tmp6[ tmp4[i,1] ] - paste(tmp6[ tmp4[i,1] ], 'r',count,'',sep='') tmp6[ tmp4[i,2] ] - paste(tmp6[ tmp4[i,2] ], 'r',count,'',sep='') count - count + 1 } out.text - paste( tmp1,tmp6, sep='',collapse=' ') If you are doing a lot of text processing like this, I would suggest doing it in Perl rather than R. S Poetry by Dr. Burns has a function to take a vector of character strings in R and run a Perl script on it and return the results. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Stefan Th. Gries Sent: Saturday, July 22, 2006 7:49 PM To: r-help@stat.math.ethz.ch Subject: [R] RfW 2.3.1: regular expressions to detect pairs of identical word-final character sequences Dear all I use R for Windows 2.3.1 on a fully updated Windows XP Home SP2 machine and I have two related regular expression problems. platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 3.1 year 2006 month 06 day01 svn rev38247 language R version.string Version 2.3.1 (2006-06-01) I would like to find cases of words in elements of character vectors that end in the same character sequences; if I find such cases, I want to add r to both potentially rhyming sequences. An example: INPUT:This is my dog. DESIRED OUTPUT: Thisr isr my dog. I found a solution for cases where the potentially rhyming words are adjacent: text-This is my dog. gsub((\\w+?)(\\W\\w+?)\\1(\\W), \\1r\\2\\1r\\3, text,
Re: [R] greek letters, text, and values in labels
Try: plot(1:10, main = bquote(Results for ~ theta == .(theta))) On 7/25/06, Adrian Dragulescu [EMAIL PROTECTED] wrote: Hello, I want to have a title that will look something like: Results for \theta=2.1, given that I have a variable theta=2.1, and \theta should show on the screen like the greek letter. I've tried a lot of things: theta - 2.1 plot(1:10, main=expression(paste(Results for, theta, =, eval(theta or using bquote plot(1:10, main=paste(Results for , bquote(theta == .(theta or using substitute, etc. I could not make it work. This should be easy. I would appreciate your help. Thanks, Adrian __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] RfW 2.3.1: regular expressions to detect pairs of identical word-final character sequences
Here is yet another solution. This one consists only of two gsubs and a function to reverse a string. It runs at about the same speed as f3 but its main advantage is how compact it is. pat could be the same as before however we have made use of Greg's discussion to use \\w\\w to avail ourself of his speedup idea. If single letter endings are ok use \\w instead of \\w\\w. This time the first gsub simply appends r to the first in any duplicated ending. Then we reverse the string. In the second gsub we look for any sequence at the start of a word for which r followed by that sequence is found later in the string and prepend r to that. Finally we reverse the result. text - And this is the second sentence strrev - function(x) paste(rev(strsplit(x, )[[1]]), collapse = ) pat - (\\w\\w)(?=\\b.+\\1\\b) out - strrev(gsub(pat, \\1\\r, text, perl = TRUE)) strrev(gsub(\\b(\\w+)(?=.*r\\1), r\\1, out, perl = TRUE)) On 7/23/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: The following requires more than just a single gsub but it does solve the problem. Modify to suit. The first gsub places ... around the first occurrence of any duplicated suffixes. We use the (?=...) zero width regexp to circumvent the nesting problem. Then we use strapply from the gsubfn package to extract the suffixes so marked and paste them together to pass to a second gsub which locates them in the original string appending an r to each. Uncomment the commented pat if you only want to match 2+ character suffixes. library(gsubfn) # places ... around first occurrences of repeated suffixes text - And this is the second sentence pat - (\\w+)(?=\\b.+\\1\\b) # pat - (\\w\\w+)(?=\\b.+\\1\\b) out - gsub(pat, 1\\, text, perl = TRUE) suff - strapply(out, ([^]+), function(x,y)y)[[1]] gsub(paste((, paste(suff, collapse = |), )\\b, sep = ), \\1r, text) On 7/22/06, Stefan Th. Gries [EMAIL PROTECTED] wrote: Dear all I use R for Windows 2.3.1 on a fully updated Windows XP Home SP2 machine and I have two related regular expression problems. platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 3.1 year 2006 month 06 day01 svn rev38247 language R version.string Version 2.3.1 (2006-06-01) I would like to find cases of words in elements of character vectors that end in the same character sequences; if I find such cases, I want to add r to both potentially rhyming sequences. An example: INPUT:This is my dog. DESIRED OUTPUT: Thisr isr my dog. I found a solution for cases where the potentially rhyming words are adjacent: text-This is my dog. gsub((\\w+?)(\\W\\w+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) However, with another text vector, I came across two problems I cannot seem to solve and for which I would love to get some input. (i) While I know what to do for non-adjacent words in general gsub((\\w+?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, This not is my dog, perl=TRUE) # I know this is not proper English ;-) this runs into problems with overlapping matches: text-And this is the second sentence gsub((\\w+?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) [1] Andr this is the secondr sentence It finds the nd match, but since the is match is within the two nd's, it doesn't get it. Any ideas on how to get all pairwise matches? (ii) How would one tell R to match only when there are 2+ characters matching? If the above expression is applied to another character string text-this is an example sentence. gsub((\\w+?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) it also matches the e's at the end of example and sentence. It's not possible to get rid of that by specifying a range such as {2,} text-this is an example sentence. gsub((\\w{2,}?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) because, as I understand it, this requires the 2+ cases of \\w to be identical characters: text-doo yoo see mee? gsub((\\w{2,}?)(\\W.+?)\\1(\\W), \\1r\\2\\1r\\3, text, perl=TRUE) Again, any ideas? I'd really appreciate any snippets of codes, pointers, etc. Thanks so much, STG -- Stefan Th. Gries --- University of California, Santa Barbara http://www.linguistics.ucsb.edu/faculty/stgries __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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
Re: [R] generating sequences with gaps
Here are a few possibilies: x - c(4, 12, 20) rep(x, each = 3) + 0:2 rep(x, each = 3) + sequence(rep(3, length(x))) - 1 c(sapply(x, seq, length = 3)) On 7/25/06, etienne [EMAIL PROTECTED] wrote: I need sequences that have gaps in them, such as the following: 4 5 6 | 12 13 14 | 20 21 22 a simple question, I've been scratching my head for a R function that will do this The : and seq do not allow this, and the c() can be used although not in an automatic way. I'm sure there is a way to do it without using a for() construct. Thank you __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Main title of plot
This was just discussed yesterday. See the thread: https://www.stat.math.ethz.ch/pipermail/r-help/2006-July/109931.html On 7/26/06, Marco Boks [EMAIL PROTECTED] wrote: I am a newbie, and I am afraid this may be a rather trivial question. However I could not find the answer anywhere. I am plotting a series of plots with different values for p. In the main title of a plot I have used the following code: plot(a,b,type=l,ylim=c(0,1), xlab=freq,ylab=power, main=c(maximum gain=,p) ) That works fine. However the value of p is plotted on a new line, instead of just after the = Is there anyway to print the value of p on the same line? Thanks Marco [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] the first and last case
Try these: # 1 library(Hmisc) summary(y ~ ind, dat, fun = range, overall = FALSE) # 2 # or with specified column names f - function(x) c(head = head(x,1), tail = tail(x,1)) summary(y ~ ind, dat, fun = f, overall = FALSE) # 3 # another approach using by - same f as above do.call(rbind, by(dat$y, dat$ind, f)) # 4 # same but with with an ind column g - function(x) c(ind = x$ind[1], head = head(x$y,1), tail = tail(x$y,1)) do.call(rbind, by(dat, dat$ind, g)) On 7/26/06, Mauricio Cardeal [EMAIL PROTECTED] wrote: Hi all Sometime ago I asked for a solution about how to aggregate data and the help was wonderful. Now, I´d like to know how to extract for each individual case below the first and the last observation to obtain this: ind y 18 19 27 2 11 39 3 10 4 8 4 5 # Below the example: ind - c(1,1,1,2,2,3,3,3,4,4,4,4) y - c(8,10,9,7,11,9,9,10,8,7,6,5) dat - as.data.frame(cbind(ind,y)) dat attach(dat) mean.ind - aggregate(dat$y, by=list(dat$ind), mean) mean.ind Thanks Mauricio __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Moving Average
See ?filter - simple and exponential are special cases ?runmean - in package caTools (the fastest) ?rollmean - in zoo package ?embed - can write your own using embed as basis ?sma - in package fSeries, also see ewma in same package Probably other functions in other packages too. On 7/26/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Dear R-Users, How can I compute simple moving averages from a time series in R? Note that I do not want to estimate a MA model, just compute the MA's given a lenght (as excel does). Thanks Ricardo Gonçalves Silva, M. Sc. Apoio aos Processos de Modelagem Matemática Econometria Inadimplência Serasa S.A. (11) - 6847-8889 [EMAIL PROTECTED] ** As informações contidas nesta mensagem e no(s) arquivo(s) anexo(s) são endereçadas exclusivamente à(s) pessoa(s) e/ou instituição(ões) acima indicada(s), podendo conter dados confidenciais, os quais não podem, sob qualquer forma ou pretexto, ser utilizados, divulgados, alterados, impressos ou copiados, total ou parcialmente, por pessoas não autorizadas. Caso não seja o destinatário, favor providenciar sua exclusão e notificar o remetente imediatamente. O uso impróprio será tratado conforme as normas da empresa e da legislação em vigor. Esta mensagem expressa o posicionamento pessoal do subscritor e não reflete necessariamente a opinião da Serasa. ** [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Branching on 'grep' returns...
If you are using grep then I think you have it right. Note that this %in% trg is also available. On 26 Jul 2006 11:16:25 -0400, Allen S. Rout [EMAIL PROTECTED] wrote: Greetings, all. I'm fiddling with some text manipulation in R, and I've found something which feels counterintuitive to my PERL-trained senses; I'm hoping that I can glean new R intuition about the situation. Here's an example, as concise as I could make it. trg-c(this,that) # these two work as I'd expected. if ( grep(this,trg) ) { cat(Y\n) } else { cat(N\n) } if ( grep(that,trg) ) { cat(Y\n) } else { cat(N\n) } # These all fail with error 'argument is of length zero' # if ( grep(other,trg) ) { cat(Y\n) } else { cat(N\n) } # if ( grep(other,trg) == TRUE) { cat(Y\n) } else { cat(N\n) } # if ( grep(other,trg) == 1) { cat(Y\n) } else { cat(N\n) } # This says that the result is a numeric zero. Shouldn't I be able # to if on that, or at least compare it with a number? grep(other, trg) # I eventually decided this worked, but felt odd to me. if ( any(grep(other,trg))) { cat(Y\n) } else { cat(N\n) } So, is the 'Wrap it in an any()' just normal R practice, and I'm too new to know it? Is there a more fundamental dumb move I'm making? - Allen S. Rout __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Multcomp
Look through multcomp:::plot.hmtest to find out which components of an hmtest object are actually used. Now look at what an hmtest object looks like by doing this dput(Dcirec) or looking through the source of the function that produces hmtest objects. With this information in hand we can construct one from out.data.mat: my.hmtest - structure(list( estimate = t(t(structure(out.data.mat[,estimate], .Names = rownames(out.data.mat, conf.int = out.data.mat[,2:3], ctype = Dunnett), class = hmtest) plot(my.hmtest) Note that this is a bit fragile since changes to the internal representation of hmtest objects could cause your object to cease working although as long as those changes do not affect the three components we are using it should be ok. By the way I hard coded Dunnett above since ctype is not available in out.data.mat . On 7/26/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: Let me clarify with a simpler example what I want to accomplish library(multcomp) data(recovery) Dcirec-simint(minutes~blanket,data=recovery, conf.level=0.9, alternative=less) out.data.mat - with(Dcirec,data.frame(estimate, conf.int, p.value.raw = c(p.value.raw), p.value.bon, p.value.adj)) I want to generate the same type of plot using out.data.mat that I get by plot(Dcirec) How do I specify the plot method how the data in out.data.mat is to be plotted? I am interested in doing this because, I am running about 1500 different comparisons, which creates 1500 different objects. I need to analyze them and combine significant ones into one plot. -Original Message- From: Greg Snow [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 25, 2006 12:12 PM To: Nair, Murlidharan T Subject: RE: [R] Multcomp Doing: str( fungus.cirec ) Suggests that fungus.cirec$conf.int contains the confidence intervals, you can manually plot the subset that you are intereseted in (and label them whatever you want) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Nair, Murlidharan T Sent: Saturday, July 22, 2006 11:00 AM To: R-help@stat.math.ethz.ch Subject: [R] Multcomp Here it is again, hope this is more clear I am using the following data (only a small subset is given): Habitat Fungus.yield Birch 20.83829053 Birch 22.9718181 Birch 22.28216829 Birch 24.23136797 Birch 22.32147961 Birch 20.30783598 Oak 27.24047258 Oak 29.7730014 Oak 30.12608508 Oak 25.76088669 Oak 30.14750974 Hornbeam 17.05307949 Hornbeam 15.32805111 Hornbeam 18.26920177 Hornbeam 21.30987049 Hornbeam 21.7173223 I am using the multcomp package to do multiple comparisons as follows library(multcomp) # loads the package fungus-read.table(fungi.txt, Header=T)# Reads the data from file saved as fungi.txt fungus.cirec-simint(Fungus.yield~Habitat, data=fungus,conf.level=0.95,type =c(Tukey)) # Computes cimultaneous intervals using Tukey's method plot(fungus.cirec) # plots the data The plot function plots all the comparisons, I want to plot only part of the data since it clutters the graph. How do I plot only part of the data ? How do I tell it to mark the significant comparisons? How do I get rid of the field names in the plot? For eg. The plot labels are HabitatBirch-HabitatOak, I want it to be labeled as Birch-Oak. Hope I have posted it according to the guidelines, let me know otherwise. Cheers .../Murli __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Multcomp
Here is a minor simplication: my.hmtest - structure(list( estimate = t(t(out.data.mat[,estimate,drop=FALSE])), conf.int = out.data.mat[,2:3], ctype = Dunnett), class = hmtest) plot(my.hmtest) On 7/26/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Look through multcomp:::plot.hmtest to find out which components of an hmtest object are actually used. Now look at what an hmtest object looks like by doing this dput(Dcirec) or looking through the source of the function that produces hmtest objects. With this information in hand we can construct one from out.data.mat: my.hmtest - structure(list( estimate = t(t(structure(out.data.mat[,estimate], .Names = rownames(out.data.mat, conf.int = out.data.mat[,2:3], ctype = Dunnett), class = hmtest) plot(my.hmtest) Note that this is a bit fragile since changes to the internal representation of hmtest objects could cause your object to cease working although as long as those changes do not affect the three components we are using it should be ok. By the way I hard coded Dunnett above since ctype is not available in out.data.mat . On 7/26/06, Nair, Murlidharan T [EMAIL PROTECTED] wrote: Let me clarify with a simpler example what I want to accomplish library(multcomp) data(recovery) Dcirec-simint(minutes~blanket,data=recovery, conf.level=0.9, alternative=less) out.data.mat - with(Dcirec,data.frame(estimate, conf.int, p.value.raw = c(p.value.raw), p.value.bon, p.value.adj)) I want to generate the same type of plot using out.data.mat that I get by plot(Dcirec) How do I specify the plot method how the data in out.data.mat is to be plotted? I am interested in doing this because, I am running about 1500 different comparisons, which creates 1500 different objects. I need to analyze them and combine significant ones into one plot. -Original Message- From: Greg Snow [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 25, 2006 12:12 PM To: Nair, Murlidharan T Subject: RE: [R] Multcomp Doing: str( fungus.cirec ) Suggests that fungus.cirec$conf.int contains the confidence intervals, you can manually plot the subset that you are intereseted in (and label them whatever you want) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Nair, Murlidharan T Sent: Saturday, July 22, 2006 11:00 AM To: R-help@stat.math.ethz.ch Subject: [R] Multcomp Here it is again, hope this is more clear I am using the following data (only a small subset is given): Habitat Fungus.yield Birch 20.83829053 Birch 22.9718181 Birch 22.28216829 Birch 24.23136797 Birch 22.32147961 Birch 20.30783598 Oak 27.24047258 Oak 29.7730014 Oak 30.12608508 Oak 25.76088669 Oak 30.14750974 Hornbeam 17.05307949 Hornbeam 15.32805111 Hornbeam 18.26920177 Hornbeam 21.30987049 Hornbeam 21.7173223 I am using the multcomp package to do multiple comparisons as follows library(multcomp) # loads the package fungus-read.table(fungi.txt, Header=T)# Reads the data from file saved as fungi.txt fungus.cirec-simint(Fungus.yield~Habitat, data=fungus,conf.level=0.95,type =c(Tukey)) # Computes cimultaneous intervals using Tukey's method plot(fungus.cirec) # plots the data The plot function plots all the comparisons, I want to plot only part of the data since it clutters the graph. How do I plot only part of the data ? How do I tell it to mark the significant comparisons? How do I get rid of the field names in the plot? For eg. The plot labels are HabitatBirch-HabitatOak, I want it to be labeled as Birch-Oak. Hope I have posted it according to the guidelines, let me know otherwise. Cheers .../Murli __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Overplotting: plot() invocation looks ugly ... suggestions?
With the lattice package it would be done like this (where the panel.points function places large red pluses on the plot): xyplot(Consumption ~ Quarter, group = Year, data, type = o) trellis.focus(panel, 1, 1) panel.points(1:4, mean.per.quarter, pch = +, cex = 2, col = red) trellis.unfocus() On 7/26/06, John McHenry [EMAIL PROTECTED] wrote: Hi Hadley, Thanks for your suggestion. The description of ggplot states: Description: ... It combines the advantages of both base and lattice graphics ... and you can still build up a plot step by step from multiple data sources So I thought I'd try to enhance the plot by adding in the means from each quarter (this is snagged directly from ESS): qplot(Quarter, Consumption, data=data, type=c(point,line), id=data$Year) ( mean.per.quarter- with(data, tapply(Consumption, Quarter, mean)) ) points(mean.per.quarter, pch=+, cex=2.0) qplot(Quarter, Consumption, data=data, type=c(point,line), id=data$Year) ( mean.per.quarter- with(data, tapply(Consumption, Quarter, mean)) ) 1 2 3 4 888.2 709.2 616.4 832.8 points(mean.per.quarter, pch=+, cex=2.0) Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet Now I'm green behind the ears when it comes to R, so I'm guessing that there is some major conflict between base graphics and lattice graphics, which I thought ggplot avoided, given the library help blurb. I'm assuming that there must be a way to add points / lines to lattice / ggplot graphics (in the latter case it seems to be via ggpoint, or some such)? But is there a way that allows me to add via: points(mean.per.quarter, pch=+, cex=2.0) and similar, or do I have to learn the lingo for lattice / ggplot? Thanks, Jack. hadley wickham [EMAIL PROTECTED] wrote: And if lattice is ok then try this: library(lattice) xyplot(Consumption ~ Quarter, group = Year, data, type = o) Or you can use ggplot: install.packages(ggplot) library(ggplot) qplot(Quarter, Consumption, data=data,type=c(point,line), id=data$Year) Unfortunately this has uncovered a couple of small bugs for me to fix (no automatic legend, and have to specify the data frame explicitly) The slighly more verbose example below shows you what it should look like. data$Year - factor(data$Year) p - ggplot(data, aes=list(x=Quarter, y=Consumption, id=Year, colour=Year)) ggline(ggpoint(p), size=2) Regards, Hadley - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] How to get the name of the first argument in an assignment function?
If you are willing to write fu2[Var] - 3 instead of fu2(Var) - 3 then this workaround may suffice: fu2 - structure(NA, class = fu2) [-.fu2 - function(x, ..., value) { print(match.call()[[3]]); fu2 } # test fu2[Var] - 3 # prints Var On 7/27/06, Heinz Tuechler [EMAIL PROTECTED] wrote: Dear All! If I pass an object to an assignment function I cannot get it's name by deparse(substitute(argument)), but I get *tmp* and I found no way to get the original name, in the example below it should be va1. Is there a way? Thanks, Heinz ## example 'fu1-' - function(var, value) { print(c(name.of.var=deparse(substitute(var} fu1(va1) - 3 name.of.var *tmp* ## desired result: ## name.of.var ##va1 version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status Patched major 2 minor 3.1 year 2006 month 07 day23 svn rev38687 language R version.string Version 2.3.1 Patched (2006-07-23 r38687) __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] how to resample (or resize) matrix?
Assuming the problem is to partition the 10x10 matrix x into 25 two by two squares and then average each of those squares, try this: apply(array(x, c(2,5,2,5)), c(2,4), mean) On 7/27/06, Vladimir Eremeev [EMAIL PROTECTED] wrote: Dear r-help, I have a matrix, suppose, 10x10, and I need the matrix 5x5, having in each cell a mean value of the cells from the initial matrix. Please, point me to a function in R, which can help me doing that. Digging the documentation and mail archives didn't give me a result. Thank you. --- Best regards, Vladimirmailto:[EMAIL PROTECTED] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] deparse(substitute(foo))
See ?match.call On 7/27/06, Armstrong, Whit [EMAIL PROTECTED] wrote: I see that plot.default uses deparse(substitute(x)) to extract the character name of an argument and put it on the vertical axis. Hence: foo - 1:10 plot( foo ) will put the label foo on the vertical axis. However, for a function that takes a ... list as an input, I can only extract the first argument name: x - 1:10 y - 10:20 foo - function(...) { print(deparse(substitute(...))) } foo(x,y) returns: foo(x,y) [1] x and when I try to convert the list to a local variable and then extract names, that doesn't work either: x - 1:10 y - 10:20 foo - function(...) { x - list(...) print(deparse(substitute(names(x } foo(x,y) returns: foo(x,y) [1] names(list(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), c(10, 11, 12, 13, [2] 14, 15, 16, 17, 18, 19, 20))) Can someone suggest a way to extract the variable names when they are passed as a list via ... ? Thanks, Whit This e-mail message is intended only for the named recipient(s) above. It may contain confidential information. If you are not the intended recipient you are hereby notified that any dissemination, distribution or copying of this e-mail and any attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by replying to this e-mail and delete the message and any attachment(s) from your system. Thank you. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] How to get the name of the first argument in an assignment function?
The complexity of the function should not matter. Here is another example of this technique: http://tolstoy.newcastle.edu.au/R/help/04/06/1430.html On 7/27/06, Heinz Tuechler [EMAIL PROTECTED] wrote: At 06:10 27.07.2006 -0400, Gabor Grothendieck wrote: If you are willing to write fu2[Var] - 3 instead of fu2(Var) - 3 then this workaround may suffice: fu2 - structure(NA, class = fu2) [-.fu2 - function(x, ..., value) { print(match.call()[[3]]); fu2 } # test fu2[Var] - 3 # prints Var Thank you, Gabor for your response. My example was very reduced, just to show the point, but in the way I would like to use it, probably your solution may be difficult to apply. Seems, I have to accept that I cannot solve it. Thanks again, Heinz On 7/27/06, Heinz Tuechler [EMAIL PROTECTED] wrote: Dear All! If I pass an object to an assignment function I cannot get it's name by deparse(substitute(argument)), but I get *tmp* and I found no way to get the original name, in the example below it should be va1. Is there a way? Thanks, Heinz ## example 'fu1-' - function(var, value) { print(c(name.of.var=deparse(substitute(var} fu1(va1) - 3 name.of.var *tmp* ## desired result: ## name.of.var ##va1 version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status Patched major 2 minor 3.1 year 2006 month 07 day23 svn rev38687 language R version.string Version 2.3.1 Patched (2006-07-23 r38687) __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] inserting rows into a matrix
Try this where f and A2 are as in your post: out -f(A2[A20]) replace(matrix(0, length(A2), ncol(out)), A2 0, out) On 7/27/06, Robin Hankin [EMAIL PROTECTED] wrote: Hi I have a little vector function that takes a vector A of strictly positive integers and outputs a matrix M each of whose columns is the vector, modified in a complicated combinatorical way. Now I want to generalize the function so that A can include zeroes. Given A, I want to strip out the zeroes, pass it to my function, and pad M with rows at positions corresponding to the zeroes of A. Commented, minimal, self-contained, reproducible toy example follows. f - function(a){cbind(a,a+1,rev(a))} #real function a ghastly nightmare A - 1:5 f(A) a [1,] 1 2 5 [2,] 2 3 4 [3,] 3 4 3 [4,] 4 5 2 [5,] 5 6 1 # f() works as desired. # Now introduce A2, that includes zeroes. In my application, f(A2) would fail because of the zeroes. A2 - c(1,0,0,2,4,0,3) I can strip the zeroes out and call f(): f(A2[A20]) a [1,] 1 2 3 [2,] 2 3 4 [3,] 4 5 2 [4,] 3 4 1 which is fine. How to put the zeroes back in in the appropriate rows and get the following: cbind(c(1,0,0,2,4,0,3),c(2,0,0,3,5,0,4),c(3,0,0,4,2,0,1)) [,1] [,2] [,3] [1,]123 [2,]000 [3,]000 [4,]234 [5,]452 [6,]000 [7,]341 anyone? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Vector extracted from a matrix. How can I specify dimensions in as.matrix?
Use the notation x[ , 1, drop = FALSE] See ?[ On 7/27/06, Neuro LeSuperHéros [EMAIL PROTECTED] wrote: Transpose vector extracted from a matrix Hello, I am doing a recursive analysis that uses every line (vector) of a matrix in a loop. In the model, I need to transpose those vectors that are extracted from a matrix. Using simple vectors (no matrix involved) the transpose function works fine: simplevector -matrix(1:3,3,1) tsimplevector -t(simplevector) #transposed dim(simplevector) #3x1 matrix (vector) dim(tsimplevector)#1x3 PROBLEM: However, when the vector is extracted from a matrix, its dimension is NULL. In this case the transposed dimension is correct, but you'll see the next example, that if a row was extracted, the dimension would be wrong: initialmatrix - matrix(1:9,3,3) extractedvector -initialmatrix[,1] # extract first columm as vector textractedvector -t(extractedvector)#transposed dim(extractedvector) #NULL - not working dim(textractedvector)#1x3 as expected I have tried to transform the extracted vector as.vector and as.matrix. as.vector does not give the what I want (still NULL) and as.matrix can't specify the number of row and columns so both vectors are vertical. In this example, I extract a column and a row. Notice that both as.matrix(vector) have the same dimension. Notice that both transposed vectors have 1x3 dimensions. initialmatrix - matrix(1:9,3,3) extractedvector3x1 -initialmatrix[,1] # extract first columm as vector extractedvector1x3 -initialmatrix[1,] # extract first row as vector #Both are NULL dim(extractedvector3x1) #NULL dim(extractedvector1x3) #NULL #transposed dimensions both 1x3 dim(t(extractedvector3x1)) #1x3 dim(t(extractedvector1x3)) #1x3 - not 3x1 #as.vector: still NULL dim(as.vector(extractedvector3x1)) #NULL dim(as.vector(extractedvector1x3)) #NULL #as.matrix: Both dim at 3x1 dim(as.matrix(extractedvector3x1)) #3x1 dim(as.matrix(extractedvector1x3)) #3x1 - Problem: not 1x3 #as.matrix transposed: Both dim at 1x3 dim(as.matrix(t(extractedvector3x1))) #1x3 dim(as.matrix(t(extractedvector1x3))) #1x3 - Problem: not 3x1 How can I get correct dimensions? Is there a way to specify dimensions in as.matrix? Neuro __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] greek letters, text, and values in labels
Try this where gr and theta are as in your post: xyplot(1~1|gr, main = as.expression(bquote(theta == .(theta))), strip = strip.custom(factor.levels = expression(theta, beta)) ) On 7/27/06, Valentin Todorov [EMAIL PROTECTED] wrote: Unfortunately this does not work for lattice graphics. In such case I do something like the following, but I still do not know how to plot Greek letters in the panel titles: theta - 2.1 gr - as.factor(c(1,2)) levels(gr)[1]-Group 1 levels(gr)[2]-Group 2 library(lattice) print(xyplot(1~1|gr, xlab=eval(substitute(expression(paste(theta, = , tval)), list(tval=theta))), ylab=eval(substitute(expression(paste(theta, = , tval)), list(tval=theta))), main=eval(substitute(expression(paste(Results for ,theta, = , tval)), list(tval=theta))), sub=eval(substitute(expression(paste(theta, = , tval)), list(tval=theta))) )) best, valentin __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Moving from Splus to R - advice/opinions request from a management perspective
How large is your data? If its large you may or may not have problems. If its small you probably won't. Try prototyping the most data intensive portion in R before you commit significant resources. S Plus can time stamp objects and R cannot although you could come up with some workarounds for specific objects if your app uses this. If your app naturally divides into independent sections you could consider an incremental approach converting one section over at a time. On 7/27/06, Dave [EMAIL PROTECTED] wrote: Hi, I've looked through the archives and seen several posts discussing technical differences between R and S(plus). It appears to me that R can likely functionally replace Splus for my situation, but I'm more interested in looking at the risks and benefits of moving from Splus to R from a (project) management point of view. Background (a bit wordy, I'm afraid): - I'm not a stats guy, but rather a project manager responsible for an internal application that utilizes Splus as a back-end analysis engine to analyze manufacturing data at a med/large company. - I really don't know why Splus was chosen as the analysis engine for this app - that choice was made long before I inherited the project. - The developer currently in charge of the Splus code is not a stats guy either, but he is a very talented programmer that has been to one Insightful course and taught himself enough to maintain the existing code (the original authors are long gone). - While there is quite a large quantity of existing code that is used by our application, I don't believe that it is terribly complex, from an applied statistics point of view. Using Splus may be over-kill from a functionality standpoint, but we just haven't had the time to re-write in a more appropriate package - even if we knew what that more appropriate package might be. - With the imminent release of Splus 8, I'm feeling uncomfortable with the risk associated with remaining on Splus 2000, which we are currently using. ***Comments on this point would be appreciated.*** - The developer was able to modify the existing code to run in Splus 7. Unfortunately, the code is significantly slower than before, and Insightful claims this is due to a change to the S language (at version 6) that was out of their control (and one reason that they bought the S language rights). - We have found Insightful's telephone support to be rather unresponsive, and not very helpful, other than to recommend their consulting services. Obtaining consulting services from Insightful to improve performance has proved challenging (don't ask), and if we ever actually do receive any consulting services from them, it will no doubt be quite expensive. (if you are still with me, thank you) I see our realistic options as: A) Stick with Splus with the assumption that eventually, Insightful will help us migrate our code to the latest release, and performance will be comparable to what it is today. The advantage of this is working with a known entity, if not one we are very pleased with. In addition, if we can get the relationship to work, we can hopefully outsource future statistics development and support to Insightful. The disadvantage is that it is costing us quite a bit of $$$ to maintain a relationship that we are not really happy with. Incidentally, I'm not intending to bash Insightful here - it may just be that we are not a good fit for each other. B) Drop Splus for R with the assumption that migrating to R and rewriting to improve performance will be little more difficult than rewriting by ourselves in Splus. The obvious advantage is the cost savings, which is very significant. In addition, based on the archives I've read, it appears that there is a very responsive and helpful R community. The disadvantage is that we may be committing to maintaining statistical expertise in-house, whereas we were hoping to be able to outsource some or all of it (to Insightful). In addition, we will be leaving behind an entity that has a mailing address, phone number, and stock symbol for one that is represented only by a mailing list - I'm rather conservative and risk averse. C) Stick with Splus and either find some consulting help outside of Insightful or slog away internally. I suppose that we could even end our MS contract with Insightful and just continue to use Splus, knowing that we will never receive any releases newer than 8.x nor any future support. (almost done) So, I'd like to hear opinions of whether you think that the benefits of moving to R outweigh the risks. Listing additional benefits and risks that I have not identified would be appreciated. I'm particularly interested in hearing from anyone that has made this same S - R transition in a business environment. Also, does anyone have any recommendations for either Splus or
Re: [R] how to skip certain rows when reading data
Just read them in and throw them away: read.table(myfile.dat, ...whatever...)[-c(10, 12), ] On 7/27/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Dear all, I am reading the data using read.table. However, there are a few rows I want to skip. How can I do that in an easy way? Suppose I know the row number that I want to skip. Thanks so much! __ R-help@stat.math.ethz.ch 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.
Re: [R] Non-interpreted strings
Enter this at the console x - scan(what = ) and after pressing the enter after the right paren, do a paste and then press enter twice. On 7/27/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am new to R, so please forgive me if there is an obvious answer to this question. I have done fairly extensive searching through R docs, google and a few R users and have not found an answer to my question. Is there a way to create a non-interpreted string object in R? For example, I am using R in a MS Windows environment and I would like to paste DOS paths into some R command: setwd(c:\some\directory) Obviously this does not work because of the escaping mechanism. And I know the obvious answer, use \\. But if you do a lot of pasting into R it could get tedious manually editing escape sequences. I did find a workable solution to this particular problem: setwd(choose.dir())EnterPasteEnter This saves me from having to do the editing myself. I can conceive of other examples of wanting to paste other more abstract stings into R that may happen to have a \ in it. And now, thanks to choose.dir(), I have a way to do the translation automagically but... My question is, is there any way in R to not interpret the string and store the string as is? For instance, Perl allows you to do interpreted ( ) and non-interpreted strings (' '). This does not work in R; ' ' acts just like and my testing indicates that the interpretation is done at parse time. Is there any language level construct for creating a non-interpreted string in R? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Non-interpreted strings
Or even easier, x - scan(clipboard, what = ) at least on Windows. On 7/28/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Enter this at the console x - scan(what = ) and after pressing the enter after the right paren, do a paste and then press enter twice. On 7/27/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am new to R, so please forgive me if there is an obvious answer to this question. I have done fairly extensive searching through R docs, google and a few R users and have not found an answer to my question. Is there a way to create a non-interpreted string object in R? For example, I am using R in a MS Windows environment and I would like to paste DOS paths into some R command: setwd(c:\some\directory) Obviously this does not work because of the escaping mechanism. And I know the obvious answer, use \\. But if you do a lot of pasting into R it could get tedious manually editing escape sequences. I did find a workable solution to this particular problem: setwd(choose.dir())EnterPasteEnter This saves me from having to do the editing myself. I can conceive of other examples of wanting to paste other more abstract stings into R that may happen to have a \ in it. And now, thanks to choose.dir(), I have a way to do the translation automagically but... My question is, is there any way in R to not interpret the string and store the string as is? For instance, Perl allows you to do interpreted ( ) and non-interpreted strings (' '). This does not work in R; ' ' acts just like and my testing indicates that the interpretation is done at parse time. Is there any language level construct for creating a non-interpreted string in R? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
[R] placing rectangle behind plot
I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() __ R-help@stat.math.ethz.ch 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.
Re: [R] placing rectangle behind plot
The reason I explicitly specified in the problem that the rectangle should not be drawn first is that the xyplot is issued as part of a larger routine that I don't want to modify. On 7/29/06, Sebastian P. Luque [EMAIL PROTECTED] wrote: Hi Gabor, On Sat, 29 Jul 2006 17:20:29 -0400, Gabor Grothendieck [EMAIL PROTECTED] wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? If you only need to draw the rectangle behind the points, why not 'panel.polygon' before 'panel.xyplot'? xyplot(x ~ x | gl(2, 1), layout=1:2, panel=function(x, y, ...) { panel.polygon(c(3, 3, 8, 8), c(0, 12, 12, 0), col=2) panel.xyplot(x, y, ...) }) Cheers, -- Seb __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] overlaying the values of tab-delim file in to a pre-existing matrix
Please provide reproducible examples (as discussed at end of each posting): Lines - Apple S 21.0 Apple A 21.6 Apple O 43.0 Orange A 45.0 Orange O 64.0 Orange S 32.5 Mango M 40.3 Mango A 32.6 Mango S 24.6 tb - read.table(textConnection(Lines)) # alternative 1 - create a matrix tmat - matrix(0, nrow = nlevels(tb$V2), ncol = nlevels(tb$V1), dimnames = list(levels(tb$V2), levels(tb$V1))) tmat[cbind(tb$V2, tb$V1)] - tb$V3 # alternative 2 - out is a data frame in wide format out - reshape(tb, dir = wide, timevar = V1, idvar = V2) # fix up rownames and colnames and remove first column rownames(out) - out[,1] out - out[,-1] colnames(out) - sub(.*[.], , colnames(out)) out[is.na(out)] - 0 On 7/30/06, Srinivas Iyyer [EMAIL PROTECTED] wrote: Hello : I have matrix with dimensions(200 X 20,000). I have another file, a tab-delim file where first column variables are row names and second column variables are column names. Tab-delim file has smaller values than the matrix. Matrix = tmat tab-delim file read as data.frame = tb My aim is to read in a line in # Apple, S , 21. Find column Apple and row S and fill the value 21. For instance: tmat Apple Orange Mango Grape Star A 0 0 0 00 O 0 0 0 00 M 0 0 0 00 G 0 0 0 00 S 0 0 0 00 tb # tab- delim file read as a data.frame V1 V2 V3 1 Apple S 21 2 Apple A 21.6 3 Apple O 43 4 Orange A 45 5 Orange O 64 6 Orange S 32.5 7 Mango M 40.3 8 Mango A 32.6 9 Mango S 24.6 Now I have to fill in the values in tb (V3) into tmat. For instance, (Apple, S) pair value is 21, I want Apple Orange Mango Grape Star A 21.6 0 0 00 O 0 0 0 00 M 0 0 0 00 G 0 0 0 00 S 21 0 0 00 tbm - as.matrix(tb) tmat[cbind(match(tbm[,2],rownames(tmat)),match(tbm[,1],colnames(tmat)))] -tbm[,3] Error: NAs are not allowed in subscripted assignments I am using R.2.2.1 on a Dell Latutite windows XP with 1GB RAM. Could any one please help me whats wrong with above code. thank you. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] main= bquote(paste(Results for , beta, 3, ==.(b1)))) doesn't work.
I assume the 3 is supposed to be a subscript. Try this: b1 - x - y - 1 plot(x,y, main = bquote(Results for ~ beta[3] ==.(b1))) On 7/30/06, Marco Boks [EMAIL PROTECTED] wrote: Hi, I need to plot the beta as the symbol, followed by the index 3 as the title of a graph. This code works main= bquote(paste(Results for , beta ==.(b1)) but I also need the index 3. I tried (simplified): plot(x,y, main= bquote(paste(Results for , beta, 3, ==.(b1 and a few other versions, but I can not get it to run properly. Any help would be greatly appreciated, Thanks Marco [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] placing rectangle behind plot
Thanks. That's helpful. I would be interested in the case where 1. one does not have a variable latticeplot, as per your example, but just has the output of xyplot(x ~ x | gl(2,1), layout = 1:2) sitting on the screen, having been printed by a prior function. We can assume that no other graphics have been issued since then. Can one still create a grey rectangle behind the lower panel? 2. In fact, ideally what I would like is to create a function, put.in.bg, say, that works something like this: xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) put.in.bg(grid.rect(w = 0.5)) trellis.unfocus() or maybe xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus.bg(panel, 1, 1) grid.rect(w = 0.5) trellis.unfocus() That allows one to add objects to a lattice panel behind the objects that are already there. This would also be helpful for adding grid lines afterwards or other lines, rectangles, etc. On 7/30/06, Paul Murrell [EMAIL PROTECTED] wrote: Hi Gabor Grothendieck wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? Correct. At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() The user-interface is a little rough, but this can be done by accessing the underlying grid objects. Here's an example, with explanatory bits interspersed ... # grab the lattice plot as a grid gTree # There are warnings, but they are ignorable latticeplot - grid.grabExpr(print(xyplot(x ~ x | gl(2,1), layout = 1:2))) # Demonstrate that the gTree faithfully replicates the # original lattice plot (not necessary, just to to what's going on) grid.newpage() grid.draw(latticeplot) # Explore the gTree (just to to show what's going on) # Better user-interface would be nice here ... childNames(latticeplot) # Identify which children are which # (appropriate grob names would be nice here) lapply(latticeplot$children, class) # Identify where each child is drawn latticeplot$childrenvp lapply(latticeplot$children, [[, vp) # Add a rect (starts off on top of everything else) # NOTE that rect has to have correct vpPath plotwithrect - addGrob(latticeplot, rectGrob(w = .5, gp = gpar(fill = light grey), vp=vpPath(plot1.toplevel.vp, plot1.panel.1.1.vp))) # Check this draws what we expect (just to show what's going on) grid.newpage() grid.draw(plotwithrect) # Reorder children to put rect at back # Appropriate user-interface would be nice here ... nc - length(plotwithrect$childrenOrder) plotwithrect$childrenOrder - plotwithrect$childrenOrder[c(nc, 1:(nc - 1))] # Final result grid.newpage() grid.draw(plotwithrect) Paul -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ __ R-help@stat.math.ethz.ch 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.
Re: [R] question about dataframe (sensory) in PLS package
Try: ?sensory str(sensory) dput(sensory) lapply(sensory, class) lapply(sensory, dim) to see what it looks like inside. Seems that sensory is a data frame consisting of two columns each of which is a matrix except that each has a class of AsIs. Thus try this (where I(...) creates objects of class AsIs): mat1 - cbind(a = 1:5, b = 11:15) mat2 - cbind(x = 21:25, y = 31:35) DF - data.frame(A = I(mat1), B = I(mat2)) On 7/30/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Dear all, I am trying to my dataframe for the PLS analysis using the PLS package. However I have some trouble generating the correct dataframe. The main problem is how to use one name to represent several columns in the dataframe. The example dataframe in PLS package is called sensory. I cannot directly read the data file since it's a binary file. If I use names(sensory) command, I will get two names: Quality and Panel. But if I use summary(sensory) command, I will get information of five columns for Quality and 6 columns for Panel (such as Quality.Acidity Quality.Peroxide...). So when I use PLS regression, the function is simply Panel ~ Quality (but it's actually multiple regression). Does anyone know how to build such dataframe? Please share some experience. Really appreciate the help! Sincerely, Jeny __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] placing rectangle behind plot
Just to answer my own question I just discovered trellis.panelArgs() and that can be used to give the following solution: library(lattice) library(grid) x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = 0.5, gp = gpar(fill = light grey)) # re-plot panel over rectangle do.call(panel.xyplot, trellis.panelArgs()) trellis.unfocus() nevertheless, as a point of general interest I would still be interested to know what a general grid-based solution might be. On 7/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Thanks. That's helpful. I would be interested in the case where 1. one does not have a variable latticeplot, as per your example, but just has the output of xyplot(x ~ x | gl(2,1), layout = 1:2) sitting on the screen, having been printed by a prior function. We can assume that no other graphics have been issued since then. Can one still create a grey rectangle behind the lower panel? 2. In fact, ideally what I would like is to create a function, put.in.bg, say, that works something like this: xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) put.in.bg(grid.rect(w = 0.5)) trellis.unfocus() or maybe xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus.bg(panel, 1, 1) grid.rect(w = 0.5) trellis.unfocus() That allows one to add objects to a lattice panel behind the objects that are already there. This would also be helpful for adding grid lines afterwards or other lines, rectangles, etc. On 7/30/06, Paul Murrell [EMAIL PROTECTED] wrote: Hi Gabor Grothendieck wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? Correct. At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() The user-interface is a little rough, but this can be done by accessing the underlying grid objects. Here's an example, with explanatory bits interspersed ... # grab the lattice plot as a grid gTree # There are warnings, but they are ignorable latticeplot - grid.grabExpr(print(xyplot(x ~ x | gl(2,1), layout = 1:2))) # Demonstrate that the gTree faithfully replicates the # original lattice plot (not necessary, just to to what's going on) grid.newpage() grid.draw(latticeplot) # Explore the gTree (just to to show what's going on) # Better user-interface would be nice here ... childNames(latticeplot) # Identify which children are which # (appropriate grob names would be nice here) lapply(latticeplot$children, class) # Identify where each child is drawn latticeplot$childrenvp lapply(latticeplot$children, [[, vp) # Add a rect (starts off on top of everything else) # NOTE that rect has to have correct vpPath plotwithrect - addGrob(latticeplot, rectGrob(w = .5, gp = gpar(fill = light grey), vp=vpPath(plot1.toplevel.vp, plot1.panel.1.1.vp))) # Check this draws what we expect (just to show what's going on) grid.newpage() grid.draw(plotwithrect) # Reorder children to put rect at back # Appropriate user-interface would be nice here ... nc - length(plotwithrect$childrenOrder) plotwithrect$childrenOrder - plotwithrect$childrenOrder[c(nc, 1:(nc - 1))] # Final result grid.newpage() grid.draw(plotwithrect) Paul -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ __ R-help@stat.math.ethz.ch 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.
Re: [R] How does biplot.princomp scale its axes?
Its easiest to just check the source. biplot is a generic which calls biplot.princomp which calls biplot.default which in turn calls plot so try this and examine the source: stats:::biplot.default On 7/31/06, Patrick Connolly [EMAIL PROTECTED] wrote: I'm attempting to modify how biplot draws its red vectors (among other things). This is how I've started: Biplot - function(xx, comps = c(1, 2), cex = c(.6, .4)) { ## Purpose: Makes a biplot with princomp() object to not show arrows ## -- ## Arguments: xx is an object made using princomp() ## -- scores - xx$scores[, paste(Comp, comps, sep = .)] loadings - xx$loadings[, paste(Comp, comps, sep = .)] plot(range(scores), range(scores), xlab = , ylab = , xaxt = n, yaxt = n, pch = ) text(scores[,1], scores[,2], rownames(scores), cex = cex[1]) axis(2) axis(1) } I can make part of a biplot using that function with the USArrests data: Biplot(princomp(USArrests, cor = TRUE), c(1,2), cex = c(.6, .4)) Compare that with what we get using biplot.princomp: biplot(princomp(USArrests, cor = TRUE), c(1,2), cex = c(.6, .4)) It seems to me that the y-values are the same in both plots, but some sort of scaling on the x-axis is happening. Something similar seems to happen with the loadings as well. I notice in the documentation for biplot, mention is made of ... many variations on biplots. Would I be doing something inexcusable if I ignored the differences I've noticed here? TIA -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_Middle minds discuss events (:_~*~_:)Small minds discuss people (_)-(_) . Anon ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Global setting for na.rm=TRUE
There is a global option setting for na.action. See ?na.action . That does not completely address your question but might help with lm, glm, etc. You could define your own wrapper functions if you know ahead of time which functions with na.rm= args you need. e.g. my.max = function(..., na.rm = getOption(na.rm)) max(..., na.rm = na.rm) getOption(na.rm) # NULL my.max(1, 2, NA) # 2 options(na.rm = FALSE) my.max(1,2,NA) # NA On 8/1/06, Gorjanc Gregor [EMAIL PROTECTED] wrote: Hello! Is it possible to set na.rm=TRUE in a global way? I'am constantly forgeting on this when performing analyses. I agree that one should be carefull with this when developing some code, but not necesarilly so in data analysis. Lep pozdrav / With regards, Gregor Gorjanc -- University of Ljubljana PhD student Biotechnical FacultyURI: http://www.bfro.uni-lj.si/MR/ggorjan Zootechnical Department mail: gregor.gorjanc at bfro.uni-lj.si Groblje 3 tel: +386 (0)1 72 17 861 SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia, Europe -- One must learn by doing the thing; for though you think you know it, you have no certainty until you try. Sophocles ~ 450 B.C. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Fitting models in a loop
A simple way around this is to pass it as a data frame. In the code below the only change we made was to change the formula from y ~ poly(x, i) to y ~ . and pass poly(x,i) in a data frame as argument 2 of lm: # test data set.seed(1) x - 1:10 y - x^3 + rnorm(10) # run same code except change the lm call mod - list() for (i in 1:3) { mod[[i]] - lm(y ~., data.frame(poly(x, i))) print(summary(mod[[i]])) } After running the above we can test that it works: for(i in 1:3) print(formula(mod[[i]])) y ~ X1 y ~ X1 + X2 y ~ X1 + X2 + X3 On 8/1/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Markus Gesmann writes: Murray, How about creating an empty list and filling it during your loop: mod - list() for (i in 1:6) { mod[[i]] - lm(y ~ poly(x,i)) print(summary(mod[[i]])) } All your models are than stored in one object and you can use lapply to do something on them, like: lapply(mod, summary) or lapply(mod, coef) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Subject: Re: [R] Fitting models in a loop Murray, Here is a general paradigm I tend to use for such problems. It extends to fairly general model sequences, including different responses, c First a couple of tiny, tricky but useful functions: subst - function(Command, ...) do.call(substitute, list(Command, list(...))) abut - function(...) ## jam things tightly together do.call(paste, c(lapply(list(...), as.character), sep = )) Name - function(...) as.name(do.call(abut, list(...))) Now the gist. fitCommand - quote({ MODELi - lm(y ~ poly(x, degree = i), theData) print(summary(MODELi)) }) for(i in 1:6) { thisCommand - subst(fitCommand, MODELi = Name(model_, i), i = i) print(thisCommand) ## only as a check eval(thisCommand) } At this point you should have the results and objects(pat = ^model_) should list the fitted model objects, all of which can be updated, summarised, plotted, c, because the information on their construction is all embedded in the call. Bill. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Murray Jorgensen Sent: Tuesday, 1 August 2006 2:09 PM To: r-help@stat.math.ethz.ch Subject: [R] Fitting models in a loop If I want to display a few polynomial regression fits I can do something like for (i in 1:6) { mod - lm(y ~ poly(x,i)) print(summary(mod)) } Suppose that I don't want to over-write the fitted model objects, though. How do I create a list of blank fitted model objects for later use in a loop? Murray Jorgensen -- __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Fitting models in a loop
Actually in thinking about this some more that still gets you into a mess if you want to do prediction at anything other than the original points. On 8/1/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: A simple way around this is to pass it as a data frame. In the code below the only change we made was to change the formula from y ~ poly(x, i) to y ~ . and pass poly(x,i) in a data frame as argument 2 of lm: # test data set.seed(1) x - 1:10 y - x^3 + rnorm(10) # run same code except change the lm call mod - list() for (i in 1:3) { mod[[i]] - lm(y ~., data.frame(poly(x, i))) print(summary(mod[[i]])) } After running the above we can test that it works: for(i in 1:3) print(formula(mod[[i]])) y ~ X1 y ~ X1 + X2 y ~ X1 + X2 + X3 On 8/1/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Markus Gesmann writes: Murray, How about creating an empty list and filling it during your loop: mod - list() for (i in 1:6) { mod[[i]] - lm(y ~ poly(x,i)) print(summary(mod[[i]])) } All your models are than stored in one object and you can use lapply to do something on them, like: lapply(mod, summary) or lapply(mod, coef) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Subject: Re: [R] Fitting models in a loop Murray, Here is a general paradigm I tend to use for such problems. It extends to fairly general model sequences, including different responses, c First a couple of tiny, tricky but useful functions: subst - function(Command, ...) do.call(substitute, list(Command, list(...))) abut - function(...) ## jam things tightly together do.call(paste, c(lapply(list(...), as.character), sep = )) Name - function(...) as.name(do.call(abut, list(...))) Now the gist. fitCommand - quote({ MODELi - lm(y ~ poly(x, degree = i), theData) print(summary(MODELi)) }) for(i in 1:6) { thisCommand - subst(fitCommand, MODELi = Name(model_, i), i = i) print(thisCommand) ## only as a check eval(thisCommand) } At this point you should have the results and objects(pat = ^model_) should list the fitted model objects, all of which can be updated, summarised, plotted, c, because the information on their construction is all embedded in the call. Bill. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Murray Jorgensen Sent: Tuesday, 1 August 2006 2:09 PM To: r-help@stat.math.ethz.ch Subject: [R] Fitting models in a loop If I want to display a few polynomial regression fits I can do something like for (i in 1:6) { mod - lm(y ~ poly(x,i)) print(summary(mod)) } Suppose that I don't want to over-write the fitted model objects, though. How do I create a list of blank fitted model objects for later use in a loop? Murray Jorgensen -- __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Fitting models in a loop
Here is another attempt. This one allows general prediction yet its actually shorter and does not use any advanced language constructs (although to understand why it works one must understand that formulas have environments and the environment of the formula corresponding to each component of mod is the environment within the anonymous function instance that created it): # test data - as before set.seed(1) x - 1:10 y - x^3 + rnorm(10) mod - lapply(1:3, function(i) lm(y ~ poly(x,i))) print(mod) # test - each component of mod remembers its 'i' # This returns 1, 2 and 3 as required. for (j in 1:3) print(environment(formula(mod[[j]]))$i) # following two lines give same answer # showing prediction works predict(mod[[2]], list(x = 1:10)) fitted(lm(y ~ poly(x,2))) On 8/1/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Actually in thinking about this some more that still gets you into a mess if you want to do prediction at anything other than the original points. On 8/1/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: A simple way around this is to pass it as a data frame. In the code below the only change we made was to change the formula from y ~ poly(x, i) to y ~ . and pass poly(x,i) in a data frame as argument 2 of lm: # test data set.seed(1) x - 1:10 y - x^3 + rnorm(10) # run same code except change the lm call mod - list() for (i in 1:3) { mod[[i]] - lm(y ~., data.frame(poly(x, i))) print(summary(mod[[i]])) } After running the above we can test that it works: for(i in 1:3) print(formula(mod[[i]])) y ~ X1 y ~ X1 + X2 y ~ X1 + X2 + X3 On 8/1/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Markus Gesmann writes: Murray, How about creating an empty list and filling it during your loop: mod - list() for (i in 1:6) { mod[[i]] - lm(y ~ poly(x,i)) print(summary(mod[[i]])) } All your models are than stored in one object and you can use lapply to do something on them, like: lapply(mod, summary) or lapply(mod, coef) I think it is important to see why this deceptively simple solution does not achieve the result that Murray wanted. Take any fitted model object, say mod[[4]]. For this object the formula component of the call will be, literally, y ~ poly(x, i), and not y ~ poly(x, 4), as would be required to use the object, e.g. for prediction. In fact all objects have the same formula. You could, of course, re-create i and some things would be OK, but getting pretty messy. You would still have a problem if you wanted to plot the fit with termplot(), for example, as it would try to do a two-dimensional plot of the component if both arguments to poly were variables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 01 August 2006 06:16 To: [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Subject: Re: [R] Fitting models in a loop Murray, Here is a general paradigm I tend to use for such problems. It extends to fairly general model sequences, including different responses, c First a couple of tiny, tricky but useful functions: subst - function(Command, ...) do.call(substitute, list(Command, list(...))) abut - function(...) ## jam things tightly together do.call(paste, c(lapply(list(...), as.character), sep = )) Name - function(...) as.name(do.call(abut, list(...))) Now the gist. fitCommand - quote({ MODELi - lm(y ~ poly(x, degree = i), theData) print(summary(MODELi)) }) for(i in 1:6) { thisCommand - subst(fitCommand, MODELi = Name(model_, i), i = i) print(thisCommand) ## only as a check eval(thisCommand) } At this point you should have the results and objects(pat = ^model_) should list the fitted model objects, all of which can be updated, summarised, plotted, c, because the information on their construction is all embedded in the call. Bill. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Murray Jorgensen Sent: Tuesday, 1 August 2006 2:09 PM To: r-help@stat.math.ethz.ch Subject: [R] Fitting models in a loop If I want to display a few polynomial regression fits I can do something like for (i in 1:6) { mod - lm(y ~ poly(x,i)) print(summary(mod)) } Suppose that I don't want to over-write the fitted model objects, though. How do I create a list of blank fitted model objects for later use in a loop? Murray Jorgensen -- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help
Re: [R] open DLL in R
This refers to the windows command pedump.exe found in the Rtools collection at: http://www.murdoch-sutherland.com/Rtools/ On 8/2/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hello Sir, I am just wondering that pedump is a command of 'R' because in could not find in the 'R' help using help.search(pedump). I am requesting you to narrate as i also have to look into .dll(s). Is there any way to know what are the exported functions and constants and imported functions and constants in a easy way. thanks -gaurav. Prof Brian Ripley [EMAIL PROTECTED] Sent by: [EMAIL PROTECTED] 01-08-06 11:18 PM To qian li [EMAIL PROTECTED] cc r-help@stat.math.ethz.ch Subject Re: [R] open DLL in R On Tue, 1 Aug 2006, qian li wrote: I have downloaded a DLL file. I want to look at the contents in the DLL file. How can I do it in R? You need a disassembler such as VC++'s DUMPBIN, but looking at compiled code you did not write is not an easy task. (Or objdump from the MinGW toolset.) If only you want to know what entry points it exports, use pedump -e for the pedump.exe in tools.zip (see the R-admin manual). What has this to do with R? Thanks, QL - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch 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. DISCLAIMER AND CONFIDENTIALITY CAUTION:\ \ This message and ...{{dropped}} __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] loop, pipe connection, quote/unquote
Try something along these lines assuming that the current directory is \Program Files\R\R-2.3.1pat. Note use of paste to create the command line to pass to pipe: # search for indicated string in each of the files and # for each match output the file name Files - c(CHANGES, COPYING, NEWS, NEWS) for(f in Files) print(readLines(pipe(paste(findstr /m out-of-range, f On 8/2/06, Marco Grazzi [EMAIL PROTECTED] wrote: Hi all, I have the following problem. Inside R, I am trying to run a loop on several files. The data are stored in these files in a peculiar way, thus, at the same time I load the data, I would like to invoke a utility. I do this with pipe. (The utility I am using is gbget from the package gbutils. It works correctly from shell, and it is not the problem.) The problem is that from within pipe I do not know how to have the loop running. I guess I should manage to unquote the quotation mark inside the pipe for the loop to run, but I do not how to do it. In the following examples, for instance, I would like pipe to interpret the i as the file.names of the loop specified above. # specify the files on which I want to have the loop running file.names - system(ls ???.gz, intern=TRUE) # Start loop for(i in file.names){ dati - read.table(pipe(gbget 'i[160](1)' '[37](1)' '[145](1)' |gbget '()D' )) # [...] some statistical analysis follows [...] } Thanks for your help (hoping I manged to be enough clear), marco -- Marco Grazzi - PhD Candidate in Economics and Management LEM-Sant'Anna School of Advanced Studies Piazza Martiri della Liberta', 33 56127 Pisa, Italy Tel. +39-050-883365 Fax +39-050-883344 Web site: https://mail.sssup.it/~grazzi __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] How to share variables
On 8/2/06, Sergio Martino [EMAIL PROTECTED] wrote: Hi, I would like to realize in R a structure like the fortran common ie a way to declare some variable that can only be accessed by all the functions which need to. Browsing the archive it seems that the simplest way is to declare the variables and the functions in a big function which wraps all. But this is impratical when the functions are big. There is a demonstration of that found by issuing the command: demo(scoping) The environments seems to do the trick but I am not enough familiar with them to make my ways out. Yes place your data in an environment as shown and then for each function that is to access the environment should have its environment set accordingly: e - new.env() e$dat - 1:3 myfun - function(x) sum(x + dat) environment(myfun) - e myfun(10) # fun can access dat Realize that what you are trying to do is to create a sort of object oriented structure with the data being the objects and the functions being the methods. The proto package provides some functionality to implement that and also supports delegation (similar to inheritance): library(proto) package?proto # all sources of info on proto # example - create proto object p with some data dat and a method fun p - proto(dat = 1:3, fun = function(., x) sum(x + .$dat)) # invoke method p$fun(10) # runs fun. fun has access to dat # create a child q of p and run fun # q overrides dat with its own dat while inheriting fun q - p$proto(dat = 4:6) q$fun(10) Another possibility would be to look at the R.oo package which is another object oriented infrastructure based on environments. Is there any example or pointers to easy but complete environment usage? Thanks in Advance Sergio Martino __ R-help@stat.math.ethz.ch 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.
Re: [R] ordering columns (longitudinal data in wide format)
Assuming this data: s - structure(list(L.qol.0 = 83, L.qol.0.08 = 86, L.qol.0.17 = 89, L.qol.0.25 = 92, L.qol.0.5 = 91, L.qol.0.42 = 87, L.qol.0.34 = 90), .Names = c(L.qol.0, L.qol.0.08, L.qol.0.17, L.qol.0.25, L.qol.0.5, L.qol.0.42, L.qol.0.34), class = data.frame, row.names = 1) # we can sort it by column names like this: s[,sort(names(s))] # also note that mixed sort in gtools can sort by numeric # value in mixed character/numeric names which gives the # same result here but may not in different examples library(gtools) s[,mixedsort(names(s))] On 8/2/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, I am working on some longitudinal data in wide format and I am having a problem ordering the data columns. To expand, a subset of what I am working on is as follows; s L.qol.0 L.qol.0.08 L.qol.0.17 L.qol.0.25 L.qol.0.5 L.qol.0.42 L.qol.0.34 1 83 86 89 9291 87 90 names(s) [1] L.qol.0L.qol.0.08 L.qol.0.17 L.qol.0.25 L.qol.0.5 [6] L.qol.0.42 L.qol.0.34 # in this object s (not a vector), 'L.qol' is measured at time points 0, 0.08, 0.17, 0.25, 0.34, 0.42 and 0.5. As you can see, however, the time points are not in the correct order in object s. Does anyone know how to order these column names along with their corresponding measurements? Clearly s[order(s)] does not work since this just orders the corresponding measurements. I would be extremely grateful for any help on this matter, it may be really simple, but I have tried for ages. Thank you, Zoe __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] help with formatting legend in xyplot
If you set it through par.settings then it will affect both the drawing and the legend: xyplot(Sepal.Length ~ Petal.Length, iris, groups = Species, auto.key = TRUE, par.settings = list(superpose.symbol = list(pch = *, cex = 1))) On 8/2/06, Kaushik Katari [EMAIL PROTECTED] wrote: I am doing a xyplot: (x~y, groups = z, pch=8, auto.key=T). This changes the symbol in the graph to an asterisk (*), but not in the legend, which is still an open circle. I have found out how to manipulate the position and the color of the letters in the legend, but cannot change the legend symbol to match the symbol in the graph. Could you help? Thanks, Kaushik [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] ggplot facet label font size
On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch 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.
Re: [R] Finding the position of a variable in a data.frame
On 8/3/06, John Kane [EMAIL PROTECTED] wrote: --- Don MacQueen [EMAIL PROTECTED] wrote: You don't need to find out the column index. This works: Df[5,'bat'] - 100 -Don Thanks, I'd tried Df[5, bat] - 100 :( I never thought of the ' ' being needed. Right -- the quotes are not needed if you use $ but they are needed if you use [. __ R-help@stat.math.ethz.ch 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.
Re: [R] ggplot facet label font size
If you are willing to use grid then you could create only the sex factor in the left strips since its already in the desired position but when displaying it output a factor.level, i.e. label of A. (my.strip.left is modified from the prior post to do that.) Then after the plot is drawn, looping through all grobs looking for those with a label component of A producing a list of grob names, strip.left.names. We then mapply the real factor levels with those grobs editing them in reset.levels(), defined below. (I have used the fact, empirically determined that the stripts are produced in order of the factor levels.) Everything is the same as the last post except my.strip.left which has been modified and everything which comes after the call to histogram. Although this seems to work, maybe Deepayan or Paul can think of something slicker. library(ggplot) # data resides here library(lattice) library(grid) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., factor.levels, horizontal) if (which.given == 1 which.panel[1] == 1) strip.default(which.given, which.panel, factor.levels = LETTERS, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) is.strip.left - function(name) identical(grid.get(name)$label, A) strip.left.names - getNames()[sapply(getNames(), is.strip.left)] reset.levels - function(nam, lev) grid.edit(nam, label = lev) mapply(reset.levels , strip.left.names, levels(tips$smoker)) On 8/3/06, Walker, Sam [EMAIL PROTECTED] wrote: This works OK, but there is some extra spacing between the panels, the top axis and the strip on the top, and the left labels and panel. How can I remove these extra spaces? I've tried changing various layout.widths settings with no luck. It seems the spaces are calculated based on the number of conditioning variables, in this case 2 (sex+smoker). Thanks in advance... -Sam -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 02, 2006 6:04 PM To: Walker, Sam Cc: r-help@stat.math.ethz.ch Subject: Re: [R] ggplot facet label font size On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] ggplot facet label font size
Just sending this to you. One thing that might be easy to do yet give a lot of flexibility is to: 1. put meaningful names on the grobs. Even with just this it would be possible to do a getNames() in grid and then from inspection grid.edit the appropriate one(s). 2. create a routine that retrieves grobs so one does not have to use getNames with grep. trellis.focus and friends do this in lattice. Regards. On 8/3/06, hadley wickham [EMAIL PROTECTED] wrote: Hi Sam, How do I change the font size in the facet labels along the edges of the plot? Unfortunately, you can't currently change the size of those fonts. However, it is on my todo list (as well as completely custom strip functions) and should be available in the near future. One thing you could do is have a look at ggopt, where you can at least change the strip text, if not the size. Regards, Hadley On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots, but the label font size is too large and exceeding the size of the label box. Thanks in advance... -Sam __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Data frame referencing?
When specifying a column name with [ the name must be quoted (unlike when using it with $): a[a$y 0.5, y] - 1 On 8/4/06, Sander Oom [EMAIL PROTECTED] wrote: Dear R users, When you do: x - rnorm(10) y - rnorm(10) z - rnorm(10) a - data.frame(x,y,z) a$x [1] 1.37821893 0.21152756 -0.55453182 -2.10426048 -0.08967880 0.03712110 [7] -0.80592149 0.07413450 0.15557671 1.22165341 Why does this not work: a[a$y0.5,y] -1 Error in [-.data.frame(`*tmp*`, a$y 0.5, y, value = 1) : only 0's may be mixed with negative subscripts While this works: a[a$y0.5,2] -1 a x y z 1 1.37821893 -1.0887363 1.7340522 2 0.21152756 -0.7256467 -1.3165373 3 -0.55453182 1.000 -2.1116072 4 -2.10426048 -0.4898596 -1.5863823 5 -0.08967880 1.000 -0.9139706 6 0.03712110 1.000 -1.3004970 7 -0.80592149 -0.7004193 -0.1958059 8 0.07413450 1.000 -1.3574303 9 0.15557671 -0.3335407 -2.1991236 10 1.22165341 1.000 -0.7576708 For a complex loop I would prefer to reference the right colomn by name, not by number! Now, when the colomns change, I need to check my code to make sure that the right colomns are referenced. Suggestions much appreciated! Thanks in advance, Sander. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] expression() - Superscript in y-axis, keeping line break in string
Use atop: plot(1, main = expression(atop( ^14*C*-glyphosate line, line2))) On 8/4/06, Andrew Kniss [EMAIL PROTECTED] wrote: I've tried several different ways to accomplish this, but as yet to no avail. My y-axis for a plot has a rather long label, and thus I have been using /n to break it into two lines. However, to make it technically correct for publication, I also need to use superscript in the label. For example: par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=14C-glyphosate line1\n line2) will provide the text in two lines as I would like it. However, I am trying to keep those same line breaks when using expression() to get my superscript number. This will not work, as it aligns the 14C section with the bottom line of the expression making little sense to the reader. par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=expression( ^14*C*-glyphosate line1\n line2)) Is there a way to align the 14C portion of the expression with the top line of the string rather than the bottom line? Any suggestions are greatly appreciated. Andrew -- Andrew Kniss Assistant Research Scientist University of Wyoming Department of Plant Sciences [EMAIL PROTECTED] Office: (307) 766-3949 Fax:(307) 766-5549 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] expression() - Superscript in y-axis, keeping line break in string
Sorry, you wanted a ylab=, not a main=. Try using xyplot in lattice: library(lattice) xyplot(1~1, ylab = expression(atop(phantom(0)^14*C*-glyphosate line, line2))) On 8/4/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Use atop: plot(1, main = expression(atop( ^14*C*-glyphosate line, line2))) On 8/4/06, Andrew Kniss [EMAIL PROTECTED] wrote: I've tried several different ways to accomplish this, but as yet to no avail. My y-axis for a plot has a rather long label, and thus I have been using /n to break it into two lines. However, to make it technically correct for publication, I also need to use superscript in the label. For example: par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=14C-glyphosate line1\n line2) will provide the text in two lines as I would like it. However, I am trying to keep those same line breaks when using expression() to get my superscript number. This will not work, as it aligns the 14C section with the bottom line of the expression making little sense to the reader. par(oma=c(0,0,2,0),mar=c(5,6,0.25,2),lheight=1) plot(1:10, ylab=expression( ^14*C*-glyphosate line1\n line2)) Is there a way to align the 14C portion of the expression with the top line of the string rather than the bottom line? Any suggestions are greatly appreciated. Andrew -- Andrew Kniss Assistant Research Scientist University of Wyoming Department of Plant Sciences [EMAIL PROTECTED] Office: (307) 766-3949 Fax:(307) 766-5549 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] ggplot facet label font size
With ggplot its possible to do this too but in that case it seems necessary to recurse through the grobs. Here we look for grobs that have a label component which contains a colon and grid.edit those changing the value of cex. Note that getNames() give a single grob named pretty and we start from that: # run code library(ggplot) library(grid) p - ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) recurse - function(x) { if (!is.null(x$label) regexpr(:, x$label) 0) grid.edit(x$name, gp = gpar(cex = 0.7)) for (ch in x$children) recurse(ch) } recurse(grid.get(pretty)) On 8/3/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: If you are willing to use grid then you could create only the sex factor in the left strips since its already in the desired position but when displaying it output a factor.level, i.e. label of A. (my.strip.left is modified from the prior post to do that.) Then after the plot is drawn, looping through all grobs looking for those with a label component of A producing a list of grob names, strip.left.names. We then mapply the real factor levels with those grobs editing them in reset.levels(), defined below. (I have used the fact, empirically determined that the stripts are produced in order of the factor levels.) Everything is the same as the last post except my.strip.left which has been modified and everything which comes after the call to histogram. Although this seems to work, maybe Deepayan or Paul can think of something slicker. library(ggplot) # data resides here library(lattice) library(grid) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., factor.levels, horizontal) if (which.given == 1 which.panel[1] == 1) strip.default(which.given, which.panel, factor.levels = LETTERS, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) is.strip.left - function(name) identical(grid.get(name)$label, A) strip.left.names - getNames()[sapply(getNames(), is.strip.left)] reset.levels - function(nam, lev) grid.edit(nam, label = lev) mapply(reset.levels , strip.left.names, levels(tips$smoker)) On 8/3/06, Walker, Sam [EMAIL PROTECTED] wrote: This works OK, but there is some extra spacing between the panels, the top axis and the strip on the top, and the left labels and panel. How can I remove these extra spaces? I've tried changing various layout.widths settings with no luck. It seems the spaces are calculated based on the number of conditioning variables, in this case 2 (sex+smoker). Thanks in advance... -Sam -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 02, 2006 6:04 PM To: Walker, Sam Cc: r-help@stat.math.ethz.ch Subject: Re: [R] ggplot facet label font size On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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
Re: [R] formating for dist function
Here are three ways: # read in data Lines - object1 object1 78 object1 object2 45 object1 object3 34 object1 object4 45 object2 object2 89 object2 object3 32 object2 object4 13 DF - read.table(textConnection(Lines)) # 1 - xtabs xt - as.matrix(xtabs(V3 ~., DF)) # 2 - reshape wide - reshape(DF, direction = wide, idvar = V1, timevar = V2) rownames(wide) - wide$V1 colnames(wide) - sub(.*[.], , colnames(wide)) wide - as.matrix(wide[,-1]) # 3 - [ mat - matrix(0, nlevels(DF$V1), nlevels(DF$V2), dimnames = list(levels(DF$V1), levels(DF$V2))) mat[cbind(DF$V1, DF$V2)] - DF$V3 On 8/5/06, Ffenics [EMAIL PROTECTED] wrote: Hi there I have a list that looks like this object1 object1 78 object1 object2 45 object1 object3 34 object1 object4 45 object2 object2 89 object2 object3 32 object2 object4 13 but i want to create a matrix like this in order to use the dist function of R object1 object2 object3 object4 object1 78 45 3445 object245 89 32 13 Is there a method in R that will take a list and format it in this way? Any help much appreciated. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] ordering by a datframe date
Try this: Lines - story,datepub story10,1 April 1999 story 90,1 March 2002 story 37,10 July 1985 DF - read.csv(textConnection(Lines)) DF[order(as.Date(DF$datepub, %d %B %Y)),] On 8/6/06, Bob Green [EMAIL PROTECTED] wrote: I am hoping for some advice regarding ordering a dataframe, by date. The dataframe is in the format below. $story $datepub story10 1 April 1999 story 901 March 2002 story 3710 July 1985 I want to reorder the entire dataframe so the earliest story is first, and save the reordered dataframe. The command, 'class' (datepub) reveals $datepub is a factor variable. I tried the following: d2 - as.character(datepub) rank(d2) Any assistance is appreciated, regards Bob Green __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] classification tables
Also check out CrossTable in the gmodels package. Regarding your other question, assuming we have tab-table(x,y) as in Philippe's post, the fraction of pairs in x and y that match can be calculated via any of these: sum(x==y) / length(x) sum(diag(tab)) / sum(tab) library(e1071) classAgreement(tab) # tab from above sum(diag(prop.table(tab))) On 8/7/06, Philippe Grosjean [EMAIL PROTECTED] wrote: x - c(1,2,3,4,2,3,3,1,2,3) y - c(2,1,3,4,1,3,3,2,2,3) table(x, y) y x 1 2 3 4 1 0 2 0 0 2 2 1 0 0 3 0 0 4 0 4 0 0 0 1 ?table Best, Philippe Grosjean ..°})) ) ) ) ) ) ( ( ( ( (Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( (Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Belgium ( ( ( ( ( .. Taka Matzmoto wrote: Dear R-users I have two vectors. One vector includes true values and the other vector has estimated values. Values are all integers from 1 to 4. For example, x - c(1,2,3,4,2,3,3,1,2,3) y - c(2,1,3,4,1,3,3,2,2,3) I would like to a classfication table x by y. With the table, I would like to calculate what percentage is correct classfication. Which R function do I need to use for creating a 4 * 4 classification table? Thank you. Taka, __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] kmeans and incom,plete distance matrix concern
There are many clustering functions in R and R packages and some take distance objects whereas others do not. You likely read about hclust or some different clustering function. See ?kmeans for the kmeans function and also look at the CRAN Task View on clustering for other clustering functions: http://cran.r-project.org/src/contrib/Views/ On 8/7/06, Ffenics [EMAIL PROTECTED] wrote: well then i dont understand because everything i have read so far suggests that you use the dist() function to create a matrix based on the euclideam distance and then the kmeans() function. If this is incorrect, then any suggestins as to how to do this properly would be much appreciated. Christian Hennig [EMAIL PROTECTED] wrote: First of all, kmeans doesn't work on distance matrices. On Mon, 7 Aug 2006, Ffenics wrote: Hi there I have been using R to perform kmeans on a dataset. The data is fed in using read.table and then a matrix (x) is created i.e: [ mat - matrix(0, nlevels(DF$V1), nlevels(DF$V2), dimnames = list(levels(DF$V1), levels(DF$V2))) mat[cbind(DF$V1, DF$V2)] - DF$V3 This matrix is then taken and a distance matrix (y) created using dist() before performing the kmeans clustering. My query is this: not all the data for the initial matrix (x) exists and therefore the matrix is not fully populated - empty cells are populated with '0's. Could someone please tell me how this may affect the result from the dist() command - because a '0' in a distance matrix means that the two variables are identical doesnt it(?) - but I dont want tthings clustered together simply because there was no information. Is this a problem and are there ways to circumnavigate them? Thanks [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. *** --- *** Christian Hennig University College London, Department of Statistical Science Gower St., London WC1E 6BT, phone +44 207 679 1698 [EMAIL PROTECTED], www.homepages.ucl.ac.uk/~ucakche [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] kmeans and incom,plete distance matrix concern
?kmeans says the following. Note that x is a matrix of ***data***. Also look at the examples at the end of the help page if its still not clear. Usage: kmeans(x, centers, iter.max = 10, nstart = 1, algorithm = c(Hartigan-Wong, Lloyd, Forgy, MacQueen)) Arguments: x: A numeric matrix of data, or an object that can be coerced to such a matrix (such as a numeric vector or a data frame with all numeric columns). On 8/7/06, Ffenics [EMAIL PROTECTED] wrote: Thanks. I had a look at that and it says: Partitioning Clustering: Functionkmeans() from package stats provides several algorithms for computing partitions with respect to Euclidean distance. Hence why I am using a euclidean distance matrix. Why is this incorrect? Gabor Grothendieck [EMAIL PROTECTED] wrote: There are many clustering functions in R and R packages and some take distance objects whereas others do not. You likely read about hclust or some different clustering function. See ?kmeans for the kmeans function and also look at the CRAN Task View on clustering for other clustering functions: http://cran.r-project.org/src/contrib/Views/ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Plots
Try: RSiteSearch(Horses and Hounds) On 8/7/06, Sonal Darbari [EMAIL PROTECTED] wrote: Hi, What commands are needed to get an output like this: 1. On X-Axis : 2 Indices ex. SP500 and DOW JONES 2. Their repective dates If I use the plot command, I get one output if I use it again, I lose the previous output. I need both of them on one graph only(As seen in the attachment). Thanks, Sonal __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Plots
Also RSiteSearch(ts.plot.2Axis) On 8/7/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Try: RSiteSearch(Horses and Hounds) On 8/7/06, Sonal Darbari [EMAIL PROTECTED] wrote: Hi, What commands are needed to get an output like this: 1. On X-Axis : 2 Indices ex. SP500 and DOW JONES 2. Their repective dates If I use the plot command, I get one output if I use it again, I lose the previous output. I need both of them on one graph only(As seen in the attachment). Thanks, Sonal __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Retain only those records from a dataframe that exist in another dataframe
Although this is probably not directly applicable to this problem I might mention here that merge.zoo does support left and right joins and that handles problems similar to this. z3t, z3ft, z3tf and z3f below have times of both unioned, the times of z2, the times of z1 and the times of both z1 and z2 intersected respectively: library(zoo) z1 - zoo(1:5, 1:5) z2 - zoo(2:6, 2:6) z3t - merge(z1, z2, all = TRUE) z3ft - merge(z1, z2, all = c(FALSE, TRUE)) z3tf - merge(z1, z2, all = c(TRUE, FALSE)) z3f - merge(z1, z2, all = FALSE) On 07 Aug 2006 22:14:05 +0200, Peter Dalgaard [EMAIL PROTECTED] wrote: Mark Na [EMAIL PROTECTED] writes: Dear R community, I have two dataframes first and second which share a unique identifier. I wish to make a new dataframe third retaining only the rows in first which also occur in second. I have tried using merge but can't seem to figure it out. Any ideas? Doesn't sound like a merge problem. Will this do it?: first[first$ID %in% second$ID,] -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Pairwise n for large correlation tables?
Try this: # mat is test matrix mat - matrix(1:25, 5) mat[2,2] - mat[3,4] - NA crossprod(!is.na(mat)) On 8/7/06, Adam D. I. Kramer [EMAIL PROTECTED] wrote: Hello, I'm using a very large data set (n 100,000 for 7 columns), for which I'm pretty happy dealing with pairwise-deleted correlations to populate my correlation table. E.g., a - cor(cbind(col1, col2, col3),use=pairwise.complete.obs) ...however, I am interested in the number of cases used to compute each cell of the correlation table. I am unable to find such a function via google searches, so I wrote one of my own. This turns out to be highly inefficient (e.g., it takes much, MUCH longer than the correlations do). Any hints, regarding other functions to use or ways to maket his speedier, would be much appreciated! pairwise.n - function(df=stop(Must provide data frame!)) { if (!is.data.frame(df)) { df - as.data.frame(df) } colNum - ncol(df) result - matrix(data=NA,nrow=colNum,ncol=ncolNum,dimnames=list(colnames(df),colnames(df))) for(i in 1:colNum) { for (j in i:colNum) { result[i,j] - length(df[!is.na(df[i])!is.na(df[j])])/colNum } } result } -- Adam D. I. Kramer University of Oregon __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Netiquette, was Re: ... gfortran and gcc...
I agree. Also, sending a copy to the poster means that they are likely to get it first which seems like a desirable courtesy. On 8/8/06, Marc Schwartz (via MN) [EMAIL PROTECTED] wrote: [Re-sending to the list only for archiving, as my original reply had too many recipients and I cancelled it.] 1. One need not be subscribed to the list to be able to post. Thus, indeed, a poster may not see all postings. 2. On the relatively rare occasion (thanks to Martin) where the server seems to incur delays in sending out posts and replies, copying the original poster on your reply ensures that they will get the reply in a timely fashion. HTH, Marc Schwartz On Tue, 2006-08-08 at 17:41 +0100, Heinz Tuechler wrote: What could be the reason, to respond not only to the list? I did not see an advantage, to receive a response twice, once directly, once by the list. Is it wrong, to assume that someone who writes to the list, does also receive all the postings on the list? Heinz At 08:09 08.08.2006 -0500, Mike wrote: Thank you both. I would prefer to communicate through the list only. Mike. On Tue August 8 2006 04:47, Prof Brian Ripley wrote: On Tue, 8 Aug 2006, Peter Dalgaard wrote: Prof Brian Ripley [EMAIL PROTECTED] writes: First, you replied to the list and not to me, which was discourteous. You mean that he replied to the list *only*, I hope. Yes, and it was written as if to me, and was a reply to an email from me. I usually consider it offensive when people reply to me and not the list (reasons including: It feels like being grabbed by the sleeve, I might not actually be the best source for the answer, and it's withholding the answer from the rest of the subscribers.) We do ask people to copy to the list. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] legend on trellis plot
1. Use the x, y and corner components to the key= list to specify the legend position, and 2. pass the panel.number in the panel function and test that as shown in the panel function below. Alternately you can place the horizontal line on afterwards using trellis.focus/trellis.unfocus as shown below. Read the material under key= and panel= in ?xyplot for more information on the key and panel arguments and read ?trellis.focus for more information on trellis.focus/trellis.unfocus. xyplot(DV~TIME | DOSE, data=data, groups=ID, layout=c(2,1), key=list(x=.1,y=.8,corner=c(0,0),border=TRUE,colums=2,text=list(c(ID1,ID2),col=c(1,4)), lines=list(type=o,pch=c(1,16),lty=c(1,2), col=c(1,4)), layout.heights=list(key.axis.padding=15)), panel = function(x,y,groups,...,panel.number) { panel.superpose.2(x,y,groups,...,type=o,pch=c(1,16), lty=c(1,2), col=c(1,4), cex=0.8) if (panel.number == 1) panel.abline(h=0.301,col=5,lty=1,lwd=2) } ) # add a red horizontal line only to panel 2, 1 trellis.focus(panel, 2, 1, highlight = FALSE) panel.abline(h=0.301,col=2,lty=1,lwd=2) trellis.unfocus() On 8/9/06, HKAG (Henrik Agersø) [EMAIL PROTECTED] wrote: Dear all I have two questions regarding trellis plots - which I hope you may be able to help me with. Is it possible to place the key in a trellis plot on the panel (instead of beside the panel)? This will cause the same key to be reproduced on each panel. Please see the plot below - here I placed the legend below the plot. I tried moving the key to the function statement, but it did not really work out the way I expected. One last thing, in the plot below I placed a horizontal line on the plot, is it possible to only have the horizontal line on the left panel (I remember that in S it was possible to state something like if(get(cell,fr=9)==2) in the function statement to include the line on only one of the panels)? All suggestions will highly appreciated. Br Henrik ### data - as.data.frame(cbind(rep(1:4,each=25), rep(1:2,each=50) ,rep(1:25,4), rnorm(100,0,1) )) names(data) - c(ID,DOSE,TIME,DV) xyplot(DV~TIME | DOSE, data=data, groups=ID, layout=c(2,1), key=list(space=bottom,border=TRUE,colums=2,text=list(c(ID1,ID2),col=c(1,4)), lines=list(type=o,pch=c(1,16),lty=c(1,2), col=c(1,4)), layout.heights=list(key.axis.padding=15)), panel = function(x,y,groups,...) { panel.superpose.2(x,y,groups,...,type=o,pch=c(1,16), lty=c(1,2), col=c(1,4), cex=0.8) panel.abline(h=0.301,col=5,lty=1,lwd=2) } ) ### [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] evolutionary computing in R
Check out the machining learning task view at: http://cran.r-project.org/src/contrib/Views/ On 8/9/06, Christian Miehle [EMAIL PROTECTED] wrote: Hallo, Ich bin auf der Suche nach umgesetzten evolutionären Algorithmen in R. Leider habe ich kein entsprechendes Package oder Funktionen dieser Verfahrensgruppe gefunden. Weiß zufällig jemand, ob Funktionen oder Pakete zu dieser Problematik in R existieren? Vielen Dank, Christian [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] data.frame to shape
Try this: DF[unlist(tapply(rownames(DF), DF$id, function(x) c(x, x[1]))),] On 8/9/06, Leonardo Lami [EMAIL PROTECTED] wrote: Hi all, I have a simple question: I have a data.frame like this: id x y 1 50 1647685 4815259 2 50 1647546 4815196 3 50 1647454 4815294 4 50 1647405 4815347 5 50 1647292 4815552 6 50 1647737 4815410 7 74 1647555 4815201 8 74 1647464 4815023 9 74 1646970 4815129 10 74 1646895 4815264 11 74 1646762 4815513 and I'd like to trasform it with the convert.to.shapefile function (shapefiles package) but to make this I must have a data.frame like this: id x y 1 50 1647685 4815259 2 50 1647546 4815196 3 50 1647454 4815294 4 50 1647405 4815347 5 50 1647292 4815552 6 50 1647737 4815410 7 50 1647685 4815259 8 74 1647555 4815201 9 74 1647464 4815023 10 74 1646970 4815129 11 74 1646895 4815264 12 74 1646762 4815513 13 74 1646762 4815513 with the first point of every id repeated to close the polygon. There is a function to make this indipendently by the number of the id Best regards Leonardo -- Leonardo Lami email + jabber: [EMAIL PROTECTED] www.faunalia.it Cell: (+39)349-1310164 Tel+Fax: (+39) 0587-213742 Piazza Garibaldi 5 - 56025 Pontedera (PI), Italy __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] objects and environments
Dmitris has already provided the solution but just throught I would' mention that your third alternative can be written: apply(mymatrix, 1, fun2, bb = bb) (assuming fun2 has arguments idx and bb) which is not nearly so ugly so you might reconsider whether its ok for you to just pass bb. On 8/9/06, Adrian Dusa [EMAIL PROTECTED] wrote: Dear list, I have two functions created in the same environment, fun1 and fun2. fun2 is called by fun1, but fun2 should use an object which is created in fun1 fun1 - function(x) { ifelse(somecondition, bb - o, bb - *) ## mymatrix is created, then myresult - apply(mymatrix, 1, fun2) } fun2 - function(idx) { if (bb == o) { # do something with idx } else { # do something else with idx } } What should I do to have bb available in fun2? I tried everything I could with sys.parent(), sys.frame(), parent.env() but it just doesn't want to work. I have three solutions but none of them satisfactory; inside fun1: 1. assign(bb, aa, .GlobalEnv) # don't want to do that, do I? 2. assign(bb, aa, 1) # for some reason aa appears in the .GlobalEnv anyway 3. pass bb as an argument to fun2, but this would require: apply(mymatrix, 1, function(idx) fun2(idx, bb)) # which is not elegant I played further with assign and get, but there's something I'm missing: fun1 - function() { e2 - new.env() assign(bb, 4, e2) fun2() } fun2 - function(idx) { get(bb, e2) } fun1() Error in get(bb, e2) : object e2 not found Any hint would be highly appreciated, Adrian -- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd 050025 Bucharest sector 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] index.cond in xyplot
That's not a valid specification. See the description of the index.cond argument in ?xyplot and in particular this part: If 'index.cond' is a list, it has to be as long as the number of conditioning variables, and the 'i'-th component has to be a valid indexing vector for the integer vector '1:nlevels(g_i)' (which can, among other things, repeat some of the levels or drop some altogether). Thus in your case index.cond is a list with three components and each of those components can specify a vector of the levels of interest in the order of interest. For example, compare the output of these two to get the idea where CO2 is a builtin data set: xyplot(conc ~ uptake | Type * Treatment, CO2, index.cond = list(1:2, 1:2)) xyplot(conc ~ uptake | Type * Treatment, CO2, index.cond = list(1:2, 2:1)) On 8/9/06, Taka Matzmoto [EMAIL PROTECTED] wrote: Dear R-users I have 5 dependent variables (y1 to y5) and one independent variable (x) and 3 conditioning variables (m, n, and 0). Each of the conditioning variables has 2 levels. I created 2*4 panel plots. xyplot(y1+y2+y3+y4+y5 ~ x | m*n*o,layout = c(4,2)) I would like to reorder the 8 panels. I tried to use index.cond (e.g., index.cond = list(c(1,3,2,4,5,7,6,8)) but it didn't work out. I got a error message Error in cond.orders(foo) : Invalid value of index.cond. Please let me know if I didn't use index.cond argument properly. I looked at the example in R-help but all examples have just only one conditioning variable. Is there any way I can arrange the panels in whatever order I want ? Thanks Taka __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Is there a better way than x[1:length(x)-1] ?
On 8/9/06, John McHenry [EMAIL PROTECTED] wrote: Hi WizaRds, In MATLAB you can do x=1:10 and then specify x(2:end) to get 2 3 4 5 6 7 8 9 10 In R you could do the above via: x[-1] or whatever (note that in MATLAB the parenthetic index notation is used, not brackets as in R). The point is that 'end' allows you to refer to the final index point of the array. Obviously there isn't much gain in syntax when the variable name is x, but when it's something like hereIsABigVariableName(j:end-i) it makes things a lot more readable than hereIsABigVariableName(j:length(hereIsABigVariableName)-i) In R I could do: n- length(hereIsABigVariableName) hereIsABigVariableName[j:n-i] In R version 2.4.0 Under development (unstable) (2006-08-08 r38825) available from CRAN, head and tail can have negative arguments: head(x, -2) is the same as x[1:8] using your x. but I'd like to use something like 'end', if it exists. Am I missing something obvious in R that does what 'end' does in MATLAB? Thanks, Jack. - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Geometrical Interpretation of Eigen value and Eigen vector
A matrix M can be thought of as a linear transformation which maps input vector x to output vector y: y = Mx The eigenvectors are those directions that this mapping preserves. That is if x is an eigenvector then y = ax for some scalar a. i.e. y lies in the same one dimensional space as x. The only difference is that y is dilated or contracted and possibly reversed and the scale factor defining this dilation/contraction/reversal which corresponds to a particular eigenvector x is its eigenvalue: i.e. y = ax (where a is a scalar, the eigenvalue, corresponding to eigenvector x). In matrix terms, the eigenvectors form that basis in which the linear transformation M has a diagonal matrix and the diagonal values are the eigenvalues. On 8/10/06, Arun Kumar Saha [EMAIL PROTECTED] wrote: Dear all, It is not a R related problem rather than statistical/mathematical. However I am posting this query hoping that anyone can help me on this matter. My problem is to get the Geometrical Interpretation of Eigen value and Eigen vector of any square matrix. Can anyone give me a light on it? Thanks and regards, Arun [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] basic question re lm()
Try: lm(Sepal.Length ~., iris) On 8/10/06, r user [EMAIL PROTECTED] wrote: I am using R in a Windows environment. I have a basic question regarding lm(). I have a dataframe data1 with ncol=w. I know that my dependent variable is in column1. Is there a way to write the regression formula so that I can use columns 2 thru w as my independent variables? e.g. something like: lm(data1[,1] ~ data1[,2:w] ) Thanks __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] day, month, year functions
Here are three ways: xx - as.Date(2006-01-05) # 1. use as.POSIXlt as.POSIXlt(xx)$mday as.POSIXlt(xx)$mon + 1 as.POSIXlt(xx)$year + 1900 # 2. use format as.numeric(format(xx, %d)) as.numeric(format(xx, %m)) as.numeric(format(xx, %Y)) # 3. use month.day.year in chron package library(chron) month.day.year(unclass(xx))$day month.day.year(unclass(xx))$month month.day.year(unclass(xx))$year Also see the help desk article in R News 4/1. On 8/10/06, Horace Tso [EMAIL PROTECTED] wrote: Hi list, I'm trying to turn a date into something productive. (Not what you may be thinking) I want three functions so I could take a date object and get the day of week, month, and year from it. xx - as.Date(2006-01-05) month(xx) equal 1 day(xx) equal 5 year(xx) equal 2006 I'm aware of the weekdays() and months() functions in the base package. But they return a character object which requires some coding to convert into a numeric value. I've also tried the sday.of.week() in fCalendar but it doesn't like my date, sday.of.week(xx) Error in Ops.Date(sdates, 1) : %/% not defined for Date objects Do these functions exist in some package I'm not aware of? Thanks in adv. Horace Tso __ R-help@stat.math.ethz.ch 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.
Re: [R] Multiple density curves
From your description I assume you want both histograms and the densities all on the same chart. With existing R graphics I am not sure that there really is a simple way to do that. That aside, note that the hist function returns a list of components that includes - breaks, defining the breakpoints of the histogram - intensities defining the heights of the histogram bars We can use these two to determine the breaks and y limits of the combined plot and then use the breaks= and ylim= arguments of hist to specify them so that both histograms can be drawn on the same chart. We also use freq=FALSE in the hist calls to draw intensities rather than counts. On the second hist call we use add=TRUE to cause it to be drawn on the existing plot. The other problem is to distinguish the superimposition of the bars and that can be handled by using shading lines of different colors and angles using the col= and angle= and density= arguments of hist. # data DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H, I, J, K, L, M), class = factor), A1 = c(532.5, 25.5, 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5, 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56, 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ, A1, A2), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) # determine breaks and y limits of the combined plot breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities) ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities) ylim - c(0, max(ymax1, ymax2)) # draw the two histograms and two densities hist(DF$A1, ang = 45, col = red, ylim = ylim, freq = FALSE, density = 10) lines(density(DF$A1), col = red) hist(DF$A2, ang = -45, col = blue, add = TRUE, freq = FALSE, density = 10) lines(density(DF$A2), col = blue) On 8/10/06, Davendra Sohal [EMAIL PROTECTED] wrote: Hi, I am new to R...a recent convert from SAS. I have a dataset that looks like this: SEQA1A2 A532.5554.5 B25.535.5 C265.2522.2 D245.55521.56 E546.52141.52 F243.2532.56 G452.55635.56 H15.1416.54 I543.4646.56 J54.4654.5 K646.564.54 L645.4614.46 M646.54634.46 I want to make a histogram each for A1 and A2, with density curves, on the same plot so that I can see how they overlap. Please let me know some simple code for this. I looked at ldahist but it was complicated. Anything simpler? Thanks a lot, -DS. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Multiple density curves
The code below was missing the breaks= argument to hist. I had not noticed because coincidentally both give the same breaks anways thus the following corrected version gives the same plot in this case but might not in other cases. # data DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H, I, J, K, L, M), class = factor), A1 = c(532.5, 25.5, 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5, 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56, 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ, A1, A2), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) # determine breaks and y limits of the combined plot breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities) ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities) ylim - c(0, max(ymax1, ymax2)) # draw the two histograms and two densities hist(DF$A1, ang = 45, col = red, ylim = ylim, breaks = breaks, freq = FALSE, density = 10) lines(density(DF$A1), col = red) hist(DF$A2, ang = -45, col = blue, add = TRUE, breaks = breaks, freq = FALSE, density = 10) lines(density(DF$A2), col = blue) On 8/11/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: From your description I assume you want both histograms and the densities all on the same chart. With existing R graphics I am not sure that there really is a simple way to do that. That aside, note that the hist function returns a list of components that includes - breaks, defining the breakpoints of the histogram - intensities defining the heights of the histogram bars We can use these two to determine the breaks and y limits of the combined plot and then use the breaks= and ylim= arguments of hist to specify them so that both histograms can be drawn on the same chart. We also use freq=FALSE in the hist calls to draw intensities rather than counts. On the second hist call we use add=TRUE to cause it to be drawn on the existing plot. The other problem is to distinguish the superimposition of the bars and that can be handled by using shading lines of different colors and angles using the col= and angle= and density= arguments of hist. # data DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H, I, J, K, L, M), class = factor), A1 = c(532.5, 25.5, 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5, 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56, 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ, A1, A2), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) # determine breaks and y limits of the combined plot breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities) ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities) ylim - c(0, max(ymax1, ymax2)) # draw the two histograms and two densities hist(DF$A1, ang = 45, col = red, ylim = ylim, freq = FALSE, density = 10) lines(density(DF$A1), col = red) hist(DF$A2, ang = -45, col = blue, add = TRUE, freq = FALSE, density = 10) lines(density(DF$A2), col = blue) On 8/10/06, Davendra Sohal [EMAIL PROTECTED] wrote: Hi, I am new to R...a recent convert from SAS. I have a dataset that looks like this: SEQA1A2 A532.5554.5 B25.535.5 C265.2522.2 D245.55521.56 E546.52141.52 F243.2532.56 G452.55635.56 H15.1416.54 I543.4646.56 J54.4654.5 K646.564.54 L645.4614.46 M646.54634.46 I want to make a histogram each for A1 and A2, with density curves, on the same plot so that I can see how they overlap. Please let me know some simple code for this. I looked at ldahist but it was complicated. Anything simpler? Thanks a lot, -DS. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Multiple density curves
Here is one more solution. This one uses lattice. Its a bit shorter than the classic graphics solution. In the classic graphics version we used shading and color to distinguish the bars; however, grid, and therefore lattice, do not easily support shading (its possible to simulate it using low level vector graphics but that's beyond the scope of this) so we use width (lwd), style (lty) and colour (col) to distinguish them. Also note that the for loop iterates over the groups since the lattice histogram function does not use the groups= argument of lattice's xyplot. library(lattice) # data DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H, I, J, K, L, M), class = factor), A1 = c(532.5, 25.5, 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5, 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56, 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ, A1, A2), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) histogram(~ unlist(DF[,-1]), type = density, panel = function(x, breaks, ...) for(j in 2:ncol(DF)) { panel.histogram(DF[,j], border = j, lwd = j, lty = j, breaks = breaks, col = transparent, ...) panel.densityplot(DF[,j], col = j, ...) }) On 8/11/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: The code below was missing the breaks= argument to hist. I had not noticed because coincidentally both give the same breaks anways thus the following corrected version gives the same plot in this case but might not in other cases. # data DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H, I, J, K, L, M), class = factor), A1 = c(532.5, 25.5, 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5, 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56, 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ, A1, A2), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) # determine breaks and y limits of the combined plot breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities) ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities) ylim - c(0, max(ymax1, ymax2)) # draw the two histograms and two densities hist(DF$A1, ang = 45, col = red, ylim = ylim, breaks = breaks, freq = FALSE, density = 10) lines(density(DF$A1), col = red) hist(DF$A2, ang = -45, col = blue, add = TRUE, breaks = breaks, freq = FALSE, density = 10) lines(density(DF$A2), col = blue) On 8/11/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: From your description I assume you want both histograms and the densities all on the same chart. With existing R graphics I am not sure that there really is a simple way to do that. That aside, note that the hist function returns a list of components that includes - breaks, defining the breakpoints of the histogram - intensities defining the heights of the histogram bars We can use these two to determine the breaks and y limits of the combined plot and then use the breaks= and ylim= arguments of hist to specify them so that both histograms can be drawn on the same chart. We also use freq=FALSE in the hist calls to draw intensities rather than counts. On the second hist call we use add=TRUE to cause it to be drawn on the existing plot. The other problem is to distinguish the superimposition of the bars and that can be handled by using shading lines of different colors and angles using the col= and angle= and density= arguments of hist. # data DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H, I, J, K, L, M), class = factor), A1 = c(532.5, 25.5, 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5, 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56, 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ, A1, A2), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) # determine breaks and y limits of the combined plot breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities) ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities) ylim - c(0, max(ymax1, ymax2)) # draw the two histograms and two densities hist(DF$A1, ang = 45, col = red, ylim = ylim, freq = FALSE, density = 10) lines(density(DF$A1), col = red) hist(DF$A2, ang = -45, col = blue, add = TRUE, freq = FALSE, density = 10) lines(density(DF$A2), col = blue) On 8/10/06, Davendra Sohal [EMAIL PROTECTED] wrote: Hi, I am new to R...a recent convert from SAS. I have a dataset that looks like this: SEQA1A2 A532.5
Re: [R] An apply and rep question
The approach here is to perform the repetition on the indices (or rownames) rather than on the data frame directly. Using the builtin data frame BOD any of the following would work: BOD[gl(nrow(BOD), 2),] BOD[rep(1:nrow(BOD), each = 2),] BOD[rep(rownames(BOD), each = 2),] On 8/11/06, Horace Tso [EMAIL PROTECTED] wrote: Hi list, I'm sure the explanation must be laughably simple to the experts out there, but I just could figure it out. I have a simple data frame that looks like, head(da.off) DDate OffP 1 2005-01-01 41.23 2 2005-01-02 44.86 3 2005-01-03 44.86 4 2005-01-04 43.01 5 2005-01-05 45.47 6 2005-01-06 48.62 where the first column DDate currently is character, and OffP is numeric. I want to duplicate every row 2 times, so I thought I use apply(), x - apply(da.off, 2, rep, each=2) The result is a matrix of all character, head(x) DDateOffP 1 2005-01-01 41.23 1 2005-01-01 41.23 2 2005-01-02 44.86 2 2005-01-02 44.86 3 2005-01-03 44.86 3 2005-01-03 44.86 To convert it back to numeric, I did x - as.data.frame(x) x$OffP - as.numeric(x$OffP) However, the OffP column didn't convert correctly, a mystery since they look quite alright above. (I know, I know, there seems to be a space there. But why?) head(x) DDate OffP 1 2005-01-01 150 1.1 2005-01-01 150 2 2005-01-02 202 2.1 2005-01-02 202 3 2005-01-03 202 3.1 2005-01-03 202 Is this the wrong way to use apply or rep? Horace __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] x tick labels - sparse?
Try this: x - seq(-100,1000,25) y - x * x plot(x, y, xaxt = n) axis(1, x[x %% 100 == 0]) On 8/11/06, Darren Weber [EMAIL PROTECTED] wrote: Hi, I'm stuck on creating a plot with x tick labels for every Nth tick mark - how is that done? I don't see a simple solution to this in help(plot) or help(par) and what I've tried is not working, eg, the following does not work, although it seems intuitive to me that it should work: x - seq(-100,1000,25) y - x * x % find all the x values that are multiples of 100 tmp - x / 100 tmp - tmp %% 1 tmp - tmp 0 % set all other values to null strings xtickLabels - as.character( x ) xtickLabels[tmp] - plot(x, y, xlab=xtickLabels) These commands look like this (the plot is not right): x - seq(-100,1000,25) x [1] -100 -75 -50 -250 25 50 75 100 125 150 175 200 225 250 [16] 275 300 325 350 375 400 425 450 475 500 525 550 575 600 625 [31] 650 675 700 725 750 775 800 825 850 875 900 925 950 975 1000 tmp - x / 100 tmp [1] -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 [13] 2.00 2.25 2.50 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 [25] 5.00 5.25 5.50 5.75 6.00 6.25 6.50 6.75 7.00 7.25 7.50 7.75 [37] 8.00 8.25 8.50 8.75 9.00 9.25 9.50 9.75 10.00 tmp - tmp %% 1 tmp [1] 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 [16] 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 [31] 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 tmp - tmp 0 tmp [1] FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE [13] FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE [25] FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE [37] FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE xtickLabels - as.character( x ) xtickLabels [1] -100 -75 -50 -25 025 50 75 100 125 [11] 150 175 200 225 250 275 300 325 350 375 [21] 400 425 450 475 500 525 550 575 600 625 [31] 650 675 700 725 750 775 800 825 850 875 [41] 900 925 950 975 1000 xtickLabels[tmp] - xtickLabels [1] -1000 100 [11] 200 300 [21] 400 500 600 [31] 700 800 [41] 900 1000 y - x * x y [1] 156252500 625 0 62525005625 1 [10] 15625 22500 30625 4 50625 62500 75625 9 105625 [19] 122500 140625 16 180625 202500 225625 25 275625 302500 [28] 330625 36 390625 422500 455625 49 525625 562500 600625 [37] 64 680625 722500 765625 81 855625 902500 950625 100 plot(x, y, xlab=xtickLabels) Thanks in advance. Best, Darren __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] adding multiple fitted curves to xyplot graph
Try this after displaying the xyplot: # this fit.curve returns the whole nls object, not the coefs fit.curve-function(tab) { nls(y1 ~ 100/(1+exp(((-log(81))/a)*(x1-b))), start=list(a=min(tab$x1[tab$y176],na.rm=T)-max(tab$x1[tab$y115],na.rm=T),b=tab$x1[abs(tab$y1-50)==min(abs(tab$y1-50),na.rm=T)][!is.na(tab$x1[abs(tab$y1-50)==min(abs(tab$y1-50),na.rm=T)])]),data=tab) } trellis.focus(panel, 1, 1) f - function(x) panel.lines(x$x1, fitted(fit.curve(x)), col = 1) junk - by(df, df$factor1, f) trellis.unfocus() On 8/16/06, GOUACHE David [EMAIL PROTECTED] wrote: Hello RHelpers, This may already have been answered, but despite days of scouring through the archives I haven't found it. My goal is to add multiple fitted curves to a plot. An example data set (a data frame named df in following code) is: x1 y1 factor1 4 1298.25 0. 1 5 1393.25 0. 1 6 1471.50 0.04597701 1 7 1586.70 2.56908046 1 8 1692.10 11.14080460 1 9 1832.55 45.50459770 1 10 1928.30 65.5600 1 11 2092.40 100. 1 31 1202.90 0. 2 41 1298.25 0. 2 51 1393.25 0.37885057 2 61 1471.50 0.76839080 2 71 1586.70 7.75206897 2 81 1692.10 50.19448276 2 91 1832.55 94.08045977 2 101 1928.30 100. 2 111 2092.40 100. 2 14 1028.50 0. 3 22 1106.40 0.04938272 3 32 1202.90 0.03448276 3 42 1298.25 0.34482759 3 52 1393.25 1.43850575 3 62 1471.50 1.96850575 3 72 1586.70 36.80597701 3 82 1692.10 92.83390805 3 92 1832.55 100. 3 15 1028.50 0.09638554 4 23 1106.40 0.39988506 4 33 1202.90 0.49321839 4 43 1298.25 1.66045977 4 53 1393.25 7.51137931 4 63 1471.50 42.02724138 4 73 1586.70 99.12068966 4 83 1692.10 100. 4 I plot this with xyplot: trellis.par.set(background,white) trellis.par.set(list(superpose.symbol=list(pch=c(15:17,21,25 xyplot(y1 ~ x1, data=df, groups=factor1, type = p, auto.key = list(space = right, points = TRUE, lines = FALSE)) For each level of factor1 I fit a growth curve: fit.curve-function(tab) { res.fit-nls(y1 ~ 100/(1+exp(((-log(81))/a)*(x1-b))), start=list(a=min(tab$x1[tab$y176],na.rm=T)-max(tab$x1[tab$y115],na.rm=T),b=tab$x1[abs(tab$y1-50)==min(abs(tab$y1-50),na.rm=T)][!is.na(tab$x1[abs(tab$y1-50)==min(abs(tab$y1-50),na.rm=T)])]),data=tab) coef(res.fit) } by(df,list(df$factor1),fit.curve) I would like to add the 4 curves corresponding to these 4 fits to my graphic. The elegant way would be a custom panel function I suppose, but I haven't been able to write one up... Could someone help me out on this please? In advance thanks very much!!! David Gouache Arvalis - Institut du Végétal Station de La Minière 78280 Guyancourt Tel: 01.30.12.96.22 / Port: 06.86.08.94.32 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] bwplot in loop doesn't produce any output
Its a FAQ http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f On 8/16/06, Nick Desilsky [EMAIL PROTECTED] wrote: Hi, running the following code by itself runs as expected. k - 1 i - 2 j - 3 NumName - varnames[num.cols[k]] FacNames - varnames[fac.cols[c(i,j)]] tmp - paste(FacNames[1],NumName,sep=~) fml - formula(paste(tmp,FacNames[2],sep=|)) bwplot(fml, data = X) But when set into a loop, it doens't produce anything. I've tried sending the output to pdf(file=test.pdf), and the pdf file stays empty. for (i in 1:(lfc-1)) { for (j in (i+1):lfc) { for (k in 1:lnc) { NumName - varnames[num.cols[k]] FacNames - varnames[fac.cols[c(i,j)]] tmp - paste(FacNames[1],NumName,sep=~) fml - formula(paste(tmp,FacNames[2],sep=|)) bwplot(fml, data = X) } } } Any thoughts ? And if you know how to unlock test.pdf for viewing while R is running, I'd appreciate this bit of info too (i've tried dev.off(), windows() and such, and test.pdf can only be viewed after shutting down R). Thank you. Nick. - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] separate row averages for different parts of an array
The following reshapes mat so we can take the means of the columns of the resulting 3d array and then transposes it back to the original orientation: t(colMeans(array(t(mat), c(100, 448, 24 You might want to try it on this test set first where anscombe is an 11x8 data set built into R. Here are 4 solutions using anscombe 1. This is just the above written for the anscombe data set: t(colMeans(array(t(anscombe), c(4,2,11 2. Here is a solution using apply instead of colMeans and t. In this case anscombe is a data.frame, not an array/matrix, and we need to turn it into one first. The prior solution also required a matrix but tranpose will convert a dataframe to a matrix so we did not have to explicitly do it there. If your array is indeed an array as stated in your post then you can omit the as.matrix part. In your case the c(11,4,2) vector would be c(24, 100, 448) : apply(array(as.matrix(anscombe), c(11,4,2)), c(1,3), mean) 3. Here is another solution. This one uses the zoo package and does have the advantage of not having to specify a bunch of dimensions. It uses rapply from zoo (which will be renamed rollapply in the next version of zoo so as not to conflict with the new rapply that is appearing in R 2.4.0). In your case both occurrences of 4 would be 100: library(zoo) coredata(t(rapply(zoo(t(anscombe)), 4, by = 4, mean))) 4. This is Marc's solution except we use seq instead of : at the end in order to make use of the length= argument. In your case c(11, 8, 4) would be c(1, 44800, 100) and length = 4 would be length = 100: sapply(seq(1, 8, 4), function(i) rowMeans(anscombe[, seq(i, length = 4)])) On 8/16/06, Spencer Jones [EMAIL PROTECTED] wrote: I have an array with 44800 columns and 24 rows I would like to compute the row average for the array 100 columns at a time, so I would like to end up with an array of 24 rows x 448 columns. I have tried using apply(dataset, 1, function(x) mean(x[])), but I am not sure how to get it to take the average 100 columns at a time. Any ideas would be welcomed. thanks, Spencer [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Plots Without Displaying
Also check out the displaylist: http://tolstoy.newcastle.edu.au/R/help/04/05/0817.html On 8/17/06, Lothar Botelho-Machado [EMAIL PROTECTED] wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Thank you, It seems that a list of plots is just possible using lattice plots. But that's a good keyword for me to look for, I appreciate your help! Lothar Christos Hatzis wrote: Yes, you can do that for lattice-based plots. The functions in the lattice package produce objects of class trellis which can be stored in a list and processed or updated at a later time: library(lattice) attach(barley) plotList - list(length=3) plotList[[1]] - xyplot(yield ~ site, data=barley) plotList[[2]] - xyplot(yield ~ variety, data=barley) plotList[[3]] - xyplot(yield ~ year, data=barley) plotList plotList[[3]] - update(plotList[[3]], yaxis=Yield (bushels/acre)) print(plotList[[3]]) Obviously, you can store any lattice-based plot in the list. HTH. -Christos Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Lothar Botelho-Machado Sent: Wednesday, August 16, 2006 4:49 PM To: r-help@stat.math.ethz.ch Subject: Re: [R] Plots Without Displaying Prof Brian Ripley wrote: Yes, see ?jpeg ?bitmap and as you didn't tell us your OS we don't know if these are available to you. jpeg(file=test.jpg) boxplot(sample(100)) dev.off() may well work. 'An Introduction to R' explains about graphics devices, including these. On Wed, 16 Aug 2006, Lothar Botelho-Machado wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 R Help Mailing List, I'd like to generate a plot that I could display and/or store it as e.g. jpeg. But unfortunately always a plotting window opens. Is it possible to prevent that? I tried the following: R bp-boxplot( sample(100), plot=FALSE) This works somehow, but it only stores data (as discribed in the help) in bp and it is not possible afaik to display bp later on or store them as a jpeg. The next: R p-plot(sample(100), sample(100), plot=FALSE) ..and also a variant using jpeg() didn't work at all. Is there a way to generally store the plots as object, without displaying them, or perhaps directly saving them to disc as jpeg? A Yes or No or any further help/links are appreciated!!! Thank you for the explanation and your patience in answering me this obviously very simple question!! Originally I tried to store plots directly in a list. So writing them directly to disc was just a good alternative. I knew that that jpeg() provides functionality for that, but didn't use it correctly. Hence, is it also possible to store a plot in a list, somehow? Kind regards, Lothar __ R-help@stat.math.ethz.ch 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. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.3 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFE5HU1HRf7N9c+X7sRAguEAJ4855nuonJaB9VXHkGOr/SZhqow8wCfXcuB o8oqpYoJ7MXgnVtnuGAE5Yk= =ZWgN -END PGP SIGNATURE- __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] rbind-ing vectors inside lists
Try: mapply(rbind, a, b, SIMPLIFY = FALSE) On 8/17/06, Domenico Vistocco [EMAIL PROTECTED] wrote: Dear helpeRs, suppose I have two lists as follows: a = list(1:5,5:9) b = lapply(a,*,2) I would like to rbind-ing the two lists, that is I would like to use something as rbind applied component to component for the two list. I have used the following solution: fun.tile.wt = function(list1, list2) { for(i in 1:length(list1)) { list1[[i]]=rbind(list1[[i]],list2[[i]]) } list1 } fun.tile.wt(a,b) Is it possible to directly obtain the result using the apply family (or something else)? Any suggestions is appreciated. Thanks in advance, domenico vistocco [[alternative HTML version deleted]] Chiacchiera con i tuoi amici in tempo reale! http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] day, month, year functions
On 8/17/06, Martin Maechler [EMAIL PROTECTED] wrote: Gregor == Gregor Gorjanc [EMAIL PROTECTED] on Fri, 11 Aug 2006 00:27:27 + (UTC) writes: Gregor Gabor Grothendieck ggrothendieck at gmail.com writes: Here are three ways: xx - as.Date(2006-01-05) # 1. use as.POSIXlt as.POSIXlt(xx)$mday as.POSIXlt(xx)$mon + 1 as.POSIXlt(xx)$year + 1900 # 2. use format as.numeric(format(xx, %d)) as.numeric(format(xx, %m)) as.numeric(format(xx, %Y)) # 3. use month.day.year in chron package library(chron) month.day.year(unclass(xx))$day month.day.year(unclass(xx))$month month.day.year(unclass(xx))$year Gregor Hi, Gregor it would really be great if there would be Gregor sec(), min(), hour() day(), month(), year() Gregor generic functions that would work on all date classes. Where Gregor applicable of course. I imagine that argument to get out integer Gregor or character would alse be nice. I disagree pretty strongly: - We definitely don't want min() to return minutes instead of minimum ! - Why pollute the namespace with 6 (well, actualy 5!) new function names, when as.POSIXlt() *REALLY* is there exactly for this purpose ??? I rather think the authors of each of the other old-fashioned date classes should provide as.POSIXlt() methods for their classes. Then, we'd have uniform interfaces, following's Gabor's # 1. above. Martin Maechler, ETH Zurich There are two problems: 1. as.POSIXlt is not generic. (This problem may not be too important given that as.POSIXlt does handle Date and chron dates classes already but in terms of handling all potential classes its a limitation.) 2. in the case of as.POSIXlt converting chron dates objects to POSIXlt there is a time zone consideration, as shown below, where today, August 17th in the Eastern Daylight Time zone, is displayed as August 16th using as.POSIXlt unless we use tz = GMT library(chron) # today is August 17th. Sys.Date() [1] 2006-08-17 chron(unclass(Sys.Date())) [1] 08/17/06 Sys.time() [1] 2006-08-17 14:28:19 Eastern Daylight Time as.POSIXlt(Sys.Date()) [1] 2006-08-17 as.POSIXlt(chron(unclass(Sys.Date( [1] 2006-08-16 20:00:00 Eastern Daylight Time as.POSIXlt(chron(unclass(Sys.Date())), tz = GMT) [1] 2006-08-17 GMT R.version.string # Windows XP [1] Version 2.3.1 Patched (2006-06-04 r38279) __ R-help@stat.math.ethz.ch 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.
Re: [R] getting sapply to skip columns with non-numeric data?
Use the first few rows of iris as test data and try this where isnum is 1 for each numeric column and NA for others. irish - head(iris) isnum - ifelse(sapply(iris, class) == numeric, 1, NA) iris.data - data.matrix(iris) rbind(iris, colMeans(iris.data) * isnum, sd(iris.data) * isnum) On 8/17/06, r user [EMAIL PROTECTED] wrote: getting s-apply to skip columns with non-numeric data? I have a dataframe x of w columns. Some columns are numeric, some are not. I wish to create a function to calculate the mean and standard deviation of each numeric column, and then bind the column mean and standard deviation to the bottom of the dataframe. e.g. tempmean - apply(data.frame(x), 2, mean, na.rm = T) xnew - rbind(x,tempmean) I am running into one small problem…what is the best way to have sapply skip the non-numeric data and return NA's? __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice package par.settings/trellis.par.settings questions
The parameter names are axis.text$font and axis.text$cex . Try issuing the command: trellis.par.get() to get a complete list. Here is an example: histogram(1:10, par.settings = list(axis.text = list(font = 2, cex = 0.5))) On 8/17/06, Debarchana Ghosh [EMAIL PROTECTED] wrote: Hi All, I'm trying to modify some of the default graphic parameters in a conditional histogram. While I was able to change the default grey background to white, I couldn't change the axis.font or the xlab font. I used the following code: /histogram(~V751|V013+V025, finalbase, xlab=Heard of HIV/AIDS (No/Yes), col=c(cyan,magenta), par.settings=list(background=white)) /The arguments for example like /axis.font=2/, or /cex=2/ are not working in the /par.settings(). /I also tried to read the manual of /trellis.par.settings()/ but didn't understand how to use it and where exactly to put it. Any help with this will be appreciated. Thanks, Debarchana. -- Debarchana Ghosh Research Assistant Department of Geography University of Minnesota PH: 8143607580 email to: [EMAIL PROTECTED] www.tc.umn.edu/~ghos0033 __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Insert rows - how can I accomplish this in R
Here are two solutions. In both we break up DF into rows which start with 1. In solution #1 we create a new data frame with the required sequence for A and zeros for B and then we fill it in. In solution #2 we convert each set of rows to a zoo object z where column A is the times and B is the data. We convert that zoo object to a ts object (which has the effect of filling in the missing times) and then create a zoo object with no data from its times merging that zoo object with z using a fill of 0. Finally in both solutions we reconstruct the rows from that by rbind'ing everything together. # 1 f - function(x) { DF - data.frame(A = 1:max(x$A), B = 0) DF[x$A,B] - x$B DF } do.call(rbind, by(DF, cumsum(DF$A == 1), f)) # 2 library(zoo) f - function(x) { z - zoo(x$B, x$A) ser - merge(zoo(,time(as.ts(z)), z, fill = 0) data.frame(A = time(ser), B = coredata(ser)) } do.call(rbind, by(DF, cumsum(DF$A == 1), f) On 8/18/06, Sachin J [EMAIL PROTECTED] wrote: Hi, I have following dataframe. Column A indicates months. DF - structure(list(A = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 7, 8, 11, 12, 1, 2, 3, 4, 5, 8), B = c(0, 0, 0, 8, 0, 19, 5, 19, 0, 0, 0, 11, 0, 8, 5, 11, 19, 8, 11, 10, 0, 8, 36, 10, 16, 10, 22)), .Names = c(A, B), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27)) There is some discontinuity in the data. For example month 6, 9,10 data (2nd year) and month 6 data (3rd year) are absent. I want to insert the rows in place of these missing months and set the corresponding B column to zero. i.e., the result should look like: DFNEW - structure(list(A = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8), B = c(0, 0, 0, 8, 0, 19, 5, 19, 0, 0, 0, 11, 0, 8, 5, 11, 19, 0, 8, 11, 0, 0, 10, 0, 8, 36, 10, 16, 10, 0, 0, 22)), .Names = c(A, B), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)) Thanks in advance. Sachin - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Insert rows - how can I accomplish this in R
I did run it so I am not sure how the error crept in. Anyways, I have fixed it and a corrected version is below. --- Here are two solutions. In both we break up DF into rows which start with 1. In #1 we create a new data frame with the required sequence for A and zeros for B and then we fill it in. In #2 we convert each set of rows to a zoo object z where column A is the times and B is the data. We convert that zoo object to a ts object (which has the effect of filling in the missing times) and then create a zoo object with no data from that merging it with z using a fill of 0. Finally in both solutions we reconstruct the rows from that by rbind'ing everything together. # 1 f - function(x) { DF - data.frame(A = 1:max(x$A), B = 0) DF[x$A,B] - x$B DF } do.call(rbind, by(DF, cumsum(DF$A == 1), f)) # 2 library(zoo) f - function(x) { z - zoo(x$B, x$A) ser - merge(zoo(,time(as.ts(z))), z, fill = 0) data.frame(A = time(ser), B = coredata(ser)) } do.call(rbind, by(DF, cumsum(DF$A == 1), f)) On 8/18/06, Sachin J [EMAIL PROTECTED] wrote: Gabor, Thanks a lot for the help. The 1st method works fine. In 2nd method I am getting following error. do.call(rbind, by(DF, cumsum(DF$A == 1), f)) Error in zoo(, time(as.ts(z)), z, fill = 0) : unused argument(s) (fill ...) Unable to figure out the cause. Thanks, Sachin Gabor Grothendieck [EMAIL PROTECTED] wrote: Here are two solutions. In both we break up DF into rows which start with 1. In solution #1 we create a new data frame with the required sequence for A and zeros for B and then we fill it in. In solution #2 we convert each set of rows to a zoo object z where column A is the times and B is the data. We convert that zoo object to a ts object (which has the effect of filling in the missing times) and then create a zoo object with no data from its times merging that zoo object with z using a fill of 0. Finally in both solutions we reconstruct the rows from that by rbind'ing everything together. # 1 f - function(x) { DF - data.frame(A = 1:max(x$A), B = 0) DF[x$A,B] - x$B DF } do.call(rbind, by(DF, cumsum(DF$A == 1), f)) # 2 library(zoo) f - function(x) { z - zoo(x$B, x$A) ser - merge(zoo(,time(as.ts(z)), z, fill = 0) data.frame(A = time(ser), B = coredata(ser)) } do.call(rbind, by(DF, cumsum(DF$A == 1), f) On 8/18/06, Sachin J wrote: Hi, I have following dataframe. Column A indicates months. DF - structure(list(A = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 7, 8, 11, 12, 1, 2, 3, 4, 5, 8), B = c(0, 0, 0, 8, 0, 19, 5, 19, 0, 0, 0, 11, 0, 8, 5, 11, 19, 8, 11, 10, 0, 8, 36, 10, 16, 10, 22)), .Names = c(A, B), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27)) There is some discontinuity in the data. For example month 6, 9,10 data (2nd year) and month 6 data (3rd year) are absent. I want to insert the rows in place of these missing months and set the corresponding B column to zero. i.e., the result should look like: DFNEW - structure(list(A = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8), B = c(0, 0, 0, 8, 0, 19, 5, 19, 0, 0, 0, 11, 0, 8, 5, 11, 19, 0, 8, 11, 0, 0, 10, 0, 8, 36, 10, 16, 10, 0, 0, 22)), .Names = c(A, B), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)) Thanks in advance. Sachin - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. Do you Yahoo!? Next-gen email? Have it all with the all-new Yahoo! Mail Beta. __ R-help@stat.math.ethz.ch 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.