Re: [R] Automatic detachment of dependent packages
Paul Smith wrote: > Dear All, > > When one loads certain packages, some other dependent packages are > loaded as well. Is there some way of detaching them automatically when > one detaches the first package loaded? For instance, > >> library(sqldf) > Loading required package: RSQLite > Loading required package: DBI > Loading required package: gsubfn > Loading required package: proto > > but > >> detach(package:sqldf) >> >> search() > [1] ".GlobalEnv""package:gsubfn""package:proto" > [4] "package:RSQLite" "package:DBI" "package:stats" > [7] "package:graphics" "package:grDevices" "package:utils" > [10] "package:datasets" "package:methods" "Autoloads" > [13] "package:base" > > The packages > > RSQLite > DBI > gsubfn > proto > > were not detached. The danger here is that after attaching sqldf you might attach some other package that needs, say, DBI, then when your cleanup routine detaches DBI that other package dies because DBI isn't there. The way to do it would be to detach any packages that are only depended on by the package you are detaching. You'd have to call packageDescription("foo", fields="Depends") for currently attached packages to build the dependency tree and then work out which ones you can remove... There's a bit of recursive tree-walking in there, but it should be simple... Ummm... Barry __ 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] R CMD BATCH: cat does not print
Paul Smith wrote: > Thanks, Barry. Indeed, the file myscript.Rout exists and contains the > output of cat. I was expecting a behavior similar to the bash scripts. > And by the way, cannot a R script write only on the console and just > what one tells it to write, likewise bash scripts? Not easily, I think. The 'BATCH' command is really intended for long-running or off-line jobs that may be disconnected from a terminal. Barry __ 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] R CMD BATCH: cat does not print
Paul Smith wrote: > Dear All, > > I am trying to write my first R script. The code is simply > > cat("Hello!\n") > > However, when I run > > $ R CMD BATCH myscript.R > > I do not see "Hello!" on the console. I am using Fedora 7 (Linux) and R-2.5.1. > > Any ideas? > You shouldn't see it on the console! BATCH writes its output to a file. You should find a file called myscript.Rout that does contain the 'Hello!'. Barry __ 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 lines from file
uv wrote: > Hi. I have a text file containing a few hundred lines of numbers, each line > has a different length. For example: > > 1 4 1 1 7 > 3 11 1 1 1 1 1 1 2 > 2 4 1 2 > > And so on. I need to do a simple plot function for each line, and then to > save each plot into a separate file. Is there any way doing it from within > R? You can use read.table with the fill argument: > read.table("file.txt",fill=NA) V1 V2 V3 V4 V5 V6 V7 V8 V9 1 1 4 1 1 7 NA NA NA NA 2 3 11 1 1 1 1 1 1 2 3 2 4 1 2 NA NA NA NA NA and then loop over rows. You may have to tell read.table the maximum number of columns in your data, since it only looks at the first five rows to have a guess at the size. Or you can use readLines("file.txt") to read each line into an element of a character vector, then use strsplit() to break it up: > lapply(readLines("file.txt"), function(s){ as.numeric(unlist(strsplit(s,split=" "))) }) [probably a neater way to do this but I'm not in the mood for playing R golf today] which gives a list: [[1]] [1] 1 4 1 1 7 [[2]] [1] 3 11 1 1 1 1 1 1 2 [[3]] [1] 2 4 1 2 You could then lapply() on this list to do whatever to the elements, in your case the plotting. Barry __ 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] Synchronzing workspaces
Eric Turkheimer wrote: > How do people go about synchronizing multiple workspaces on different > workstations? I tend to wind up with projects spread around the various > machines I work on. I find that placing the directories on a server and > reading them remotely tends to slow things down. If R were to store all its workspace data objects in individual files instead of one big .RData file, then you could use a revision control system like SVN. Check out the data, work on it, check it in, then on another machine just update to get the changes. However SVN doesn't work too well for binary files - conflicts being hard to resolve without someone backing down - so maybe its not such a good idea anyway... On unix boxes and derivatives, you can keep things in sync efficiently with the 'rsync' command. I think there are GUI addons for it, and Windows ports. Barry __ 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] RServe
yoo wrote: > Yea, I found the shutdown function in the java interface as well.. but is > there a way I can send a shutdown command through linux shell? (something > that I can cron?) Write a minimal java program that sends the shutdown command, then run that from your shell... Barry __ 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] [R-pkgs] CRANberries -- An RSS feed about New and Updated CRAN packages
Dirk Eddelbuettel wrote: > but the easiest way may just be to subscribe to Elijah's wonderful 'Planet R' > feed aggregator My favourite RSS reader at the moment is the RSS cat caption generator: http://lol.ianloic.com/feed/dirk.eddelbuettel.com/cranberries/index.rss Barry __ 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] Problems with na.rm=T
Lucke, Joseph F wrote: > Suddenly (e.g. yesterday) all my functions that have "na.rm=" as a > parameter (e.g., mean(), sd(), range(), etc.) have been reporting > warnings with "na.rm=T". The message is "Warning message: the condition > has length > 1 and only the first element will be used in: if (na.rm) x > <- x[!is.na(x)] ". This has never happened before. I don't recall > having done anything that might generate this message. How do I fix Do you have something called 'T': > T=c(1,2,3,4) > mean(x,na.rm=T) [1] 2 Warning message: the condition has length > 1 and only the first element will be used in: if (na.rm) x <- x[!is.na(x)] You should always use 'TRUE' for true and 'FALSE' for false. R makes it harder to shoot yourself in the foot that way: > TRUE=c(1,2,3) Error in TRUE = c(1, 2, 3) : invalid (do_set) left-hand side to assignment help(TRUE) helps: Details: 'TRUE' and 'FALSE' are part of the R language, where 'T' and 'F' are global variables set to these. All four are 'logical(1)' vectors. Barry __ 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] Tools For Preparing Data For Analysis
Chris Evans wrote: > Thanks Ted, great thread and I'm impressed with EpiData that I've > discovered through this. I'd still like something that is even more > integrated with R but maybe some day, if EpiData go fully open source as > I think they are doing ("A full conversion plan to secure this and > convert the software to open-source has been made (See complete > description of license and principles)." at http://www.epidata.dk/ but > the link to http://www.epidata.dk/about.htm doesn't exactly clarify this > I don't think. But I can hope.) > > Thanks, yet again, to everyone who creates and contributes to the R > system and this list: wonderful! Perhaps what we need is an XML standard for describing record-oriented data and its validation? This could then be used to validate a set of records and possibly also to build input forms with built-in validation for new records. You could then write R code that did 'check this data frame against this XML description and tell me the invalid rows'. Or Python code. This is the kind of thing that is traditionally built using a database front-end, but keeping the description in XML means that alternate interfaces (web forms, standalone programs using Qt or GTK libraries) can be used on the same description set. I had a quick search to see if this kind of thing exists already, but google searches for 'data entry verification' indicate that I should really pay some people in India to do that kind of thing for me... Barry __ 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] search path question
Prof Brian Ripley wrote: > > You could do this via a search_file() connection wrapper, but there is a > problem with ensuring connections get closed (which on.exit does here). > I'm not sure exactly what you mean by a 'search_file() connection wrapper', but I have realised that its probably a better idea to write a function that checks a search path for a file and then returns that file: search_file = function(name,path=options()$scanpath,...){ for(p in path){ file=file.path(p,name) if(file.exists(file)){ return(file) } } return(name) } Then you can use that in any filename-using function: options(scanpath=c("/data1","/data2","/etc")) > search_file("passwd") [1] "/etc/passwd" > passwd = read.table(search_file("passwd"),sep=":") > record = scan(search_file("passwd"),what='')[1] Barry __ 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] search path question
Zhiliang Ma wrote: > Thanks, Barry. > In fact, I have a function just like yours, and I'm looking for a simple > alternative function, which is like "path" in Matlab. Dont think it can be done - if you look at the code for 'scan', it disappears off into internal() calls to do the business of finding and reading a file, so you're going to have trouble changing its behaviour in R. You'd have to patch R's C source to implement a search path. Barry __ 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] search path question
Zhiliang Ma wrote: > I want to find a function that can simply add > "C:\inFiles\" into R's search path, so that we I scan a file R will go to > all the search paths to find it. In matlab, path(path,"C:\inFiles") will do > this job, I'm just wondering if there is a similar function in R can do this > job. Something like this (not extensively tested): `sscan` <- function(name, path=options()$scanpath,...){ for(p in path){ file=file.path(p,name) if(file.exists(file)){ return(scan(file,...)) } ## last resort.. return(scan(name,...)) } } Then do: options(scanpath="/tmp") and then: sscan("foo.data") will look for /tmp/foo.data first, then if that fails it will do the 'last resort' which is to look in the current directory. My worry is that this will bite you one day - if you have two files with the same name, it will get the first one in your scanpath - one day this will not be the one you think it is Note this only works with 'scan' - you'll have to do the same thing for read.table, source, etc etc if you want them to behave with a search path too. Unless there's a lower-level approach. But that really will bite you! Barry Barry __ 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] windows to unix
Martin Maechler wrote: >> "Erin" == Erin Hodgess <[EMAIL PROTECTED]> >> on Fri, 25 May 2007 06:10:10 -0500 writes: > > Erin> Dear R People: > Erin> Is there any way to take a Windows version of R, compiled from > source, > Erin> compress it, and put it on a Unix-like environment, please? > Just 'zip' the corresponding directory and copy the zip > file to your "unix like" environment. > > You can take a Windows-compiled R to Unix, but you can't make it work The big unasked question is 'What is this unix-like environment?'. Linux isn't Unix, so maybe you mean that, in which case you'll not make your Windows compiled R run. Not without 'Wine' or some other layer of obfuscation. Barry __ 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 executing instruction every i-th run of loop
Mark W Kimpel wrote: > I am running a very long loop and would like to save intermediate > results in case of a system or program crash. Here is the skeleton of > what my code would be: > > for (i in 1:zillion) I'm a bit worried about this line: > 1:zillion > Error: cannot allocate vector of size 4 zillion bytes hmm, lets try on a machine with a few more zillion bytes of RAM: > 1:zillion Error: result would be too long a vector > Is there an even better way to address my need? Looping over vectors like this involves the uneccesary creation of a long vector. For anything up to a million its probably okay, but once you start getting into the zillions... You can do it with less storage by just having a while loop: > while (i != 100 ){print(i);i=i+1} Many modern computer languages have "iterators" for looping, which abstract all the looping functionality into an object. I started writing something for R a few years ago but never got round to finishing it. It let you do this: myLoop <- loop(N=10,step=1,start=1) while(iterate(myLoop)){ doSomething() } The 'myLoop' object here is the iterator that controls the looping. You can use it to get the iteration number and then use the i %% 1000 test everyone else has told you about by now... Anyway, if anyone has a spare R programmer kicking around and would like all my looper code, just ask... Barry __ 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] division of decimal number
Schmitt, Corinna wrote: > Dear R-Experts, > > how can I divide the number 0.285 with 2. I need a function. > Result: 0.285 / 2 = 0.1425 Just get the / operator: > divide = get("/") > > divide(0.285,2) [1] 0.1425 Is that what you want? Barry __ 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] Setting site-wide default CRAN repository?
Prof Brian Ripley wrote: > On Sun, 18 Mar 2007, Tim Keitt wrote: > >> I can't seem to find this anywhere. How do I set the default CRAN >> repository _site wide_ on a linux box? What I want to do is eliminate >> the pop-up list of repository locations when using >> 'install.packages()'. I know how to do this for a single account. >> Modifying files in /etc/R does not seem to work. (cc me please - I >> think I'm not subscribed). > > Modifying files in R_HOME/etc works, but I have no idea why you think > /etc/R is relevant. > Looks like some distributions package R with that as a configuration directory, for example on my Ubuntu Edgy box: $ ls -l /etc/R total 12 -rw-r--r-- 1 root root 2561 2006-06-20 07:32 Makeconf -rw-r--r-- 1 root root 1422 2006-12-05 16:30 Renviron -rw-r--r-- 1 root root 605 2006-06-20 07:32 repositories This is from an R-base-core package maintained by Dirk Eddelbuettel from the 'Universe' repository (according to the package meta-data). I've since gone from the R 2.3.1 installed by this package to a compiled 2.4.x, so /etc/R/ is no use to me now - perhaps you've done this too (Tim)? Barry __ 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] logging mouse clicks
Seth Roberts wrote: > How can I use R to record the time of a mouse click? Assuming they are mouse clicks on a plot from locator() or identify() then its as trivial as this: > plot(1:10) > locator(1); when=date(); print(when) $x [1] 3.787584 $y [1] 1.978947 [1] "Mon Mar 12 09:34:07 2007" but that only gets you one second resolution, and assumes zero delay between the click and the when=date() function call. Good enough? Barry __ 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] Tracking when an object/function was modified
Duncan Murdoch wrote: > On 3/8/2007 9:06 AM, Duncan Murdoch wrote: >> On 3/8/2007 8:54 AM, Mona Kanaan wrote: >>> Dear R-users, >>> >>> If I would like to track the date when an R-object (specifically an R- >>> function) was modified, how can I achieve that? Furthermore, how can I >>> sort these objects based on date modified? >> R doesn't give you a way to do that. Objects have no timestamps on them. > In R 2.5.0, you'll have the option of keeping source references when you > use source() or parse(), and those include timestamps on files when > that's where the source comes from. > I did once hack the R source code so that on every assignment an attribute called 'lastModified' was created on the assigned object with the current date and time. The resulting compiled R didn't pass tests (I think because it then became impossible for two objects to be 'identical' unless they also had the same timestamp) and it didn't even start up properly for a similar reason. It is useful functionality to have - I envisaged a system like 'make' for R, where if result A depends on data B and result C, and result C depends on data E and F, then you can minimise the amount of computation needed to update A if some of B,C,D,E and F change. I suspect the work to put timestamps in R would be too much. Another possible way of doing it would require all your data to be saved in .RData files, for which timestamps are available (using file.info()). Then your 'Makefile' equivalent would save the target in a .RData file instead of just creating an R object Needs some more thought... Barry __ 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] Generate random numbers up to one
Petr Klasterecky wrote: > You need to specify what 'random' means. If you have any numbers, you > can always make them add-up to 1: > x <- rnorm(100) #runif(100), rpois(100) etc. > x <- x/sum(x) > sum(x) I see a slight problem that may occur with dividing by sum(x) in certain cases Barry __ 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] get more than get
Ingmar Visser wrote: > it's unclear what you want ... > but > get("x")^2 > does not give an error Neither does get("x^2") if you actually have an object called "x^2": > "x^2"=4.01 > get("x^2") [1] 4.01 but that would be a perverse thing to do. Barry __ 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] get more than get
Alberto Monteiro wrote: > This must be a stupid question, but is there any "extension" of get? > > For example: > x <- 10 > get("x") # gives me 10 > get("x^2") # gives me an error 'get' really only gets R objects - you want to evaluate an expression - like this: > x=2 > eval(parse(text="x^2")) [1] 4 Barry __ 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] Double-banger function names: preferences and suggestions
hadley wickham wrote: > What do you prefer/recommend for double-banger function names: > > 1 scale.colour > 2 scale_colour > 3 scaleColour > > 1 is more R-like, but conflicts with S3. 2 is a modern version of > number 1, but not many packages use it. Number 3 is more java-like. > (I like number 2 best) Or you can be lisp-ish and use hyphens (or many other symbols) by quoting: > "scale-colour"=2 > ls() [1] "scale-colour" but that requires further perversions: > get("scale-colour") [1] 2 I like (3), aka camelCase - aka about a dozen other names: http://en.wikipedia.org/wiki/Camel_case - but that's mainly because its widely used in Python, and Python syntax is just marvellous. The more R syntax tends to Python syntax the better. Let's get rid of curly brackets and make whitespace significant... But I digress. As usual. ANytHiNg bUt sTUdLY cApS: http://en.wikipedia.org/wiki/StudlyCaps Barry __ 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] Ubuntu Linux and X11
Oleg Sklyar wrote: > The problem occurs after updating from Dapper to Edgy. Dapper had font > paths: /usr/share/X11/fonts and Edgy, to make the whole font system > unified, moved X11 fonts to /usr/share/fonts/X11. Oleg I think I changed the font path in the X config file *and* added a symlink of /usr/share/X11/fonts to point to /usr/share/fonts/X11 in case any other package had this coded into it. Barry __ 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] R in Industry
Matthew Keller wrote: > I do wonder if anything can/should be done about this. I generally > search using the term "CRAN" but of course, that omits lots of stuff > relevant to R. Change the name in the next major version to 'Rplus'? Barry __ 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] Inquiry
Angie Hernandez wrote: > I came across your website and thought it would be a great resource > to have listed on my friends page. Would you be interested in > exchanging links with my new site? Well, I don't see why we cant make CRAN more like MySpace? Barry [joke] __ 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] Unusual behaviour get.hist.quote
Jerry Pressnell wrote: > I have been running this script regularly for some time. This morning the > following error message appeared. > EWL<-get.hist.quote("EWL",start=(today <- Sys.Date())-350,quote="Cl") > Error in if (dat[n] != start) cat(format(dat[n], "time series starts > %Y-%m-%d\n")) : > > missing value where TRUE/FALSE needed > Looks like this has happened before and spontaneously fixed itself then: http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg77866.html http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg77869.html "Now working, must have been a Yahoo! issue." Barry __ 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] "[[" gotcha
Robin Hankin wrote: > The error is given because after B[[1]] <- a, the variable B is > just a scalar and > not a matrix (why is this?) > Because [[i]] indexes more general vectors, and if you do B[[1]] when B is NULL, R doesnt know if you want B to be a list or a simple vector. If you initialise B as an empty list then R knows: > B=list() > B list() > B[[1]]=b > B [[1]] [,1] [1,]1 Barry __ 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] Regression lines
ken knoblauch wrote: > This should do the trick: > > mind_reader <- function() { > ll <- letters[round(runif(6, 1, 26))] I see my paraNormal distribution package hasn't found its way to CRAN yet: http://tolstoy.newcastle.edu.au/R/help/05/04/1701.html Barry __ 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] image() and nonsquare matrices
Robin Hankin wrote: > How do I draw non-square matrices with image() and get the axes right? > Try 2: > > image(1:20,1:5,a,asp=1,axes=F,xlab="label here") > axis(side=1,pos=0) > # No good because the x axis label is floating far from the x axis. Its only no good if your plot device isnt a similar aspect ratio to your plot... Resize your graphics window and your label will float around... Barry __ 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] dimensions of a all objects
Farrel Buchinsky wrote: > Why will the following command not work > sapply(objects(),dim) > What does it say about the objects list? What does it say about the dim > command? > > Likewise, the following also does not work > all<-ls() > for (f in all) print(dim(f)) 'objects()' returns character strings - the names of objects - then the dim of the character strings are all NULL. I'll assume that's what you are getting at - you've not posted an example or the output you are getting or why it 'does not work'. Maybe you want this: > sapply(objects(),function(x){dim(get(x))}) $f NULL $m [1] 2 5 $x NULL $y [1] 5 2 - where m and y are matrices, f is a function, x is a scalar. Barry __ 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] setting new working directories
Bill Shipley wrote: >> Hello, and Happy New Year. My default working directory is getting very >> cluttered. I know that I should be using a different working directory for >> each project (I work in Windows), but do not know how to go about creating >> different ones and moving back and forth between them. If you make a new directory, then in it put a copy of a shortcut to R (actually to Rgui.exe in R's bin directory) then right click on the shortcut, select 'Properties', and set the 'start in' to your new working directory, you can browse to that directory in windows, double click the R shortcut, and be working in that new working directory with no setwd() needed. Barry PS oh, I mean 'folder' not 'directory' of course, this is Windows... __ 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] writing R extension
Sarah Goslee wrote: > If that's still too complex, you could also save your function to a file > and load it as needed with source(). That will give the user the > same effect. > > source("/path/to/my/stuff/myfiles.R") > > Since you didn't tell us OS or anything else about your system, > it's hard to be more specific. > Yikes No! That will load all the objects into the current workspace. If you save when you quit, you'll end up with umpteen copies of your package code! For simple bundles of functions, it would be better to use save() to save them all to a .RData-type file and then 'attach()' it. This way it doesn't get stuck in your workspace. So: > foo=function(x){x^2} > bar=function(y){y^6} > baz=function(z){z*3} > myFunctions=c("foo","bar","baz") > save(list=myFunctions,file="myFunctions.RData") then quit R, start R in another workspace: > attach("/path/to/wherever/you/put/myFunctions.RData") > foo(2) [1] 4 Building proper _packages_ (never call them 'libraries' - libraries are collections of packages) isn't that hard once you've done it a dozen times, although I'm starting the find the bondage and discipline of packaging R code getting to me. Barry __ 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 is going to take 26 hours - needs to be quicker!
Jenny Barnes wrote: > Dear R-help, > > Thank you for the responses off everyone- you'll be please to hear Duncan > that > using: >> gpcc.array <- array(gpcc.data2[,5], c(144, 72, 46)) > was spot-on, worked like a dream. The data is in the correct places as I > checked > with the text file. It took literally 2 seconds - quite an improvement time > on > the predicted 26 hours :-) > However now you cant tell your supervisor that your data manipulation will take 26 hours - giving you a day to get your Xmas shopping done... Barry __ 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] C structures in R
Prof Brian Ripley wrote: > The short answer is that quite a bit of code, e.g pwilcox and RODBC, > does things like this. You don't need to pass the pointer back to R, > but if you do external pointers are designed for this job. [reads a bit more of 'Writing R Extensions'...] Right yes, this does look like the tool for the job. I'll try and come up with a minimal example that duplicates what I'm doing with raw and pointers, it might be a useful illustration in the documentation. Barry __ 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] C structures in R
Is it safe to call C code from R that mallocs memory for a structure, returning a pointer to that structure via some 'raw()' parameter. Then, pass that pointer to another C routine, and finally free the malloced memory by passing the raw() data to another C routine? I've written some code that does this, I'm just not sure if anything could go wrong. The worst I can come up with is a memory leak if the structure's memory isn't freed - possibly because the R is interrupted. R isn't going to stomp on memory that's been malloced by an included C routine between .C calls though, is it? Barry [[ gory details: R code calls a C routine with .C passing a 4-byte (because 4 is sizeof(char*) in my architecture) 'raw' object, the C code then mallocs the structure and copies the address of the structure into the 4 bytes that the raw object (which appears as a char* in the C routine argument list) points to. This gets returned back to R. Another option would be to create a raw object of the right size to store my structure, pass that to C and not malloc anything in C. But that would mean altering the C code which I want to do as little as possible. gory code available on request. ]] __ 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
Guenther, Cameron wrote: > The code I tried was > text(Dstar+7,120,expression(paste({}D,^*))), but that doesn't work and I > get a syntax error. > > I can't seem to find anything in the help files that explains it. > plot(1:10) > text(8,5,expression(D[obs]^"*")) works for me... Barry __ 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] calling R from cmd line, loading in data sets on the call
Matt Anthony wrote: > Rcmd myprogram.R is clearly documented as unable to take parameters > passed to it ... > It can take parameters from the environment though... H:\> set METHOD=loglik H:\> "c:\Program Files\R\R-2.2.1\bin\R" blah blah > Sys.getenv("METHOD") METHOD "loglik" > Barry __ 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] Error in Calling C++ function from R!!!
>>product<-.C("prodgdot",myx=x,muy=y,myn=NROW(x),myoutput=as.double(0)) > > Error in .C("prodgdot", myx = x, muy = y, myn = NROW(x), myoutput = > as.double(0)) : > C symbol name "prodgdot" not in load table > > > Does anyone know what is the problem? C++ name mangling? http://cran.r-project.org/doc/manuals/R-exts.html#Interfacing-C_002b_002b-code Solution: wrap in extern "C" { ... } as discussed there. Barry __ 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] Conversion from expression to numeric
Paul Smith wrote: >>x <- expression(62/100) >>as.numeric(as.character(x)) > > [1] NA > Warning message: > NAs introduced by coercion > > Any idea about how to deal with the second case? > eval-uate the expression: > x <- expression(62/100) > eval(x) [1] 0.62 Barry __ 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] reaccessing array at a later date - trying to write it to file
Jenny Barnes wrote: > Having tried again your suggestion of load() worked (well - it finished, > which I > assume it meant it worked). However not I am confused as to how I can check > it > has worked. > I typed > >>data.out$data > > which called up the data from the file - but I'm not sure if this is data > from > the file I have just restored as in my "previously saved workspace restored" Remove it from your current workspace: > rm(data.out) then do the load('whatever') again: > load("/some/path/to/data.out.RData") then see if its magically re-appeared in your workspace: > data.out$data But now if you quit and save your workspace it'll be in your workspace again when you start up. So you could consider 'attach' instead of 'load'... Remove data.out from your current workspace, save your current workspace (with 'save()' - just like that with nothing in the parentheses), then instead of load('/some/path/to/data.out.RData') use: > attach('/some/path/to/data.out.RData') This makes R search for an object called 'data.out' in that file whenever you type 'data.out'. It will find it as long as there's not a thing called 'data.out' in your workspace. So if you do attach(...) and then do: > str(data.out) you'll see info about your data.out object, but then do: > data.out=99 > str(data.out) you'll see info about '99'. Your data.out is still happily sitting in its .RData file, its just masked by the data.out we created and set to 99. Delete that, and your data.out comes back: > rm(data.out) > str(data.out) # - your data object again The advantage of this is that data.out wont be stored in your current workspace again. The disadvantage is that you have to do 'attach(...whatever...)' when you start R, and that data.out can be masked if you create something with that name in your workspace. It is a handy thing to do if you create large data objects that aren't going to change much. > Also, is it normal that if I type > >>data.out.RData > > it says > Error: object "data.out.RData" not found Yes, because thats the name of the _file_ on your computer and not the R object. This should be in the R manuals and help files... and I've gone on much longer than I intended to in this email :) Barry __ 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] Translation of R code required
Sarah Goslee wrote: > Since this step works, > > mental$Rx <- factor(mental$Rx, levels=c("VS","IPS")) > > I think that some of the names are right, but perhaps that > one is spelled differently (Centre vs centre, maybe, since > R is case-sensitive?). How do you know that step works? If the dataframe 'mental' doesnt have an 'Rx' column then mental$Rx will be 'NULL', and the factor() function will make a factor out of that... > x=data.frame(foo=1:10) so x is a data frame with one column, called 'foo'. Now lets make levels from a non-existent column, 'bar': > x$bar=factor(x$bar,levels=c('x','y')) No complaints... But try printing 'x' now (by typing x at the command line)... Ick! Try things one line at a time and check the objects created or modified are sensible. Barry __ 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] Progress Monitor in R / looping
Xiaofan Cao wrote: > Hi there, > > I'm writing a program in R that has a few nested loops. I'd like to > monitor the progress when the program is running and be able to estimate > the remaining time. A long time ago I started writing some code to give R something like an 'iterator' object. You could do this: > ml=loop(5) > while(iterate(ml)) + {cat("doing ",iteration(ml)," of ",N(ml),"\n","Ending at ",predictEnd(ml),"\n");sleep(5)} doing 1 of 5 Ending at Wed 25 Oct 2006 11:00:05 BST doing 2 of 5 Ending at Wed 25 Oct 2006 11:00:20 BST doing 3 of 5 Ending at Wed 25 Oct 2006 11:00:20 BST doing 4 of 5 Ending at Wed 25 Oct 2006 11:00:20 BST doing 5 of 5 Ending at Wed 25 Oct 2006 11:00:20 BST you use loop(N) to construct a 1:N loop object, while(iterate(ml)) to loop round it, iteration(ml) to get the current iteration number, N(ml) to get the iteration limit, and predictEnd(ml) to guess when the whole thing will finish. All the information about the loop is encapsulated in the ml object. It needs a chunk of polishing up and nobody seemed that interested in it last time I mentioned it. My particular application was to MCMC, where you could have an MCMC iterator object that was a subclass of my simple loop class, and then you could have methods like if(isBurnIn(ml)) to decide when to start taking samples, or if(!isThinned(ml)) to decide whether to store a sample from a thinned chain. Again, all the info encapsulated in the loop object. Another advantage is that unlike for(i in 1:1000) it doesn't create a vector of 1000 objects... If anyone thinks this is worth me working on then I'll try and find some spare time (hah!) to fix it up. Or if anyone wants to take over, I can throw my code at you at see if it sticks. Barry __ 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] calculate area of outer polygon
Tord Snäll wrote: > Dear all, > Does anyone know of a function that calculates the area of the outer > polygon constructed from a data frame with co-ordinates? For example, If by 'outer polygon' you mean 'convex hull', then look at help(chull). Barry __ 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] write data to pdf
Franco Mendolia wrote: > Hello! > > Is there a possibility in R to save data in pdf-format? > I do not want to save a plot but some lines of simple text. Howabout: R -> RSPython[1] -> ReportLab[2] -> PDF [1] http://www.omegahat.org/RSPython/ [2] http://www.reportlab.org/ ReportLab is a very nice PDF generator library, closer integration with R would be useful... But no I dont have time to do it :( Barry __ 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] processing strings in R
Florian Menzel wrote: > Dear R cracks, We prefer to be called 'R souls'. > I need to process data in R which consist of strings like > > AAABAVVABNN > ABVVNNAA > > What I would like to know is whether there are commands that deliver > -the length of a string > -one specified character of a string (e.g. the 3rd letter) > -that allow to concatenate characters to a string again > s1="AAABAVVABNN" > s2='ABVVNNAA' > nchar(s1) [1] 11 > substr(s1,6,6) [1] "V" > strsplit(s1,'') [[1]] [1] "A" "A" "A" "B" "A" "V" "V" "A" "B" "N" "N" - returns a list. > chars = strsplit(s1,'')[[1]] > chars [1] "A" "A" "A" "B" "A" "V" "V" "A" "B" "N" "N" - back together again: > paste(chars,collapse='') [1] "AAABAVVABNN" __ 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] Block comments in R?
Note that if you only have block comments in a language, its much harder to do this: http://www.xkcd.com/c156.html Barry __ 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] Block comments in R?
Richard A. O'Keefe wrote: > > Commenting code out and providing documentation comments are easily > done with a good editor, although R documentation comments really belong > in files where help() can find them. R documentation comments belong in .Rd files at the moment, but how joyous would it be if they could be included in the .R files? Okay, this is all part of my incessant whining to make R more like Python, but I've found managing separate .Rd and .R files a pain. If .R files could have embedded documentation you'd have one source for code, documentation, tests etc. I did play about with this in the Splus days, attaching documentation strings to functions with attributes, but it was just kludgy without a proper mechanism. Is there an R 3.0 roadmap? Barry __ 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] Block comments in R?
Seth Falcon wrote: > > My wtf feature request is to add a multiline string delimiter ala > Python like """. > Anything that makes R more like Python syntax gets a +1 from me. How about getting rid of curly brackets for blocks and using indenting in R? Time to attack gram.y with a sledgehammer... Barry __ 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] [Fwd: Re: Block comments in R?]
Philippe Grosjean wrote: > > It takes advantage of `!` being not defined for character arguments: > *gasp* how much R code is destined to feature on www.thedailywtf.com in the future? whats the chances of block commenting being included in a future version? and more generally, is there a feature request system, or discussion of whats going in new releases? the nearest I can find is the 'ideas' file: http://developer.r-project.org/ideas.txt which has a bit of a 1990's feel to it (okay, it is in the 'Older Material' section). Barry __ 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] Splitting a character variable into a numeric one and a character one?
>>>Now I want to do an operation that can split it into two variables: >>> >>>Column 1Column 2 Column 3 >>> >>>"123abc" 123 "abc" >>>"12cd34" 12"cd34" >>>"1e23" 1 "e23" >>>... >>> >>>So basically, I want to split the original variabe into a numeric one and a >>>character one, while the splitting element is the first character in Column My first thought on this was to apply the regexp "^([0-9]*)(.*)$" and getting the two parts out. But I dont see a way to get both matches in parentheses out in one go. In Python you just do: >>> re.findall('^([0-9]*)(.*)$',"123abc") [('123', 'abc')] >>> re.findall('^([0-9]*)(.*)$',"1e12") [('1', 'e12')] In R you can get the groups and go gsub on them: > r="^([0-9]*)(.*)$" > gsub(r,"\\1","123abc") [1] "123" But I dont see a way of getting the two values out except as part of one string in gsub - which is right back where you started - or doing gsub twice. Barry __ 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 retrieve results of most recent command?
Yeh, Richard C wrote: > In R, is there an automatic variable that stores the results of the most > recent command or commands? (I am thinking of a behavior like > Mathematica's % result-history substitution syntax.) > Something like .Last.value: > x=sqrt(2) > .Last.value [1] 1.414214 which might get more usage if it was less fiddly to type. Barry __ 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] remotely saving an R session
Jeffrey Horner wrote: > $ kill -USR1 pid > > will save the global environment in the file .RData, but you'll need to > remember the current working directory of the process to find it. Remember? With a computer, you never need to remember! $ ls -l /proc/$pid/cwd is a symlink to the current working directory. Even if you change it with setwd() in R, it is still correct. Barry __ 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 fixed column format
Barry Rowlingson wrote: > If I had a spare day... Or if I'd just read Duncan's message about negative widths in read.fwf. Anyway, I've learnt about readLines() and seek() and reading zip files now, so I can read _anything_ Barry __ 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 fixed column format
Anupam Tyagi wrote: > There are 356,112 records, and 326 variables. It has a fixed record length of > 1283 positions, therefore "cut -b" can not be used. Okay, thats 'large' enough to be awkward... > It would be good to have a facility in R which defines the meta-data: > labelling > and structure of the dataset: positions of variables, their names, their > lables, > their levels (e.g. for ordered choice or group variables: yes, sometimes, no > type responses). This can be saved as a seperate object and passed to a > function > that gets the named varibales from the ASCII file (names of variables to get > can > be given as arguments or as, attaches the meta data and creates a dataframe > with > all the meta-data attached. The meta-data of the dataframe could include notes > at dataframe and variable level, and other information. This information is > passed on to the plotting functions and used when formatting the output of > statistical procedures. I think you need the following functions to build that kind of thing in R: * z = unz("/tmp/file.zip","data.dat") - to create a connection to a file in a zip archive - this saves you having to explicitly unzip it... * open(z) - to open the connection to the file in the zip... * readLines(z,n) - to read 'n' lines from the current position in the file... * seek(z,m*lineLength-1) - to jump to line 'm' ready to read it. Then its just 'substr' and similar string-chopping functions to build up the data from each line you want. If I had a spare day... Barry __ 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 fixed column format
Michael Kubovy wrote: > Please consider saving your data in a way that will make it easier to > read into R. No program can read every dataset. going back to the original post, there seems to be a couple of hanging questions: > None of these seem to read non-coniguous variables from columns; or > may be I am missing something. "read.fwf" is not meant for large > files according to a post in the archives. Thanks for the pointers. I > have read the R data input and output. Anupam. First up, how 'large' is your 'large ASCII file'? How many rows and columns? Secondly, what are 'non-contiguous' variables? Perhaps if you posted the first few lines and columns of the file then we might get an idea of how to read it in. Barry __ 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] about MCMC pack again...
Mariagiulia Matteucci wrote: > Hello, thank you very much for your previous answers about the C++ code. > I am interested in the application of the Gibbs Sampler in the IRT > models, so in the function MCMCirt1d and MCMCirtkd. I've found the C++ > source codes, as you suggested, but I cannot find anything about the > Gibbs Sampler. All the files are for the Metropolis algorithm. $ cd MCMCpack/ $ grep -ir gibbs . produces loads of output, including: ./src/MCMCfactanal.cc:} // end Gibbs loop ./src/MCMChierEI.cc:// and slice sampling and Gibbs sampling to sample from the posterior ./src/MCMCirt1d.cc:} // end Gibbs loop ./src/MCMCmixfactanal.cc: // Gibbs Sampler // ./src/MCMCmixfactanal.cc: } // end Gibbs loop ./src/MCMCoprobit.cc:// Gibbs loop ./src/MCMCordfactanal.cc: // Gibbs Sampler // ./src/MCMCpanel.cc:// simulate from posterior density and return a Gibbs by parameters matrix ./src/MCMCpanel.cc: const int* burnin, const int* gibbs, const int* thin, ./src/MCMCpanel.cc: int Mgibbs = gibbs[0]; ./src/MCMCpanel.cc: int Mtotiter = Mburnin + Mgibbs; ./src/MCMCpanel.cc: Matrix beta_holder(Mgibbs/Mthin,Mp); ./src/MCMCpanel.cc: Matrix D_holder(Mgibbs/Mthin,Mq*Mq); ./src/MCMCpanel.cc: Matrix sigma2_holder(Mgibbs/Mthin, 1); ./src/MCMCpanel.cc: // gibbs loop ./src/MCMCregress.cc: // Gibbs sampler ./src/MCMCregress.cc: // second set of Gibbs scans ./src/MCMCSVDreg.cc:/// Gibbs sampler /// Perhaps some of these are useful? For your info, I know nothing about MCMCpack, I just know how to use grep to search for things. If you are on Windows, you can probably use the Windows File Explorer Search option to look for it. But give me grep anyday... Barry __ 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] set.seed
ronggui wrote: > set.seed is used to set the random number seed. > When we use functions ,say runif, to generate random number ,we almost > get different set of random number. > As for what the i in set.seed(i) should be,I don't think it is a serious > matter. The help for set.seed tells you all you need to know. 'i' must be a single value "interpreted as an integer". You can give it a decimal number, but it makes it an integer: > set.seed(pi) > runif(2) [1] 0.1680415 0.8075164 > set.seed(3) > runif(2) [1] 0.1680415 0.8075164 But not too big an 'integer': > set.seed(1e100) Error in set.seed(1e+100) : supplied seed is not a valid integer In addition: Warning message: NAs introduced by coercion because 1e100 isn't represented as an integer internally (in C/Fortran code, its a 'float' or'double precision' type of thing. For me it takes signed 32 bit integers, so the limits are +/- 2147483647: > set.seed(2147483647) > set.seed(-2147483647) > set.seed(-2147483648) Error in set.seed(-2147483648) : supplied seed is not a valid integer In addition: Warning message: NAs introduced by coercion > set.seed(2147483648) Error in set.seed(2147483648) : supplied seed is not a valid integer In addition: Warning message: NAs introduced by coercion A 32 bit integer gives you over 4 billion possible random sequences. Is that enough? Barry __ 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
Re: [R] Running R
Jonathan Baron wrote: >>5. Copied the Script File R from directory R-2.3.0/bin/R to >>/usr/local/bin/R. >>6. Typed R > > > The instructions say that you should use > > make install > > unless you want to run R from the directory into which you > unpacked it. > Another idea is to have a symlink (here assuming your R was unpacked in /usr/local/src/R-2.3.0): ln -s /usr/local/src/R-2.3.0/bin/R /usr/local/bin/R which essentially runs R from that R-2.3.0 directory. But the 'make install' solution has the advantage that you can delete the source directory where you did the make step to clear out the source and save some disk space. Barry __ 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
Re: [R] Vertical Line on Plot
Peter Lauren wrote: > I was wondering what would be the best way to put a > vertical line on a graph made with plot(). I can get > an horizontal line by plotting a vector where every > element has the same value but it is not as clear how > a vertical line should be done. > abline(v=42) you can use it for horizontal lines too: abline(h=42) and several other things. See help(abline) for details. Barry __ 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
Re: [R] count repetitions
Jean-Pierre GERBAL wrote: > bonjour, > > i have a serie : x<-sample(c(0,1),50,T) > and i want to count the length of each repetition of 0 and of 1 > (00111011100 give 2313145)... > who have an idea ? rle(x) > merci :-) pas de prob. Barry __ 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
Re: [R] metaphoRs - was Can't there be a cd command?
Robert Citek wrote: > Because of that, I would say R is more like a helicopter, a HUEY > perhaps. I vote for unicycle. I've seen people race, skip, climb stairs, go up mountains, dance and even play hockey on unicycles. But when I get on one, all I can do is fall off. I know all these other feats are possible, but I can't do it. That's how a beginner in R feels, isn't it? Barry __ 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
Re: [R] Can't there be a cd command?
> so, _if_ 'cd' would be recognized in future releases, it would'nt do any > harm, would it? > ls() [1] some things here > cd("foo") > ls() [1] some things here Cue shout of: "Hey, it didnt work!" ls() is not to /bin/ls what setwd() is to 'cd'. Gets -1 from me. setwd() makes it clear[er] its a working directory on the file system and nothing to do with R's objects. Barry __ 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
Re: [R] How to write "%" before superscript?
Larsen, Thomas wrote: > I would like to write atom% 15N in the ylab of a plot - "15" should be > written as superscript. How do I put "%" into the expression? It does > not work if I type "%" directly after "atom" in the expression below. > string-quote it? plot(1:10,ylab=expression("atom% "^15*N)) Barry __ 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
Re: [R] www.r-project.org
Romain Francois wrote: > > What about > - place somewhere a div called 'Focus on a package' where we could have > a short presentation of a package, etc ... or for a CRAN task view (to > do that, php would be great, but we can do I don't know perl scripts to > generate static html pages) > - a direct link to download R, which will redirect to the appropriate > CRAN thanks to cookies > - Propose the Table of Contents of the last volume of R news. > - reduce the ratio (size of the head page graphic) / (information > directly accesible) > - move search, task views, and documentation from cran to www > Now maybe my original assessment that a CMS wasn't worth investing in needs a rethink. Suppose www.r-project.org was rebuilt with Plone: http://plone.org/ You'd get: * An easy-to-edit set of accessible web pages * A thorough search of site content * News and Events objects * Replace CRAN with Plone Software Center: http://plone.org/products/plonesoftwarecenter - as used to display plone Products here: http://plone.org/products * Use Plone Help Center for documentation, as used here: http://plone.org/documentation * Use poi to replace the R-bugs system: http://plone.org/products/poi - although plone itself has moved to using 'trac' http://www.edgewall.com/trac/ Of course, it requires time, hosting, and enthusiasm I'm currently lacking all three. Barry __ 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
Re: [R] www.r-project.org
roger bos wrote: > While there is nothing about the r-project site that I would consider fancy, > it is pretty functional. I would be interested to hear more about what you > hope to accomplish by re-doing the web site. Fancy graphics may just slow > down the experience for those not on broadband. After all, the r-help list > doesn't even like HTML in email, so it may not like too many fancy stuff on > their website either. > The frame-based nature of the CRAN pages is slightly problematic, since you click on a menu item and the URL doesn't change. Hence there's no way to send someone a URL that gives them the same view as you'd get if you go to the home page and then click on 'screenshots', for example. Sure you can send them to: http://www.r-project.org/screenshots/screenshots.html but then they dont see the menu. Frames make for simplification of page creation (the menu is in one HTML file and doesn't need to be included on every page) at the expense of usability. Template and content management systems solved this a while ago. It probably wouldn't take long to bash out a serviceable replacement using something like HTML::Mason but then you'd have to find a hosting provider that supported it (or PHP or IYFTLH[1]). I dont think it warrants a full-on CMS given the size of www.r-project.org (not including CRAN stuff). I'd just hack up some m4 scripts and 'include' the menu into a flat file. Perhaps someone could write a web site template system in R... Another option would be to make it completely web 2.0, round the corners, write some ajax, add some blog links, tag soup section[2] Barry [1] Insert Your Favourite Template Language Here [2] Joke __ 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
Re: [R] Running R on Windows 2000 Terminal Services
Gavin Simpson wrote: > Dear list, > > My employer uses a Windows 2000 Terminal Server-based system for its > college-wide managed computer service - computers connect directly to > the WTS servers for their sessions, using a Citrix ICA client. When I > asked them to install R (Windows) on an older version of this service > the IT guys installed it but pulled it for performance issues. I am > trying to get them to try again but receiving little encouragement from > them. 'performance issues'? Well, if you have 100 students running MCMC simulations on one Windows 2000 TS box then you may well have 'performance issues'! Perhaps the TS service isn't intended for people to do real computer work on, but is just for Office apps. Then you come along and want your students to do serious number crunching. At that point the MS Word writers experience what we used to call 'lag'. > Does anyone on the list have experience of a similar set-up? If you do, > I could use that as part of my argument to invest some time in sorting > these issues out. I really want to get the Windows version of R > installed for teaching because at the moment I subject my students to > the rather hostile world of an archaic UNIX session to run R - for them > at least. We have a couple of labs that are similar - we use Wyse Thin Client Xterminals which boot Thinstation Linux from a server and then connect to Windows 2003 TS machines using RDP or Ubuntu Linux boxes using XDMCP. We dont use Citrix ICA. 'Performance issues' will depend very much on what you are doing. As a quick benchmark, last term we had 24 users in a lab all running Windows and running Matlab, Firefox, that kind of stuff. One dual 2.6GHz Xeon Dell with 4G Ram never went above 60% CPU usage. And we had another three similar Dells sitting idle waiting for installation. Sessions with R run regularly in these labs and we've never had 'performance issues'. So possibly your IT support are stalling. Do they regularly say "Have you tried switching it off and on again?" in response to a support query [1]? Barry [1] Catchphrase of the tech support guys in comedy series 'The IT Crowd' __ 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
Re: [R] 3D pie
Rolf Turner wrote: > > People really ***should not*** be encouraged or abetted in > wrong-headedness. Excel is terrible. Pie charts are terrible. > Don't mess with them. Period. > Now I realise the opportunity I missed on April 1st, when I was going to try and (anonymously) post the most flammable R-help posting ever. Something like: "I'm trying to make a library with R 1.6.1 to create a 3-d pie chart in excel but seq(0,1,by=0.1)[4]==0.3 is false". Barry __ 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
Re: [R] How to get the intercept from lm?
Rainer M Krug wrote: > I want to use lm() to get the slope and intercept for several daatasets > and store them in a database. So far so good - but how do I extract the > slope and the intercept from the result from lm()? Read the help for lm - it talks about coefficients rather than slope and intercept, because a linear model can have more (or less) than a slope and an intercept. But basically you want the coef() or coefficients() function on the model: > x=1:10;y=x*1.4+2.5;m=lm(y~x) > m Call: lm(formula = y ~ x) Coefficients: (Intercept)x 2.5 1.4 > coef(m) (Intercept) x 2.5 1.4 > coef(m)[1] (Intercept) 2.5 > coef(m)[2] x 1.4 In general the help for any function should tell you what you can do with its returned object in the 'VALUE' section of the help, but there may also be functions like 'coef()' that give back useful info. For lm() objects there's coef(), residuals(), fitted() etc. Barry __ 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
Re: [R] showing the integrated number by point size
Knut Krueger wrote: > Is there any function to show the points like this example of SPSS? > > http://biostatistic.de/temp/reg.jpg > > The point size should represent the number of data at this point. > plot with cex for the size and pch for solid points: xys=data.frame(x=c(0,0,1,2,2,3,3),y=c(0,1,1,1,2,2,3),s=c(4,1,2,1,1,1,2)) plot(xys$x,xys$y,cex=xys$s,pch=19) or 'symbols', but you need to scale things a bit: symbols(xys$x,xys$y,circles=xys$s/15,inches=F,bg="black") Of course if your data is just (x,y) pairs and you want to work out the counts then that's another problem... Barry __ 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
Re: [R] readline() for passwords?
Ulf Mehlig wrote: > I am using RODBC to access a password-protected database. Is there a > possibility to prevent that the password appears on the screen when > issuing the odbcConnect() command? I thought of something like > readline() without echo. I guess that a getpass()-based solution > wouldn't work for ESS/Emacs, anyway, would it? tk has a way of doing this, so you can build a function that uses the tcltk package. Something like: getPassword <- function(){ require(tcltk) tt <- tktoplevel() pass=tclVar("") label.widget <- tklabel(tt, text="Enter Password") password.widget <- tkentry(tt,show="*",textvariable=pass) ok <- tkbutton(tt,text="Ok",command=function()tkdestroy(tt)) tkpack(label.widget, password.widget,ok) tkwait.window(tt) return(tclvalue(pass)) } The vital part here is the 'show="*"' option in the tkentry widget. Of course this is no use if you aren't in a graphical environment at the time. Barry __ 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
Re: [R] Using optim() with a function which returns more than a scalar - alternatives?
Søren Højsgaard wrote: > I want to numerically maximize a function with optim (maximization > over several arguments). optim() needs a function which returns a > scalar only. However, it could be nice to be able to "take other > things out" from the function as well. I'tried to create an attribute > to the scalar with what I want to take out, but that attribute > disappears in optim(). I looked into the code to see if it could > (easily) be modified such that it could work on a function which > returns e.g. a list or a vector (and then it should be maximized over > the first element). But I gave up... Any suggestion will be > appreciated... Have your function return a scalar value and any additional data in an attribute, and then optimise. Then call your function one more time using the resulting parameters found by optim(), and you'll get the attributes. Pro: No need to mess with the code of optim() Con: One more function call required. Probably not a problem since optim() will have called it a few times anyway. BArry __ 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
Re: [R] Saving surface3d Output
Duncan Murdoch wrote: > Are you talking about surface3d in the rgl package? It doesn't use the > standard R graphics system, so dev.copy() won't help. You need > rgl.snapshot, which can create png files. You'll need some other > utility to convert those to pdf. > Or screen-grab them somehow. In Windows: hit 'Print Scrn' - or is it shift-Print Scrn - then open a graphics program (Photoshop) and create a new picture and then 'Paste' it in. That'll give you the whole screen, so crop to size and save. There are other screen grabber utilities available. On Linux, I'd run Gimp and then capture the graphics window with the 'Acquire... Screen Shot' function. Save as required. Barry __ 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
Re: [R] histogram error: 'x' must be numeric
jia ding wrote: > Then, I use command: > score<- read.csv('file.csv', header = FALSE,sep = ",") > hist(score, main = "score") > > it gives error msg: > Error in hist.default("score", main = "score") : > 'x' must be numeric > > Can any of you know about it explain me why? Have a look at 'score' in R first. You might get this: > score V1 1 31.8450 2 24.5980 3 29.1223 4 24.7150 5 23.1847 6 24.2321 7 25.2995 8 23.4261 9 30.7873 - read.csv reads things in into "data frames" - a bit like a matrix. You've read your data into a data frame with one column, so you can do: hist(score$V1) since V1 is the name of the column. If your data is just one column, then you could do: score = scan("file.csv") and then hist(score) since scan() has read it into a single vector, not a data frame. Barry __ 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
Re: [R] Tranferring R results to word prosessors
Tom Backer Johnsen wrote: > I have just started looking at R, and are getting more and more irritated > at myself for not having done that before. > > However, one of the things I have not found in the documentation is some > way of preparing output from R for convenient formatting into something > like MS Word. Well whatever you do, don't start looking at LaTeX, because that will get you even more irritated at yourself for not having done it before. LaTeX is to Word as R is to what? SPSS? I've still not seen a pretty piece of mathematics - or even text - in Word. Barry __ 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
Re: [R] [R-sig-Geo] envi clone in R
Wladimir Eremeev wrote: > Hello all, > > Research Systems (www.rsinc.com) have developed and distributes the > language IDL, > and the GIS ENVI, written in IDL. I find it hard to believe they wrote it all in IDL! I'm guessing its probably scriptable in IDL, but underneath its written in something else... I could be wrong though! > To my oppinion, R language is superior, compared to IDL, in all aspects. > However, ENVI is the rather convenient and feature rich tool. I clicked on 'Product Documentation' on the ENVI site and it wanted me to log in or create a new user. To see the documentation? To find out what the program is about? Oh, what I really wanted was the Feature Tour.. > Is anyone aware about any work, dedicated to the creation of > something, similar to the ENVI, but in R? Last year I looked at GIS-R linkages, with the added criteria of being open source and cross-platform. There are now a few free GIS packages that can do this kind of thing, with a little added glue. I settled on OpenEV - it has vector and raster support, its extensible in Python and uses Gtk for dialogs which you can customise. All I needed was to get Python talking to R, so I wrote some Python bindings to Rserve. Now I've got a GIS with a menu that drops down, you choose the point layers you want to work on, click 'Go', and R does some analysis that ends up as a raster layer back in the GIS. The user doesnt care that R did it. Other GIS solutions are available! Barry __ 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
Re: [R] large lines of data
Sara Mouro wrote: > Dear All, > > > > I have to enter many lines of data in the same object. > > I usually use copy-paste to transfer data from an Word file to R. > > What is the best way to do that? Use 'Save As' to save your Word file - or rather just the data section - as a plain text or Ascii file. Then read into R with scan() or read.table() as appropriate. Barry __ 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
Re: [R] question about binary data file and data.frame
[EMAIL PROTECTED] wrote: > I have a binary file with data sequence in the order What do you mean by 'binary file'? > [age,weight][age,weight] How are age and weight encoded in this 'binary file'? > I know the length of the data and I want to load it into a > data.frame. of course a way to do this is to read age and weight > seperately and then use cbin(age,weight) to combine them into a > dataframe, but is there a better solution? > Is it really an ASCII file? With age and weight separated by commas, and then age-weight pairs separated by spaces? Are there really square bracket pairs in there too? Or is it really a binary file, a series of 4 or 8-byte binary representations of age and weight? Barry __ 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
Re: [R] Sos! "Lines" doesn't add plots to an existing plot...
Michael wrote: >>plot(testError, col="red") >>lines(testVar, col="black") > > > Only one plot (the red one) appear on the Window, the black line did not > appear...what's wrong? Hang on a second, just let me hack into your machine so I can find out what 'testError' and 'testVar' are I'm guessing that the lines for 'testVar' are outside the range of the plot formed by 'testError', but this is a guess and it could be a bazillion other things. We have no way of knowing unless you tell is the first things about testError and testVar. For starters, try telling us what: str(testVar) str(testError) summary(testVar) summary(testError) say. Barry __ 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
Re: [R] How do you convert this from S Plus to R - any help appreciated . thanks
Briggs, Meredith M wrote: > exportData(MU.Cost,paste("C:/RAUDSL/S",as.character(MU.Cost$Run.Id[1])," > .",as.character(MU.Cost$MU.Id[1]),".MU.PRICE.OUTPUT.txt",sep=""),append > = FALSE,type="ASCII",quote=FALSE) Looks like perfectly good R to me. Except there's no exportData function. I assume this is an Splus function that R doesn't have, in which case telling us what it does might help. What does the Splus manual have to say about it? I'm guessing R's write.table might be of use. Assuming its exportData that has you stuck - the other bits should allwork in R no problem, all it does is construct a path from parts of the MU.Cost object. Barry __ 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
Re: [R] is.integer() function
Duncan Murdoch wrote: > Here "numeric vector" is being used in the R-specific technical sense as > a vector of double precision values, so the documentor was trying hard > to be precise. The problem is that English also admits the > interpretation in a non-technical sense as a vector of numbers. I > believe your country is to blame for the language. :-) But can "numeric vector" in the "R-specific technical sense" also mean a vector of integer (representation) values? It passes is.numeric() and is.vector(): > x=1:3 > is.numeric(x) [1] TRUE > is.vector(x) [1] TRUE > is.integer(x) [1] TRUE Unless by the "R-specific technical sense of 'numeric vector'" you dont mean something for which is.numeric() and is.vector() are both true. Which is perverse. But then large chunks of R are. Anyway, is this right: is.RSpecificTechnicalSenseNumericVector=function(v){is.numeric(v) & is.double(v)} Barry __ 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
Re: [R] is.integer() function
Gabor Csardi wrote: > Becaues is.integer shows the internal representation, which is not an > integer but a double (real number). Some functions create integer vectors, Some functions that you might think create integer vectors and even seem to say they create integer vectors dont create integer vectors: 'ceiling' takes a single numeric argument 'x' and returns a numeric vector containing the smallest integers not less than the corresponding elements of 'x'. > ceiling(0.5) [1] 1 > is.integer(ceiling(0.5)) [1] FALSE > is.integer(1:3) [1] TRUE > is.integer(ceiling(1:3)) [1] FALSE This could possibly be a documentation problem, since ?ceiling is using 'integer' in the sense of 'whole number', whereas ?is.integer is concerned with internal representation (aka 'storage mode') This seems to be an endless source of confusion to anyone who didn't start their programming days in Fortran, C, or assembly language (or other strongly-typed language, I guess). Barry __ 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
Re: [R] Number of replications of a term
Laetitia Marisa wrote: > Hello, > > Is there a simple and fast function that returns a vector of the number > of replications for each object of a vector ? > For example : > I have a vector of IDs : > ids <- c( "ID1", "ID2", "ID2", "ID3", "ID3","ID3", "ID5") > > I want the function returns the following vector where each term is the > number of replicates for the given id : > c( 1, 2, 2, 3,3,3,1 ) One-liner: > table(ids)[ids] ids ID1 ID2 ID2 ID3 ID3 ID3 ID5 1 2 2 3 3 3 1 'table(ids)' computes the counts, then the subscripting [ids] looks it all up. Now try it on your 40,000-long vector! Barry __ 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
Re: [R] [Rd] Display an Image on a Plane
[more an R-help than R-dev thing] Ben Bolker wrote: > Labbe, Vincent (AEREX drdc-rddc.gc.ca> writes: >>I am new to R and I would like to display an image on a plane in a 3D plot, >>i.e. I would like to be able to specify a theta and a phi parameters like in >>the function persp to display a 2D image on an inclined plane. >can't think of an easy way to do this: what do you mean by "image" > exactly? A bitmapped image from a file? Or something like the > output of image()? If the latter, you may be able to cobble together > something > using the trans3d() function (i.e., manually recreating an image() > by drawing colored squares, but transforming each of the to the 3D > perspective). If the former, you might be able to do something with > the pixmap package ... > I think once you get into doing fancy visualisations like this then you may find a solution outside of R. The front runners are probably: http://www.opendx.org/ http://mayavi.sourceforge.net/ http://www.llnl.gov/visit/ Not sure of the R-integration possibilities here, but you'd probably have to export your data to a file and pick it up in the visualisation package. You'd also have to build your complete plot from scratch in the package if its really just an R persp() you want to add to. Most of these packages can do persp()-style plots without even *ahem* persp-iring. Some of these packages are scriptable from Python or Tcl, which could make for tighter integration with R. A full R 3d graphics device would be nice... Rgl could be there already! - http://rgl.neoscientists.org/Gallery.html Barry __ 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
Re: [R] For each element in vector do...
Andrej Kastrin wrote: > vector A: 0 1 2 3 0 4 5 > vector B: 0 2 3 4 0 5 6 > > What's the right way to do this. I still have some problems with for and > if statements... > ?ifelse perhaps... > A [1] 0 1 2 3 0 4 5 > B=ifelse(A>0,A+1,0) > B [1] 0 2 3 4 0 5 6 does a sort of element-wise if-else thing. However it evaluates A+1 for all elements. If you have something like: B = ifelse(A>0, slowFunction(A), 0) and not very many zeroes in A, you'll be doing a lot of slowFunction() work for nothing. In which case: B = numeric(length(A)) B[A>0] = slowFunction(A[A>0]) will only pass the necessary values of B to slowFunction() and put them into the right parts of B. B is initially all zeroes. It is left as an exercise to work out the better alternative to: ifelse(A>0, slowFunction(A), otherSlowFunction(A)) Barry __ 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
Re: [R] Kite diagrams
Par Leijonhufvud wrote: > I teach biology, and would like to show the students how to use R for > some statistical assignments. One of those is to make a kite diagram > (for example as seen in > http://www.medinavalleycentre.org.uk/images/Bembri1.jpg). Is there any > way to create one using R? I did a help.search("kite") and looked on the > r-project HP with no luck. The joy of R is that of course there is a way to create these - you just have to write the code! The data are, I guess, on the X-axis a discrete set of distance points, (identical for each species?), and then for each species an abundance measurement - is this continuous or discretized, or does it only take the values shown on the key ('ACFOR' = Abundant, Common, Frequent, Occasional, Rare??). Looking at the kites I'd guess the data are numbers and nearly-continuous. Anyway, you can use plot() with type='n' to set out a blank plot with X-axis according to your distance scale and a Y-axis of something like 1:Nspecies, then use the polygon() function to draw the little kites, making sure you dont draw anything between separated kites. This probably means several polygon() calls or sticking NA's in the coordinates. Adding the little cross-section of the shoreline at the top is possible too... > Previously when the course was taugh the students have either abused MS > Exel or drawn the diagrams by hand. Whereas now they can just rely on the goodwill of R-help to do it! :) Why do these interesting questions always seem to occur on a Monday morning when I really dont want to get on with the stuff I'm supposed to be doing Barry __ 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
Re: [R] Getting the numeric value of difftime
Christian Neumann wrote: > Hi folks, > > I have a small, maybe newbie, question concerning date operations. > The follwing snippet > > date1 = "2005-11-20"; > date2 = "2005-11-17"; > difftime(date1, date2) > > results in "Time difference of 3 days". How can I extract just the > numerical value 3? > As a workaround I apply mean() on the result which gives me just a > numerical value. If you want to get one thing _as_ another the R-ish solution is nearly always to do as.another(oneThing). You want it numeric? You got it! > date1 = "2005-11-20"; > date2 = "2005-11-17"; > difftime(date1, date2) Time difference of 3 days > as.numeric(difftime(date1, date2)) [1] 3 Barry __ 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
Re: [R] [Rd] Multiplication
[crossed over to r-help since its not a bug and not a devel thing any more] Thomas Lumley wrote: > So is -2^2. The precedence of ^ is higher than that of unary minus. It > may be surprising, but it *is* documented and has been in S for a long > time. And just about every other programming language: Matlab: >> -2^2 ans = -4 Maxima: (C1) -2^2; (D1) - 4 Fortran: print *,-2**2 -4 Perl: $ perl -e 'print -2^2' 4294967292 Oops. I mean: $ perl -e 'print -2**2' -4 The precendence of operators is remarkably consistent over programming languages over time. It seems natural for me now that ^ is done before unary minus, but I don't know if that's because I've been doing that for 25 years or because its really more natural. Anyone got a counter example where unary minus is higher than power? Barry __ 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
Re: [R] Wikis etc.
Jonathan Baron wrote: > And I was thinking of setting up a Wiki with one page per > function. (Given that there are now hundreds or thousands of > functions, setting this up would have to be automated.) One page per R manual page file would probably suffice. You could do something along the lines of the Zope book, where users can add comments but you can browse with comments off: http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/AdvDTML.stx then toggle the 'Com On' button. This is less of a wiki and more of an annotation service. but I think you'd run into problems with losing all the annotation when a new R version comes out. Barry __ 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
Re: [R] plot
Rhett Eckstein wrote: > Dear R users: > > >>C1 > >timeX1 > 1 0.5 6.296625 > 2 1.0 10.283977 > 3 1.5 12.718610 > 4 2.0 14.112740 > 5 3.0 15.053917 > 6 4.0 14.739725 > 7 6.0 12.912230 > 8 8.0 10.893264 > 9 0.5 6.289166 > 10 1.0 10.251247 > 11 1.5 12.651346 > 12 2.0 14.006958 > 13 3.0 14.870618 > 14 4.0 14.487026 > 15 6.0 12.66 > 16 8.0 10.474695 > >>plot(C1,type="l") > > In the plot, there is a straight line between time=0.5 and time=8, > If I do not want the straight line, what should I do? What do you want? Looking at your data makes me think you want two separate lines, in which case you probably want to do a plot() followed by a lines(), or better still with a slight rearrangement of your data you can use matplot() which is designed for doing several lines (or sets of points) in one plot. Something like: matplot(C1$time[1:8], cbind(C1$X1[1:8], C1$X1[9:16]), type='l') but you may also want to rearrange your dataframe. Try: C2 = data.frame(time=C1$time[1:8], X1=C1$X1[1:8], X2=C1$X1[9:16]) so it looks something like this (with random numbers): C2 time X1X2 1 0.5 0.754514622 0.2571699 2 1.0 0.006056693 0.7252758 3 1.5 0.694433716 0.5532185 4 2.0 0.201020796 0.4590972 5 3.0 0.114225055 0.8226671 6 4.0 0.569609820 0.9712040 7 6.0 0.306018526 0.6795705 8 8.0 0.142492724 0.3452476 then matplot becomes: matplot(C2$time, cbind(C2$X1,C2$X2),type='l') - sticking an NA in the middle (as suggested just now) seems a bit kludgy! Baz __ 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
Re: [R] scoping issues?
tom wright wrote: > Browse[1]> mean(amps[1],amps[2],amps[3],amps[7],amps[8]) > [1] 1 For starters, this just returns mean(amps[1]). 'mean' computes the mean of the first argument, the others are slurped up by '...' and in this case thrown into the bin. You want to do mean(c(amps[1],amps[2],amps[3] and so on. Wrap them into a single vector. For main course, I think you can do it like this: getAR <- function(v.amps){ sv=sort(v.amps) sum(sv[1:3])/mean(sv[-(1:3)]) } - which computes the ratio of the sum of the three biggest over the mean of the remainder. Which I think is what your code looks like its trying to do! An example input/output would be nice. My code gives: > amps<-c(1,2,3,3,3,2,1) > getAR(amps) [1] 1.454545 but I still dont know if that's what it should be! For dessert, I don't think its a scoping issue, I think you've not really explained what the problem is... Baz __ 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
Re: [R] R formatting
Peter Dalgaard wrote: > > It doesn't calculate it though... ;-) > My previous example is a bit ugly - this one looks nicer: f1=function(n){ -1 x = --- n return(x) } And it returns f(1) as -1/1 and f(-1) as -1/-1 as well. __ 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
[R] R formatting
While mucking about with semicolons and line endings I wrote this little piece of mildly obfuscated R code: f1=function(n){ x = 1 --- n return(x) } [best viewed with a proportionally-spaced font] f1(1) does indeed return 1/1. Baz __ 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
Re: [R] What is wrong with this FOR-loop?
Christian Hennig wrote: >>run_rows<-seq(0,1,0.05) >>run_cols<-seq(0.3,0.6,0.05) >> >>res<-matrix(NA,length(run_rows),length(run_cols)) >> >>for(i in 1:length(run_rows)) >>{ >>for(j in 1:length(run_cols)) >>{ >>res[i,j]=run_rows[i]+run_cols[j] >>} >>} Or the one-liner: res = outer(run_rows,run_cols,"+") but maybe its good to learn about array indexing first... Barry __ 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
Re: [R] What is wrong with this FOR-loop?
Serguei Kaniovski wrote: > Hi, I have a more complex example, but the problem boils down to this > FOR-loop not filling in the res-matrix > for(i in run_rows) > { > for(j in run_cols) > { > res[i,j]=i+j have a look at what i and j are in such a loop: > for(i in run_rows){ + print(i) + } [1] 0 [1] 0.05 [1] 0.1 [1] 0.15 [1] 0.2 [1] 0.25 - and then you are trying to fill res[0.05,.3] and so on. Its not going to work! You probably want something like: for(i in 1:length(run_rows)){ so that i is 1,2,3, then you do: res[i,j] = run_rows[i] + run_cols[j] within your loop... ...which in fact can be done in one line, but you need to learn about matrix indexing first! Barry __ 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
Re: [R] Mass 'identify' on 2d-plot
Evgeniy Kachalin wrote: > What is ability in R to graphically (per mouse) define some area and to > select all the cases felt in it? > > 'identify' is OK for 5-10 cases, but what if cases=1000? You can use 'locator' to let the user click a number of points to define a polygon, and then use one of the point-in-polygon functions provided by one of the spatial packages to work out whats in your polygon. Look at splancs, spatstat, sp - pretty much anything beginning with 'sp' - on CRAN. In splancs you can just do: poly = getpoly() - which lets the user draw a polygon on screen, then: inPoly = inpip(xypts,poly) points(xypts[inpip,], pch=19,col="red") and that will plot the selected points in solid red dots. I don't think there's a way to draw a freehand figure on an R plot, you have to go click, click, click, and draw straight lines. Barry __ 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
Re: [R] what is best for scripting?
José Matos wrote: > In this case I prefer to use rpy (look for it in sourceforge), it > allow to call R directly from python, > with the main advantage that the resulting objects are really python > objects, and vice-versa > calling R with python objects will convert them to R objects. > > It works quite well for me. :-) And if anyone wants to call Rserve[1] from Python then I've written some client code that does most of the useful[2] Rserve stuff. If anyone wants to have a go with it (essentially, be a beta tester) let me know. Baz [1] http://stats.math.uni-augsburg.de/Rserve [2] Well, useful to me. __ 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
Re: [R] Calculating the 2th power of a vector
Amir Safari wrote: > I simply want to calculate the 2th power of a vector without changing the > sign of values. How it is possible in R ? I'm not quite sure what you mean, but maybe: > x [1] -4 -3 -2 -1 0 1 2 3 4 > x^2 [1] 16 9 4 1 0 1 4 9 16 - that obviously makes everything positive (unless any of x are complex!) So do: > x^2 * sign(x) [1] -16 -9 -4 -1 0 1 4 9 16 to keep the sign. Is that what you want? Baz __ 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
Re: [R] How define global Variable?
[EMAIL PROTECTED] wrote: > R> a <- "old" > R> test <- function () { a <- "new" } > R> test() > R> a # shoud be "new" > > This doesn't work. I would like to modify the variable "a" in a > procedure. How can I do that. You may like to modify the variable, but who else wants you to? Functions should have zero side effects whenever possible. Wanting to muck with global variables is a big red flag that something is wrong with your program. It will become hard to debug or follow what is going on. Imagine, in six weeks time you look at: a = "old" test() if (a == "new"){ doSomething() } - well, its not obvious that 'a' could possibly have changed to "new". Sure you could look at test() and see, but then test() could call something else that calls something else and then somewhere else 'a' is set. It can make for very very messy code. The solution is to return anything that changes. Example: a = "old" test=function(){return(list(a="new"))} ttt = test() a = ttt$a That's probably the recommended way of returning multiple things from a function too - wrap them in a list and get them. Modifying global variables is very rarely the Right Thing. I'm sure someone will come up with a solution but it'll probably involve frames and environments and other messy magic language stuff you really dont want to get into. Keep It Simple, Sunshine. Barry __ 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
Re: [R] PNG-import into R
Tuszynski, Jaroslaw W. wrote: > Also does anybody know how hard would it be to tap into C code needed for > 'read.jpeg', 'png' and 'jpeg' functions to write 'read.png' , 'write.png', > and 'write.jpeg' functions? Much, much harder than using ImageMagick to convert to one of the formats that R can read. > system("convert foo.png foo.pnm") > foo = read.pnm("foo.pnm") ImageMagick is here: http://www.imagemagick.org/script/index.php And is normally already installed on modern Linux distributions. But yes, native reading without conversion is sometimes preferable. Baz __ 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
Re: [R] repeated %in%
Whoops The code I just posted only tested if all the subsequent elements were subsets of the first, it didn't check all the sequential subsets! Too cold to think straight here today... Baz __ 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