[R] using a variable for a column name in a formula
lm(height ~ ., data=X) works fine. However nnn - height ; lm(nnn ~ . ,data=X) fails How do I write such a formula, which depends on the value of a string variable like nnn above? A typical application might be a program that takes a data frame containing only numerical data, and figures out which of the columns can be best predicted from all the other columns. Thanks David __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Sweave: printing an underscore in the output from an R command
I am working with Sweave and would like to print out into my latex document the result of the R command version$platform So what I first tried in my .Rnw document was \Sexpr{print(version$platform)}. However, the output from this command is the string x86_64-apple-darwin10.8.0 (without the quotes). This contains an underscore, which is a special character in tex and so I get an error message from latex. I can get round this by using sub to replace underscore with a space, but I would like to know how to print the underscore if I really wanted to do so. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Meaning of Integer,19
I tried example('apply'). Among the various examples, there was the following: apply z - array(1:24, dim = 2:4) apply zseq - apply(z, 1:2, function(x) seq_len(max(x))) apply zseq ## a 2 x 3 matrix [,1] [,2] [,3] [1,] Integer,19 Integer,21 Integer,23 [2,] Integer,20 Integer,22 Integer,24 The entry Integer,19 seems to mean the list of integers [1:19], though I'm just guessing. Possibly it means some list of 19 integers. Questions: Is the notation Integer,19 documented somewhere? I can't find it. How might one proceed to find out the meaning of this notation if one didn't know it before? The actual substance of my question is a request for advice on how, in general, to look for documentation in R. I know about help.start(), help.search() and RSiteSearch(). Also, I know how to search the archives of r-help. Are there other methods of searching R that I should try? Usually I get more hits than I can cope with. Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] knitr: Was previously Sweave: printing an underscore in the output from an R command
Dear Yihui Thanks very much for drawing my attention to knitr, which I had not heard of before. Also thanks for pointing out the bug in Sweave, which I don't fully understand, but I don't want to spend time and effort on understanding it. So I hope you will find time to report the bug. I was pretty sure there was a bug somewhere that was preventing me from doing what I wanted to do in Sweave, but I misdiagnosed the source of the problem. I notice you didn't use print() or cat() in your short program for knitr. Is it the case that it's necessary to use print() or cat() with \Sexpr in Sweave, but unnecessary in knitr? I'll stick to Sweave for my current project, and try out knitr on my next project. I would welcome a list of documents about knitr that I should download, so as to make it as easy as possible to get started. I don't want to understand the internals of knitr, but I am interested in any documents on knitr, written by you or by others, directed at the user, rather than at programmers of packages. Is it convenient to use vi(m) to produce knitr source? Can vi(m) be integrated into the knitr package? My experience with editors designed specially to work with particular products (like the built-in editor for TeXWorks on the Mac) do not have the power of vi(m) and emacs, and I require this power. @Duncan: thanks for indicating the use of cat() instead of print(). However, due to the bug in Sweave pointed out by Yihui, replacing print by cat didn't help me. Thanks David On 2 Sep 2013, at 21:11, Yihui Xie wrote: I think Thierry meant gsub(_, _, version$platform); he just typed too quickly. The point is to escape _ using \, but then people are often trapped in the dreams of dreams of dreams of backslashes like the movie Inception. And then due to a long-standing bug in Sweave for \Sexpr{} (sorry I forgot to report to R core), you will be so confused that you can never wake up and come back to the reality. Dream level 1: when you need a backslash in a character string, you need \\, which really means \; you think \\_ should be good, but no -- Dream level 2: when you need one literal \ in a regular expression as the replacement expression, you need \\ Combine the two levels of dreams, you end up with _. in R really means \\, which really means \ in regular expressions. Now you are good at the regular expression level, but Sweave comes and bites you, and that is due to this bug in the regular expression in Sweave Noweb syntax: SweaveSyntaxNoweb$docexpr [1] Sexpr\\{([^\\}]*)\\} It should have been Sexpr\\{([^}]*)\\}, i.e. } does not need to be escaped inside [], and \\ will be interpreted literally inside []. In your case, Sweave sees \ in \Sexpr{}, and the regular expression stops matching there, and is unable to see } after \, so it believes there is no inline R expressions in your document. BTW, knitr does not have this bug and works well in your case: \documentclass{article} \begin{document} \Sexpr{sub(_, _, version$platform)} \end{document} Regards, Yihui -- Yihui Xie xieyi...@gmail.com Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Mon, Sep 2, 2013 at 2:18 PM, David Epstein david.epst...@warwick.ac.uk wrote: Dear Thierry, Your suggestion doesn't work on my version of R. Here's what I get gsub(_, \_, print(version$platform) Error: '\_' is an unrecognized escape in character string starting \_ print(gsub(_, \_, version$platform)) Error: '\_' is an unrecognized escape in character string starting \_ sub(_, \\_, version$platform) [1] x86_64-apple-darwin10.8.0 Sweave does not evaluate this expression when \Sexpr is applied and a tex error results sub(_, \\\_, version$platform) Error: '\_' is an unrecognized escape in character string starting \\\_ Error message from R sub(_, _, version$platform) [1] x86\\_64-apple-darwin10.8.0 R evaluates this. However, the above examples indicate a deficiency/possible bug in the command sub, because sub does not seem to be able to output an expression with a single backslash. I tried the previous version as follows in my .Rnw document \Sexpr{print(sub(_, _, version$platform))} When Sweave is run, this expression is evaluated to illegal LaTeX David. On 2 Sep 2013, at 16:47, ONKELINX, Thierry wrote: You have to escape the underscore \Sexpr{gsub(_, \_, print(version$platform))} Best regards, Thierry Van: r-help-boun...@r-project.org [r-help-boun...@r-project.org] namens David Epstein [david.epst...@warwick.ac.uk] Verzonden: maandag 2 september 2013 17:38 Aan: r-help@r-project.org Onderwerp: [R] Sweave: printing an underscore in the output from an R command I am working with Sweave and would like to print out into my latex document the result of the R command version$platform So what I
[R] Unsuccessful beginner's struggle with lm
I have two data frames, train and response. Here is my attempt to do a linear regression. All entries of both data frames are numeric. I am expecting the intercept value to lie between 2 and 3 (in particular, non-zero). Here is a record of my interaction with R: class(response) [1] data.frame c(nrow(response),ncol(response)) [1] 13891 class(train) [1] data.frame c(nrow(train),ncol(train)) [1] 1389 256 beta.lm - lm(response ~ train) Error in model.frame.default(formula = response ~ train, drop.unused.levels = TRUE) : invalid type (list) for variable 'response' What elementary syntax error am I making in my call to lm? And why does R think at first that the class of response is data.frame, but that its class is list when I call lm? Thanks David [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Mac Os X 10.6.8, R and edit/vi
I tried using various versions of the 'edit' command. Here is an account of how this failed. I hope I have included all relevant information. I haven't used R for a couple of years. Before restarting with R, I downloaded the latest version I could find in its binary version, and installed it without any problems. Mac Os X Finder command About R responds with R 3.0.1 GUI 1.61 Snow Leopard build (6492) From inside R version platform x86_64-apple-darwin10.8.0 arch x86_64 os darwin10.8.0#(However, my os is in fact 10.6.8) system x86_64, darwin10.8.0 status major 3 minor 0.1 year 2013 month 05 day16 svn rev62743 language R version.string R version 3.0.1 (2013-05-16) nickname Good Sport edit(file='2.9.R') Error in file(con, r) : cannot open the connection In addition: Warning message: In file(con, r) : cannot open file '2.9.R': No such file or directory getOption('editor') [1] vi edit(file='2.9.R',editor='/opt/local/bin/vim') Error in file(con, r) : cannot open the connection In addition: Warning message: In file(con, r) : cannot open file '2.9.R': No such file or directory vi(file='try') Error in file(con, r) : cannot open the connection In addition: Warning message: In file(con, r) : cannot open file 'try': No such file or directory And here is my interaction with tcsh (my default shell) H2:~% echo $VISUAL /opt/local/bin/vim H2:~% echo $EDITOR /opt/local/bin/vim H2:~% which vi vi: aliased to /opt/local/bin/vim H2:~/4Chap2% ls -ld drwxr-xr-x 11 dbae dbae 374 8 Aug 10:54 ./ What am I doing wrong? Thanks for any help. David __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] lmer (LME4) and survey standard error
Hello, I am using lmer (LME4) to build a model from data for 19 different neighborhoods drawn, in part, from the American Communities Survey (ACS). The ACS data is static while other variables change over the five years under investigation. I am new to mixed effects models and was hoping that someone could suggest a way to include the ACS standard error for each covariate into the model. I currently call lmer as follows: #Y = dependent variable (numeric, vector) #G = neighborhood names (string, vector) #X = independent variables (numeric, matrix) m = lmer(Y ~ (1|G) + X) Thank you, -david __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Recoding multiple TRUE/FALSE columns into a single list of TRUE columns
Hi everyone, I need to recode multiple columns in a dataframe into a single column in a variety of different ways. Often the values will be TRUE/FALSE and I want a list of the columns that are true as in the Result column below: P1 P2 P3 P4 Result 1 0011P3,P4 2 0111P2,P3,P4 3 1000P1 4 0000NA 5 1111P1,P2,P3,P4 I'm still relatively new to R and tend to think in for loops. I'm sure there exists a more concise and elegant solution. Any advice? Happy holidays, -david __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Recoding multiple TRUE/FALSE columns into a single list of TRUE columns
Jim, Wow, that does it! I think I can use strsplit and unlist to convert the string of row names into a R list. thank you! -david __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] side-by-side map with different geographies using spplot
Hello, I want to create side-by-side maps of similar attribute data in two different cities using a single legend. To simply display side-by-side census block group boundary (non-thematic) maps for Minneapolis Cleveland I do the following: library(rgdal) library(sp) Minneapolis=readOGR(../Minneapolis/Census/2010/Census_BlockGroup_GEO/,tl_2010_27053_bg10) Cleveland=readOGR(../Cleveland/Census/2010/Census_BlockGroup_GEO/,tl_2010_39035_bg10) par(mfrow=c(1,2)) plot(Minneapolis) plot(Cleveland) I can display a single thematic map for a city using spplot as follows: spplot(Minneapolis,Thematic_Data_Column) But, calling the function again for Cleveland just overwrites the window. I am unsure how to use spplot's layout tools with two different geographies. Most examples use a single geography and multiple attribute columns. Alternatively, is there a way to use par together with spplot to allow for multiple spplot calls? thank you, -david __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Using SQL IN with RJDBC or RMySQL
Hello, The code below works fine up until I try to use the IN statement in the last line. The proper SQL format is: SELECT * FROM this_table WHERE this_column IN (1,2,3,4,5) But, I think I may be getting something like: SELECT * FROM this_table WHERE this_column IN c(1,2,3,4,5) Which makes no sense in SQL. I think it may be just a matter of string massaging, but I'm not certain. I've tried to build the query using sprintf statements with no luck. I could not get RMySQL to do it either. Any suggestions? thank you, -david library(RJDBC) drv - JDBC(com.mysql.jdbc.Driver, /home/davideps/Software/extlibs/mysql-connector-java-5.0.7-bin.jar,identifier.quote=`) conn - dbConnect(drv, jdbc:mysql://localhost/civicrm, userid,pass) org_table=dbGetQuery(conn,SELECT id,organization_name FROM civicrm_contact WHERE contact_type=?,organization) dbGetQuery(conn,SELECT id from civicrm_relationship WHERE contact_id_a IN ? AND contact_id_b IN ?,org_table$id,org_table$id) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Using SQL IN with RJDBC or RMySQL
thank you! I was able to get it to work with collapse=, On Wed, 2011-10-19 at 01:52 -0500, Jeff Newmiller wrote: paste(SELECT * FROM this_table WHERE this_column IN (, paste(org_table$id, collapse=TRUE),),sep=) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Using SQL IN with RJDBC or RMySQL
Gabor, thank you. I just got the script working using paste but your solution looks good as well. I was not familiar with gsubfn. Appears very useful. -david With gsubfn if you preface your function with fn$ as shown below then it turns on a quasi-perl style string interpolation: library(gsubfn) organization - 3 org_table - fn$dbGetQuery(conn,SELECT id,organization_name FROM civicrm_contact WHERE contact_type=$organization) See ?fn and the gsubfn home page (http://gsubfn.googlecode.com) for details. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] replacing values in one vector with corresponding index from another vector
A quick question for the gurus... Given: a=c(58,73,100,40,70) b=c(40,70,73,100,58,70,70,58) How can I replace the elements of b with the corresponding index numbers from a that start at 1? All values in a are unique. So, I end up with: b=c(4,5,2,3,1,5,5,1) I believe I need to use one of the apply functions in combination with which but I cannot make it work. thank you, -david __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] replacing values in one vector with corresponding index from another vector
thank you! that is straight forward. On Wed, 2011-10-19 at 22:37 +, William Dunlap wrote: match(b, a) [1] 4 5 2 3 1 5 5 1 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Problems with Treemaps in VCD PORTFOLIO
Hi, I am trying to create a treemap for a non-symmetric tree where some branches have more sub-branches than others. I have tried tools in two packages, but have encountered some problems that I hope more experienced users on this list can help address. The PORTFOLIO package offers MAP.MARKET, but this provides only a single depth of branching (category) before rendering the leaves. I could create combined-categories from multiple columns, but these would not be nested--or even necessarily rendered close to each other. The color option provides a similar workaround with similar shortcomings. The VCD package offers MOSAIC, which may be flexible enough to be tweaked into rendering treemaps. One problem that arises is the rendering of empty cells as vertical or horizontal lines, which makes reading the maps more difficult. Empty cells in my case do not convey more information since they are rather intuitive. I would prefer not to render them at all and use location, labeling, and color to make the branching structure clear. Is this possible? Are there other packages or options I should consider? Thank you, -david __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] trying to install plotrix
Here are the error messages: install.packages('plotrix') Warning in install.packages(plotrix) : argument 'lib' is missing: using '/Users/dbae/Library/R/library' --- Please select a CRAN mirror for use in this session --- Error in .install.macbinary(pkgs = pkgs, lib = lib, contriburl = contriburl, : unable to create temporary directory '/tmp/RtmpSTiTJX/downloaded_packages' In addition: Warning message: In dir.create(tmpd) : cannot create dir '/tmp/RtmpSTiTJX/downloaded_packages', reason 'No such file or directory' I can't understand this. Surely install.packages can create the necessary directories if it needs them. I thought there might be a problem of permissions, so I tried the following csh commands on my Mac: Tottie:~/5archive% ls -l /tmp lrwxr-xr-x 1 root admin 11 Aug 7 2008 /tmp@ - private/tmp Tottie:~/5archive% ls -ld /private/tmp drwxrwxrwt 19 root wheel 646 Sep 22 09:13 /private/tmp/ Tottie:~/5archive% ls -l /private/tmp total 48 drwx-- 3 dbae wheel 102 Sep 22 10:41 502/ drwxr-xr-x 2 rona wheel68 Sep 21 16:25 506/ drwxrwxrwx 5 dbae wheel 170 Sep 20 16:47 FLEXnet/ -rw-r--r-- 1 _usbmuxd wheel 0 Sep 18 15:38 MobileDevice.log -rw-r--r-- 1 dbae wheel 1185 Sep 20 22:39 alm.log -rw-r--r-- 1 dbae wheel 2928 Sep 20 22:39 amt.log -rw-r--r-- 1 dbae wheel 0 Sep 20 16:47 cs_cache_lock_502 -rw-r--r-- 1 security wheel 0 Sep 21 13:37 cs_cache_lock_92 drwxr-xr-x 3 dbae wheel 102 Sep 22 10:10 hsperfdata_dbae/ -rw--- 1 nobodywheel36 Sep 22 10:51 objc_sharing_ppc_4294967294 -rw--- 1 dbae wheel 324 Sep 22 11:03 objc_sharing_ppc_502 -rw--- 1 rona wheel84 Sep 21 18:23 objc_sharing_ppc_506 -rw--- 1 security wheel24 Sep 22 09:13 objc_sharing_ppc_92 drwx-- 3 dbae wheel 102 Sep 13 20:04 ssh-6v6iI1izHg/ Thanks for any help. David -- View this message in context: http://www.nabble.com/trying-to-install-plotrix-tp25530666p25530666.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] trying to install plotrix
David Epstein wrote: Here are the error messages: install.packages('plotrix') Warning in install.packages(plotrix) : argument 'lib' is missing: using '/Users/dbae/Library/R/library' --- Please select a CRAN mirror for use in this session --- Error in .install.macbinary(pkgs = pkgs, lib = lib, contriburl = contriburl, : unable to create temporary directory '/tmp/RtmpSTiTJX/downloaded_packages' In addition: Warning message: In dir.create(tmpd) : cannot create dir '/tmp/RtmpSTiTJX/downloaded_packages', reason 'No such file or directory' I can't understand this. Surely install.packages can create the necessary directories if it needs them. I thought there might be a problem of permissions, so I tried the following csh commands on my Mac: Tottie:~/5archive% ls -l /tmp lrwxr-xr-x 1 root admin 11 Aug 7 2008 /tmp@ - private/tmp Tottie:~/5archive% ls -ld /private/tmp drwxrwxrwt 19 root wheel 646 Sep 22 09:13 /private/tmp/ Tottie:~/5archive% ls -l /private/tmp total 48 drwx-- 3 dbae wheel 102 Sep 22 10:41 502/ drwxr-xr-x 2 rona wheel68 Sep 21 16:25 506/ drwxrwxrwx 5 dbae wheel 170 Sep 20 16:47 FLEXnet/ -rw-r--r-- 1 _usbmuxd wheel 0 Sep 18 15:38 MobileDevice.log -rw-r--r-- 1 dbae wheel 1185 Sep 20 22:39 alm.log -rw-r--r-- 1 dbae wheel 2928 Sep 20 22:39 amt.log -rw-r--r-- 1 dbae wheel 0 Sep 20 16:47 cs_cache_lock_502 -rw-r--r-- 1 security wheel 0 Sep 21 13:37 cs_cache_lock_92 drwxr-xr-x 3 dbae wheel 102 Sep 22 10:10 hsperfdata_dbae/ -rw--- 1 nobodywheel36 Sep 22 10:51 objc_sharing_ppc_4294967294 -rw--- 1 dbae wheel 324 Sep 22 11:03 objc_sharing_ppc_502 -rw--- 1 rona wheel84 Sep 21 18:23 objc_sharing_ppc_506 -rw--- 1 security wheel24 Sep 22 09:13 objc_sharing_ppc_92 drwx-- 3 dbae wheel 102 Sep 13 20:04 ssh-6v6iI1izHg/ Thanks for any help. David -- View this message in context: http://www.nabble.com/trying-to-install-plotrix-tp25530666p25530763.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] eliminating control characters from formatted data files
I have a few hundred files of formatted data. Unfortunately most of them end with a spurious CONTROL-Z. I want to rewrite the files without the spurious character. Here's what I've come up with so far, but my code is unsafe because it assumes without justification that the last row of df contains a control character (and some NAs to fill up the record). options(warn=-1) #turn off irritating warning from read.table() df-read.table(file=filename) df.new-df[1:nrow(df)-1,] write.table(df.new,file=filename.new, quote=F) Before defining df.new, I want to check that the last line really does contain a control character. I've tried various methods, but none of them work. I have been wondering if I should use a function (scan?) that reads in the file line by line and checks each line for control characters, but I don't know how to do this either. Thanks for any help David -- View this message in context: http://www.nabble.com/eliminating-control-characters-from-formatted-data-files-tp21847583p21847583.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] eliminating control characters from formatted data files
Murray Cooper wrote: This may be a case of If all you have is a hammer, everything looks like a nail. If all you want to do is remove the last line if it contains a CONTROL-Z, why not use something like perl to process the files? My first thought was to use perl, and this would have saved a lot of my time, in the short run. However, I'm trying to learn R, and the way to learn a programming language is to use it. I've learned a lot about R from this exercise, much more than has been revealed on this forum. David -- View this message in context: http://www.nabble.com/eliminating-control-characters-from-formatted-data-files-tp21847583p21853836.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Difference between a[[i]] and a[i]
I'm sure I've read about the difference between a[[i]] and a[i] in R, but I cannot recall what I read. Even more disturbing is the fact that I don't know how to search the newsgroup for this. All the different combinations I tried were declared not to be valid search syntax. 1. What sort of object can the operators [] and [[]] be applied to? How do they differ? I mean objects in standard R, not in packages that provide conceivable overloading of these operators (if that's possible). 2. Meta-question: how could one search for an answer to this question in the newsgroup? 3. Meta-question: how could one search for an answer to this in the FAQ? 4. Meta-question: how could one search for an answer to this in any of the links one is shown after typing help.start()? Thanks for any help. David -- View this message in context: http://www.nabble.com/Difference-between-a--i---and-a-i--tp21801145p21801145.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Environmental variables
I use a Mac (10.4.11 Mac Os X). In my .tcshrc I define an environmental variable MY. Is it possible to find out its value from inside R? When one loads R for Mac OS X Cocoa GUI written by: Simon Urbanek Stefano M. Iacus are files like .tcshrc read by R? Can I make the value of this environmental variable available to R? Sys.getenv() produces a lot of output, with the values of many environmental variables, but not this one. Do I need a file with the correct value of MY, and have both R and my Unix shell read the same file, or is there a better way to proceed? I want to avoid duplicating the information in source files, as this can lead to a setup that is very hard to maintain. Thanks for any help. David -- View this message in context: http://www.nabble.com/Environmental-variables-tp21782296p21782296.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Making a picture that is wide and small height
How do I make a picture that is a horizontal strip? I tried plot(x=c(1,2,3,4),y=c(1,1,1,1)) #works but screen image is square. pdf(ratio.pdf,height=1,width=6) plot(x=c(1,2,3,4),y=c(1,1,1,1)) I got the following error message: Error in plot.new() : figure margins too large Is it possible to produce an on-screen picture that is a horizontal strip? (I use Mac Os X 10.4.11, and quartz().) What about pdf? I have spent many hours trawling through the online help information and this forum, without success. Where is this explained? Thanks for any help David -- View this message in context: http://www.nabble.com/Making-a-picture-that-is-wide-and-small-height-tp20808401p20808401.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simultaneous plots
Prof Brian Ripley wrote: How about dev.copy2pdf ? I put the following into a script: quartz(...) #many graphics commands dev.copy2pdf(pdf,file=ratio1.pdf) and then sourced the script. I got the error message Error in pdfFonts(family) : invalid arguments in 'pdfFonts' (must be font names) I found the various help files on dev.copy and dev.next, etc very hard to understand. And I have no idea how to fix the error. I'm trying to use defaults as much as I can. Could you possibly give a few more lines of code by way of example, Brian? And is a dev.off() command needed after the dev.copy()? Thanks a lot for your helpful replies to so many queries. They are much appreciated. David -- View this message in context: http://www.nabble.com/simultaneous-plots-tp20802447p20810628.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] simultaneous plots
Is there a good and concise way of making simultaneous plots that are identical, but directed to different devices? I'm writing an R-script that produces a pdf file. I would really like to check visually whether the pdf file shows what I expect. So I would like the same commands to produce a plot on screen. At the moment I'm using cut-and-paste, which is not ideal because any corrections have to be done twice---very error-prone. Thanks David -- View this message in context: http://www.nabble.com/simultaneous-plots-tp20802447p20802447.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] library(gdata) fails
I wanted to install the package gdata. Here are the commands I gave and the responses: install.packages(gdata) Warning in install.packages(gdata) : argument 'lib' is missing: using '/Users/dbae/Library/R/library' trying URL 'http://cran.uk.r-project.org/bin/macosx/universal/contrib/2.8/gdata_2.4.2.tgz' Content type 'application/x-gzip' length 539288 bytes (526 Kb) opened URL == downloaded 526 Kb The downloaded packages are in /tmp/RtmpwY5Qor/downloaded_packages library(gdata) Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared library '/Users/dbae/Library/R/library/gtools/libs/ppc/gtools.so': dlopen(/Users/dbae/Library/R/library/gtools/libs/ppc/gtools.so, 6): Library not loaded: /Library/Frameworks/R.framework/Versions/2.7/Resources/lib/libR.dylib Referenced from: /Users/dbae/Library/R/library/gtools/libs/ppc/gtools.so Reason: image not found Error: package/namespace load failed for 'gdata' I took a look in /Library/Frameworks/R.framework/Versions/2.7/Resources/ and, indeed, there was a directory library but no directory lib. The file /Library/Frameworks/R.framework/Versions/2.8/Resources/lib/libR.dylib is present, but apparently that's not good enough. Any ideas on how I can make this work? I am running R 2.8.0 GUI 1.26 (5256) under MacOsX 10.4.11 on a G4 PPC machine. Thanks for any help. David -- View this message in context: http://www.nabble.com/library%28gdata%29-fails-tp20343452p20343452.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] re flecting a line
Suppose x and y are numeric vectors of the same length. plot(x,y) #scatterplot lmObj1 - lm(y~x) # best fit line abline(lmObj1) # good lmObj2 - lm(x~y) #get best fit but with axes interchanged abline(lmObj2) # not what I want. I want the correct line, drawn on the same graph, but with # response and predictor variables interchanged One way to proceed would be to extract the intercept and slope from lmObj2 and then do the arithmetic to draw the correct line. I'm hoping for a more streamlined method. Is there one? Thanks David -- View this message in context: http://www.nabble.com/reflecting-a-line-tp19422091p19422091.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] options(contrasts)
Code: options(contrasts) $contrasts factor ordered contr.treatment contr.poly I want to change the first entry ONLY, without retyping contr.poly. How do I do it? I have tried various possibilities and cannot get anything to work. I found out that the response to options(contrasts) has class list, but that doesn't help me, although I think it ought to help. Second question (metaquestion). How should I go about finding out the answer to a question like How does one change a single item in a list? My answer to the meta-meta-question is to post to this list. I hope that at least that part is correct. Thanks for any help. David Epstein -- View this message in context: http://www.nabble.com/options%28%22contrasts%22%29-tp19158786p19158786.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] asp and ylim
Maybe what I am missing is how to set the device region mentioned in Brian's email. I have tried various searches, but I haven't had any luck in finding a reference to device region. However, I'm not sure that changing the device region will help if one stays with plot (), because the default seems to be that the physical plot region is approximately square, and I haven't found a way to control the size of the physical plot region. The help files for eqscplot() and xyplot () indicate that they are meant for scatter plots. But lots of plots are not scatter plots. I was only using a scatter plot because Rolf's code did so, and he thought I could do what I wanted inside plot(), which I can't at the moment. Perhaps the best solution is to live with plot() as it is. If I need the picture for a paper, I will export data to Matlab or Mathematica or Illustrator, where I can get the control I want. Thanks for all your help. David On 20 Jul, 2008, at 23:14, Prof Brian Ripley wrote: Take a look at eqscplot() in package MASS for a different approach. You last para forgets that once you have set the device region and the margins the physical plot region and hence its aspect ratio is determined -- see the figures in 'An Introduction to R'. On Sun, 20 Jul 2008, David Epstein wrote: #See David Williams' book Weighing the odds, p286 y - c(1.21, 0.51, 0.14, 1.62, -0.8, 0.72, -1.71, 0.84, 0.02, -0.12) ybar - mean(y) ylength - length(y) ybarv - rep(ybar, ylength) x - 1:ylength plot(x,y,asp=1,xlab=position,ylab=ybar,type=n,ylim=c(-1,1)) segments(x[1], ybar, x[ylength], ybar) segments(x,ybarv,x,y) points(x, ybarv, pch=21, bg=white) points(x,y,pch=19,col=black) With asp=1, the value of ylim seems to be totally ignored, as in the above code. With asp not set, R plays close attention to the value of ylim. This is not intuitive behaviour, or is it? How can I set the aspect ratio, and simultaneously set the plot region? The aspect ratio is one number and the plot region is given by four numbers (xleft, xright, yleft, yright). Logically, these 5 numbers are independent of each other and arbitrary, provided xleftxright and yleftyright. This should give a one-to- one bijection between 5-tuples and plots, determined up to a change of scale that is uniform in the x- and y-dirctions. My code above shows the (to me) obvious attempt, which fails. Thanks David __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. -- 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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] asp and ylim
Since posting the message immediately below, I took Brian's reference to Introduction to R more seriously, and read through the section on graphics. There I found par(fig=c(xleft,xright,ybottom,ytop)). This seems to be setting the device region which Brian pointed out is a fundamental part of the process, but I haven't tried it yet. I expect this is what I need in order to make plot() do what I want. Thanks again, especially to Brian David Maybe what I am missing is how to set the device region mentioned in Brian's email. I have tried various searches, but I haven't had any luck in finding a reference to device region. However, I'm not sure that changing the device region will help if one stays with plot(), because the default seems to be that the physical plot region is approximately square, and I haven't found a way to control the size of the physical plot region. The help files for eqscplot() and xyplot() indicate that they are meant for scatter plots. But lots of plots are not scatter plots. I was only using a scatter plot because Rolf's code did so, and he thought I could do what I wanted inside plot(), which I can't at the moment. Perhaps the best solution is to live with plot() as it is. If I need the picture for a paper, I will export data to Matlab or Mathematica or Illustrator, where I can get the control I want. Thanks for all your help. David On 20 Jul, 2008, at 23:14, Prof Brian Ripley wrote: Take a look at eqscplot() in package MASS for a different approach. You last para forgets that once you have set the device region and the margins the physical plot region and hence its aspect ratio is determined -- see the figures in 'An Introduction to R'. On Sun, 20 Jul 2008, David Epstein wrote: #See David Williams' book Weighing the odds, p286 y - c(1.21, 0.51, 0.14, 1.62, -0.8, 0.72, -1.71, 0.84, 0.02, -0.12) ybar - mean(y) ylength - length(y) ybarv - rep(ybar, ylength) x - 1:ylength plot(x,y,asp=1,xlab=position,ylab=ybar,type=n,ylim=c(-1,1)) segments(x[1], ybar, x[ylength], ybar) segments(x,ybarv,x,y) points(x, ybarv, pch=21, bg=white) points(x,y,pch=19,col=black) With asp=1, the value of ylim seems to be totally ignored, as in the above code. With asp not set, R plays close attention to the value of ylim. This is not intuitive behaviour, or is it? How can I set the aspect ratio, and simultaneously set the plot region? The aspect ratio is one number and the plot region is given by four numbers (xleft, xright, yleft, yright). Logically, these 5 numbers are independent of each other and arbitrary, provided xleftxright and yleftyright. This should give a one-to- one bijection between 5-tuples and plots, determined up to a change of scale that is uniform in the x- and y-dirctions. My code above shows the (to me) obvious attempt, which fails. Thanks David __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. -- 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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] asp and ylim
A really great answer to my concerns! I'll get hold of the Paul Murrell book, and see how far I can get. On 21 Jul, 2008, at 10:48, Martin Maechler wrote: Play around resizing your graphics window.. This is very instructive, with an 'asp = .' using traditional graphics plot(). OK, but I don't know what you mean by asp=.. Does this mean setting asp equal to NULL, or to a default setting? DE Perhaps the best solution is to live with plot() as it DE is. If I need the picture for a paper, I will export DE data to Matlab or Mathematica or Illustrator, where I DE can get the control I want. Hah, you must be kidding! Change kidding to frustrated! For a paper plot, e.g., pdf() as I'd recommend nowadays, you can set the device region by 'width' and 'height' ; and if you really want to use traditional graphics here, do something like ## modified by MM from David Epstein's original example myplot - function(y, yb = mean(y), ylim = c(-1,1)) { ybarv - rep.int(yb, length(y)) x - seq_along(y) plot(x,y, asp=1, xlab=position,ylab=ybar, type=n, ylim = ylim) abline(h = ybar)## instead of segments(x[1], ybar, x[ylength], ybar) segments(x, ybarv, x,y) points (x, ybarv, pch=21, bg=white) points (x, y, pch=19, col=black) invisible() } I learned quite a few things from this code above. y - c(1.21, 0.51, 0.14, 1.62, -0.8, 0.72, -1.71, 0.84, 0.02, -0.12) myplot(y) ## MM: setting device region so that ylim = c(-1,1) about fits pdf.do(asp-ex.pdf, height= 3.3, width=10) myplot(y) pdf.end() I cannot find the functions pdf.do and pdf.end. Are these part of some package that I need to load? Your package? Thanks David __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error in edit(name,file,title,editor)
Can anyone help me with the following attempt to use an external editor from within R vi(file=p286.R) Error in edit(name, file, title, editor) : unable to open file to read edit(file=p286.R) Error in edit(name, file, title, editor) : unable to open file to read I have only recently re-started trying to learn R. (I tried before but failed.) I am working with a Mac MacOsX 10.4.11. I have R 2.7.1 GUI 1.25 (5166) which is the Cocoa version of R. I have been reading Peter Dalgaard's book, which is very helpful, and I have not met any errors so far, so I don't understand why I should suddenly hit this one. Since edit and vi appear to be in the utils package, I did library(utils) to which I got a null response. I have searched the R-help-archives for the error message, and have not found it. I suppose I could use the function source(), but I haven't tried this. Thanks for any help David Epstein __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in edit(name,file,title,editor)
Thanks. The file didn't exist at the time I tried edit, and that was my error. David On 20 Jul, 2008, at 12:44, jim holtman wrote: Make sure you are in the right directory; you can also use the full path names. It is basically saying it can not find the file. Do 'list.files()' to see what is addressable in whatever directory you are in. On Sun, Jul 20, 2008 at 7:19 AM, David Epstein [EMAIL PROTECTED] wrote: Can anyone help me with the following attempt to use an external editor from within R vi(file=p286.R) Error in edit(name, file, title, editor) : unable to open file to read edit(file=p286.R) Error in edit(name, file, title, editor) : unable to open file to read I have only recently re-started trying to learn R. (I tried before but failed.) I am working with a Mac MacOsX 10.4.11. I have R 2.7.1 GUI 1.25 (5166) which is the Cocoa version of R. I have been reading Peter Dalgaard's book, which is very helpful, and I have not met any errors so far, so I don't understand why I should suddenly hit this one. Since edit and vi appear to be in the utils package, I did library(utils) to which I got a null response. I have searched the R-help-archives for the error message, and have not found it. I suppose I could use the function source(), but I haven't tried this. Thanks for any help David Epstein __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] drawing segments through points with pch=1
Please excuse me for asking such basic questions: Here is my code y=c(1.21,0.51,0.14,1.62,-0.8,0.72,-1.71,0.84,0.02,-0.12) ybar=mean(y) ll=length(y); ybarv=rep(ybar,ll) x=1:ll plot(x,ybarv,pch=1) segments(x[1],ybar,x[ll],ybar) What I get is a collection of small circles, with a segment on top of the circles, which is almost what I want. But I don't want the segment to be visible inside any small circle. Is there an easy way to arrange for the segment to lie behind the pch=1 markers, as in hidden line removal, so that the circles remain with nothing inside them? I tried putting the segments command first, but then no segment appeared at all. In general, is there a method of laying a drawing on top of another. I tried inserting add=T as an argument to plot, and R objected strongly. Thanks for any help David Epstein __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] drawing segments through points with pch=1
What I don't like about type=b, also suggested by Paul Smith, is that the segments do not go right up to the little circles---a gap is left, which I don't like. So far, Uwes' solution is what suits me best. However, I understand Brian's objection, though it doesn't apply in my case. The discussion makes me fear that it's a very long road ahead before I can get fine control of R graphics. Thanks David On 20 Jul, 2008, at 14:54, Prof Brian Ripley wrote: On Sun, 20 Jul 2008, Uwe Ligges wrote: You probably want to make your code readable, read ?points and go ahead by making the plot without points (plot(., type=n)), drawing segments and at the end paint points with white background colour in order to overwrite the segments: Except that the background is not necessarily white (and you may want it to be transparent or translucent). It looks to me like lines(type=b) might be what was wanted. y - c(1.21, 0.51, 0.14, 1.62, -0.8, 0.72, -1.71, 0.84, 0.02, -0.12) ybar - mean(y) ll - length(y) ybarv - rep(ybar, ll) x - 1:ll plot(x, ybarv, type=n) segments(x[1], ybar, x[ll], ybar) points(x, ybarv, pch=21, bg=white) Uwe Ligges David Epstein wrote: Please excuse me for asking such basic questions: Here is my code y=c(1.21,0.51,0.14,1.62,-0.8,0.72,-1.71,0.84,0.02,-0.12) ybar=mean(y) ll=length(y); ybarv=rep(ybar,ll) x=1:ll plot(x,ybarv,pch=1) segments(x[1],ybar,x[ll],ybar) What I get is a collection of small circles, with a segment on top of the circles, which is almost what I want. But I don't want the segment to be visible inside any small circle. Is there an easy way to arrange for the segment to lie behind the pch=1 markers, as in hidden line removal, so that the circles remain with nothing inside them? I tried putting the segments command first, but then no segment appeared at all. In general, is there a method of laying a drawing on top of another. I tried inserting add=T as an argument to plot, and R objected strongly. Thanks for any help David Epstein __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. -- 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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] asp and ylim
#See David Williams' book Weighing the odds, p286 y - c(1.21, 0.51, 0.14, 1.62, -0.8, 0.72, -1.71, 0.84, 0.02, -0.12) ybar - mean(y) ylength - length(y) ybarv - rep(ybar, ylength) x - 1:ylength plot(x,y,asp=1,xlab=position,ylab=ybar,type=n,ylim=c(-1,1)) segments(x[1], ybar, x[ylength], ybar) segments(x,ybarv,x,y) points(x, ybarv, pch=21, bg=white) points(x,y,pch=19,col=black) With asp=1, the value of ylim seems to be totally ignored, as in the above code. With asp not set, R plays close attention to the value of ylim. This is not intuitive behaviour, or is it? How can I set the aspect ratio, and simultaneously set the plot region? The aspect ratio is one number and the plot region is given by four numbers (xleft, xright, yleft, yright). Logically, these 5 numbers are independent of each other and arbitrary, provided xleftxright and yleftyright. This should give a one-to-one bijection between 5-tuples and plots, determined up to a change of scale that is uniform in the x- and y-dirctions. My code above shows the (to me) obvious attempt, which fails. Thanks David __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.