[R] omitting integer(0) rows from data frame
I'm not sure if this is correct behavior or not, but it seems counterintuitive to me: dat - data.frame(id=1:5, let=letters[1:5]) # A. omits the first row dat[- 1, ] # B. unexpectedly omits ALL rows dat[- integer(0), ] It would be less surprising if there were no rows omitted in the (B) case. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] compatibility of load() in R 2.15.2
I have some large-ish files that are the output of save() from R 2.15.1, which that version can load() just fine. After upgrading to 2.15.2, load() no longer works on these files. Is this a known issue? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] as.POSIXct questions
The following three calls all produce the same result (my machine is in EST): as.POSIXct(0, tz=, origin=ISOdatetime(1970,1,1,10,0,0)) [1] 1970-01-01 10:00:00 EST as.POSIXct(0, tz=EST, origin=ISOdatetime(1970,1,1,10,0,0)) [1] 1970-01-01 10:00:00 EST as.POSIXct(0, tz=GMT, origin=ISOdatetime(1970,1,1,10,0,0)) [1] 1970-01-01 10:00:00 EST EST is -5:00 GMT. Is it a bug that the third call above does not produce [1] 1970-01-01 5:00:00 EST ? (Under R 2.15.1, Windows 64-bit) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] memory growth with rbind and lapply
This version of my code makes the R process consume unreasonable amounts of RAM: datf - rbind(lapply(mylist, function(item) { with(item, data.frame(col1, col2, col3)) })) This version works fine: datf - lapply(mylist, function(item) { with(item, data.frame(col1, col2, col3)) }) datf - do.call(rbind, datf) Is this to be expected? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] BRugs crash, question
Bob O'Hara rni.boh at gmail.com writes: On 4 April 2012 05:35, Jack Tanner ihok at hotmail.com wrote: samplesBgr(beta) # crash samplesBgr(beta, plot=FALSE) # also crash Have you plotted your histories? I haven't used samplesBgr() much, so I don't know how stable it is (although I do know it's slow). Yes, plotting histories, densities works fine. Above, beta was an array of parameters; plotBgr of a singleton parameter also crashes. samplesStats() calls internal OpenBUGS functions (not R functions), so that would mean saving the whole BUGS run (like externalise in OpenBUGS itself. From you code fit$Stats should give you the same as sampleStats('*'): if you want more use BRugsFit(..., coda=T) and work with the coda object it produces (check the documentation for BRugsFit and coda). I thought about that, but that's not good either. fit$Stats doesn't have parameter names attached. Is there a way to figure out where in fit$Stats[,mean], say, beta ends and theta begins? They seem to be in arbitrary order, as below. The downside to BrugsFit(..., coda=TRUE) is that you don't get DIC (even if you also pass DIC=TRUE). str(fit) List of 3 $ Stats:'data.frame': 194 obs. of 8 variables: ..$ mean : num [1:194] 0.536 0.552 0.037 0.33 0.327 ... ..$ sd : num [1:194] 0.4505 0.214 0.1398 0.0789 0.3183 ... ..$ MC_error : num [1:194] 0.02775 0.011 0.00696 0.00346 0.02071 ... ..$ val2.5pc : num [1:194] -0.341 0.16 -0.215 0.179 -0.291 ... ..$ median : num [1:194] 0.5238 0.5409 0.0331 0.3279 0.3227 ... ..$ val97.5pc: num [1:194] 1.429 1.043 0.327 0.488 0.974 ... ..$ start: int [1:194] 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 ... ..$ sample : int [1:194] 750 750 750 750 750 750 750 750 750 750 ... $ DIC :'data.frame': 3 obs. of 4 variables: ..$ Dbar: num [1:3] 8.94 1661 1670 ..$ Dhat: num [1:3] 17.2 1556 1573 ..$ DIC : num [1:3] 0.686 1766 1767 ..$ pD : num [1:3] -8.26 105.3 97.01 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] BRugs crash, question
(Using BRugs 0.7-5, R 2.14.2 32-bit on 64-bit Windows 7, OpenBUGS 3.2.1) 1. BRugs crashes R for me as follows. Sorry about the lack of detail; please let me know if / how to supply a more useful bug report on this issue. fit - BRugsFit(...) # BRugs and OpenBUGS runs fine, the parameter estimates are reasonable # across 3 chains samplesBgr(beta) # crash samplesBgr(beta, plot=FALSE) # also crash 2. I'd like to run a bunch of models via BRugs, saving the state after each one, like so: for (m in models) { fit - BRugsFit(paste(m$modelname, .bug), ...) save(fit, paste(m$modelname, .RData)) } After I load the saved fitted objects in a new R process, BRugs functions like samplesStats() no longer work. Is it possible to somehow load the saved objects and use samplesStats and other BRugs functions? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] logistic regression
Melrose2012 melissa.patrician at stonybrook.edu writes: alive - (fflies$living) dead - (fflies$living[1]-alive) glm.fit - glm(cbind(alive,dead)~fflies$day,family=binomial) summary(glm.fit) Your call to glm() is not doing what you think it's doing. What you want to do is probably closer to glm.fit - glm(living ~ day, data=fflies, family=binomial) Where ffiles$living has exactly two values, e.g., alive or dead or some other pair. After you do this, you want to examine glm.fit very carefully. Make sure you can interpret all of the output of summary(glm.fit). Until you do, there's no point in trying to plot. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] RFE: vectorized behavior for as.POSIXct tz argument
x - 1472562988 + 1:10; tz - rep(EST,10) # Case 1: Works as documented ct - as.POSIXct(x, tz=tz[1], origin=1960-01-01) # Case 2: Fails ct - as.POSIXct(x, tz=tz, origin=1960-01-01) If case 2 worked, it'd be a little easier to process paired (time, time zone) vectors from different time zones. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] RFE: vectorized behavior for as.POSIXct tz argument
David Winsemius dwinsemius at comcast.net writes: sapply(tz, function(ttt) as.POSIXct(x=x, tz=ttt, origin=1960-01-01),simplify=FALSE) Sure, there's no end of workarounds. It would just be consistent to treat both the x and the tz arguments as vectors. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] perfectionism
I have a named vector: z - c(1, 2, 3, 2) names(z) - c(a,b,c,b) f - c(b,c) I want to know the index in z of the first occurrence of each of the values in f. One implementation is sapply(f, function(x) which(names(z)==x)[1]) b c 2 3 Is which() smart enough to stop when it finds in z the first occurrence of every value from f, or does it search through all the values in z only to report the first one? Are some more elegant ways of writing this code? Just curious. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] perfectionism
jim holtman jholtman at gmail.com writes: match(f, names(z)) [1] 2 3 Jim, thanks so much, that's right on. Patrick, thanks to you too, but yours is not the same as what I asked: z - c(3,4,5,4) names(z)- c(a,b,c,b) z[f] b c 4 5 Yours returns the actual values in z, not the indexes in z, i.e., not [1] 2 3 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] term lists with tm package
I've got a sparse term list of the form term1 doc1 term2 doc1 term3 doc2 etc. I'd like to load this into a Corpus, as defined in the tm package. I was thinking that one way to do this is to iterate over the list building up the i, j, v vectors for a simple_triplet_matrix, and then to use that as an input to as.TermDocumentMatrix. Is that the best way to go? Any hints? Thanks in advance. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Splitting Datasets
Wainscott, Robert LT robert.wainscott at cvn74.navy.mil writes: I want to split dataset ZIDL, into individual datasets based on the string content of variable Dept. There are many, many ways to do this, depending on what you're really after. Here's one: depts = levels(factor(zidl$dept)) for (i in 1:length(depts)) { tiny.dataset = subset(zidl, dept==depts[i]) # now do whatever processing you need with tiny.dataset } Read the Introduction to R and the help page for the subset command, i.e., ?subset __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] altering a call variable from quote()
I have a variable of mode call: b = quote(b==3) b b == 3 Now I want to append x 2 to the value of b. How do I do that? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Import variable labels to data frame columns
AjayT ajaytalati at googlemail.com writes: The data looks like this, Id=1 time=2011-03-27 19:23:40 start=1.4018 end=1.4017 Id=2 time=2011-03-27 19:23:40 start=1.8046 end=1.8047 Something like this would do: lines = scan(file, nlines=1, ...) fields = strsplit(lines[1], \s+, perl=TRUE) k.v.pairs = sapply(fields, function(f) { strsplit(f, =) }) df.row = sapply(k.v.pairs, function(k.v) { k.v[2] }) You can then rbind() the df.row values to get a data.frame. Note that this assumes that all your input records have all the same fields and all in the same order. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] altering a call variable from quote()
Jack Tanner ihok at hotmail.com writes: b = quote(b==3) Now I want to append x 2 to the value of b. How do I do that? Never mind, I figured it out: substitute(b x 2, list(b=b)) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] slow dget
I have a data structure that is fast to dput(), but very slow to dget(). On disk, the file is about 35MB. system.time(dget(r.txt)) user system elapsed 142.931.27 192.84 The same data structure is fast to save() and fast to load(). The .RData file on disk is about 12MB. system.time(load(r.RData)) user system elapsed 4.890.087.82 I imagine that this is a known speed issue with dget, and that the recommended solution is to use load, which is fine with me. If so, perhaps a note to this effect could be added to the dget help page. All timings above using R version 2.12.0 (2010-10-15) Platform: i386-pc-mingw32/i386 (32-bit) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] correlation significance testing with multiple factor levels
[Apologies in advance if this is too statistics and not enough R.] I've got an experiment with two sets of treatments. Each subject either received all treatments from set A or all treatments from set B. I can compute the N pairwise correlations for all treatments in either set using cor(). If I take the mean of these N pairwise correlations, I see that the effects of treatments in set A are much more correlated than the effects of treatments in set B. (Mean correlation for set A is 0.6, mean for set B is 0.1). This is probably wrongheaded, but I'd like to be able to report whether this is a significant difference. I know about cor.test(), but I don't know whether/how I can adapt that for my use case. Thanks in advance for your advice. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] RFE: bQuote like sQuote
I'm writing SQL queries, and it's very handy to be able to use sQuote for string parameter values. It makes me wish that I could use an sQuote-like function for enclosing column names and other identifiers in backticks, i.e., select `foo` from `table`. Obviously I can do this with paste(), I'm just asking for syntactic sugar. Oh, a buglet: the sQuote help page, See Also section, the reference to Quotes {base} is not hyperlinked. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] serialized plot object (2 years later)
About 2 years ago, Tobias Verbeke asked: I am looking for a way to capture the binary string that in normal use of graphics devices will bewritten to (most commonly) a file connection... Is there a way of capturing the binary `jpeg string' [generated by jpeg()]? http://tolstoy.newcastle.edu.au/R/e2/devel/07/09/4276.html Brian Ripley's answer was Nope, unfortunately, they write to files not connections and no R object is involved. Is this still the case? _ Hotmail: Trusted email with powerful SPAM protection. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] a sequence that wraps around
I'd like to have something like seq() where I can pass in a length of the desired sequence and a right limit so that the sequence goes up to the limit and then starts again from 1. # works now seq(from=2, length.out=3) [1] 2 3 4 # what I want seq(from=2, length.out=3, rlimit=3) [1] 2 3 1 # additional examples of what I want seq(from=2, length.out=4, rlimit=3) [1] 2 3 1 2 seq(from=2, length.out=4, rlimit=4) [1] 2 3 4 1 seq(from=2, length.out=3, rlimit=2) [1] 2 1 2 I can write this procedurally, but it seems like there ought to be a cleaner R way of doing it. Thanks in advance for any suggestions. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] a sequence that wraps around
Henrique Dallazuanna wwwhsd at gmail.com writes: Try rep: rep(2:4, length.out = 3, times = 10) That's close, but it doesn't wrap to start at 1. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] a sequence that wraps around
Szabolcs Horvát szhorvat at gmail.com writes: You could use the modulo operator. # additional examples of what I want seq(from=2, length.out=4, rlimit=3) [1] 2 3 1 2 seq(from=1, length.out=4) %% 3 + 1 Ah, that's so slick. You're off to a great start! Huge thanks to you and everyone else who responded. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] turning a list into a function's params
Apologies for the noob question. I need to split setting up a plot and drawing it into two functions. One determines the properties of a plot (data, axis, labels, etc.), and the other plots it (using a preferred device, image dimensions, etc.). get.props = function() { list(x=x, y=y, xlab=foo, ylab=bar) } myplot = function() { props = get.props() plot(props) # Doesn't work } What's the right way to call plot() with all the components of props broken out as separate parameters to plot()? Note that the components of props may be different from call to call (sometimes there's only x data, sometimes there's no ylab, etc.). Thanks in advance for your time and help. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] RConsole processing crashes Rgui.exe
Using R 2.9.2 on Windows XP SP3. 1. Edit ~/Rconsole, and set font = TT Bitstream Vera Sans Mono 2. Start Rgui.exe 3. Go to Edit, GUI Preferfences 4. Rgui.exe crashes Rgui.exe does not crash if I do not access GUI Preferences (i.e., if I just use R), and it does correctly use Bitstream Vera Sans Mono as my font. Nor does it crash if I edit RConsole to set the font back to Lucida Console and then try to access GUI Preferences. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] RFE: vectorize URLdecode
Gabor Grothendieck ggrothendieck at gmail.com writes: URLdecode.vec - Vectorize(URLdecode) On Sat, Aug 29, 2009 at 10:31 AM, Jack Tannerihok at hotmail.com wrote: Could URLdecode be modified to actually process all elements of the vector, not just the first? Sure, that's a fine workaround. But is this a legitimate RFE? Should I file a bug? It seems like the warning itself is coming from charToRaw(), but the rest of URLdecode() might need an upgrade to deal with a charToRaw that processes all the elements in the vector. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] RFE: vectorize URLdecode
In R 2.9.2, URLdecode(c(a%20b, b%20c)) [1] a b Warning message: In charToRaw(URL) : argument should be a character vector of length 1 all but the first element will be ignored Could URLdecode be modified to actually process all elements of the vector, not just the first? Thanks in advance __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] suggestion for paired t-tests
There's a funny inconsistency in how t.test handles paired=T or paired=F. If x and y parameters are lists, paired=F works, but paired=T doesn't. lg=read.csv(my.csv) a = subset(lg, condition==a)[score] b = subset(lg, condition==b)[score] t.test(a,b) t.test(a,b, paired=TRUE) Error in `[.data.frame`(y, yok) : undefined columns selected But this works a=a[,1] b=b[,1] t.test(a,b, paired=TRUE) ... __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.