I was going to suggest
AB - df[c(A,B)]
ls2 - array(split(df$C, AB), dim=sapply(AB, nlevels), dimnames=sapply(AB,
levels))
which produces a matrix very similar to what Duncan's by() call produces
ls1 - by(df$C, df[,1:2], identity)
E.g.,
ls2[[a,X]]
[1] 1 2
ls1[[a,X]]
[1] 1 2
-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of William Dunlap
Sent: Wednesday, August 10, 2011 10:05 AM
To: Duncan Murdoch; Frederic F
Cc: r-help@r-project.org
Subject: Re: [R] How to quickly convert a data.frame into a structure of lists
I was going to suggest
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of R. Michael
Weylandt
Sent: Thursday, August 11, 2011 10:09 AM
To: Srinivas Iyyer
Cc: r-help@r-project.org
Subject: Re: [R] help with loops
No problem,
By the way, you
You can get fairly bad results from solve() and
other linear algebra routines without warning.
E.g., the following function makes a 2 by 2 matrix which
would have determinate 1.0 if we had infinite precision
arithmetic and actually will produce one of determinant
1 on a computer if you use the
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of ivo welch
Sent: Monday, March 21, 2011 3:43 PM
To: r-help
Subject: [R] Merging by() results back into original data frame?
dear R experts---I am trying to figure out what the
filter(), in the stats package, can do moving
averages (with any weights).
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Tonja Krueger
Sent: Tuesday, March 22, 2011
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Jack Tanner
Sent: Sunday, March 27, 2011 9:14 PM
To: r-h...@stat.math.ethz.ch
Subject: Re: [R] altering a call variable from quote()
Jack Tanner ihok at hotmail.com writes:
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter
Sent: Tuesday, March 29, 2011 10:07 AM
To: Petr Savicky; cddesjard...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] Creating 3 vectors that sum to 1
So you
Terry,
The fact that model.frame attaches xlevels to
the terms based on factors in the input data.frame
(and attaches dataClass based on the input data.frame),
but the subsequent call to model.matrix is responsible
for turning character vectors in the data.frame into
factors (and then into
The %...% operators are not a panacea.
they have the same precedence as `*`
and `/` (I think) so you get things like:
x %-% 10 - 8 # %-% has higher precedence than -
[1] 2
x # not what you thought it would be
[1] 10
x %-% 10 ^3 # but lower than ^
[1] 1000
x # this is what you
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Stavros Macrakis
Sent: Monday, April 04, 2011 1:15 PM
To: r-help
Subject: [R] General binary search?
Is there a generic binary search routine in a standard library which
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Dan Abner
Sent: Tuesday, April 05, 2011 12:58 PM
To: r-help@r-project.org
Subject: [R] IFELSE function
Hello everyone,
This IFELSE function call is not working
Put an open brace in the first line of your file
and a close brace in the last line.
I encourage people with scripts long enough that this
is a problem to divide up their work into functions
that a shorter script calls. (This is akin to UCSD
Pascal on the Osbourne II that refused to deal with
a
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Benjamin Tyner
Sent: Friday, April 08, 2011 6:48 PM
To: r-help@r-project.org
Subject: [R] best practice(s) for retrieving a local variable
from a closure
Greetings,
Say I
I have two suggestions to speed up your code, if you
must use a loop.
First, don't grow your output dataset at each iteration.
Instead of
cases - 0
output - numeric(cases)
while(length(line - readLines(input, n=1))==1) {
cases - cases + 1
output[cases] -
[see below]
From: Frederik Lang [mailto:frederikl...@gmail.com]
Sent: Thursday, April 14, 2011 12:56 PM
To: William Dunlap
Cc: r-help@r-project.org
Subject: Re: [R] Incremental ReadLines
Hi Bill,
Thank you so much for your suggestions. I will try and alter my
code
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Kevin Ummel
Sent: Thursday, April 14, 2011 12:35 PM
To: r-help@r-project.org
Subject: [R] Find number of elements less than some number:
Elegant/fastsolution needed
Take
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of mat
Sent: Tuesday, April 19, 2011 1:59 PM
To: John Kane
Cc: r-help@r-project.org
Subject: Re: [R] Axes Alignment Problem for Multiple Plots
Ok, I can replicate your problem,
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of jim holtman
Sent: Wednesday, April 20, 2011 9:59 AM
To: baboon2010
Cc: r-help@r-project.org
Subject: Re: [R] 'Record' row values every time the binary
value in acollumn
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Nordlund,
Dan (DSHS/RDA)
Sent: Thursday, April 21, 2011 9:19 AM
To: r-help@r-project.org
Subject: Re: [R] Fibonacci
-Original Message-
From:
Does the following do what you want? It should
generate all the (unordered) NPart-partitions of Sum
by mapping the output of combn(Sum+NParts-1,NParts-1).
f - function (Sum, NParts)
{
cm - combn(Sum + NParts - 1, NParts - 1)
cm - rbind(cm, Sum + NParts)
if (NParts 1) {
r
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Brian Diggs
Sent: Monday, April 25, 2011 11:05 AM
To: christoph.jaec...@wi.tum.de
Cc: r-help@r-project.org
Subject:
Use try() or tryCatch() to let your loop continue looking
for more files after download.file() throws an error.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Rolf
A different approach is to use order() to sort
first by row number and then break the ties by
value. It is quick when there are lots of short
rows.
f1 - function (x)
+apply(x, 1, function(row) sort(row, decreasing = TRUE)[2])
f2 - function (x)
+ -apply(-x, 1, function(row)
(x)
[1] 6 10
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of William Dunlap
Sent: Tuesday, April 26, 2011 9:11 AM
To: peter dalgaard; David Winsemius
Cc: r-help
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Jannis
Sent: Wednesday, April 27, 2011 11:16 AM
To: Jonathan Daily
Cc: r-help@r-project.org
Subject: Re: [R] setting options only inside functions
Thanks to all who supplied
From: h.wick...@gmail.com [mailto:h.wick...@gmail.com] On
Behalf Of Hadley Wickham
Sent: Wednesday, April 27, 2011 2:21 PM
To: luke-tier...@uiowa.edu
Cc: William Dunlap; r-help@r-project.org
Subject: Re: [R] setting options only inside functions
Put together a list and we can see what
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius
Sent: Friday, April 29, 2011 10:36 AM
To: Tal Galili
Cc: r-help@r-project.org
Subject: Re: [R] read.csv fails to read a CSV file from google docs
On Apr 29,
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of
luke-tier...@uiowa.edu
Sent: Friday, April 29, 2011 9:35 AM
To: Jonathan Daily
Cc: r-help@r-project.org; Hadley Wickham; Barry Rowlingson
Subject: Re: [R] setting options only
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of andrija djurovic
Sent: Tuesday, May 03, 2011 11:28 AM
To: Woida71
Cc: r-help@r-project.org
Subject: Re: [R] Simple
-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Petr Savicky
Sent: Wednesday, May 04, 2011 12:51 AM
To: r-help@r-project.org
Subject: Re: [R] Simple loop
On Tue, May 03, 2011 at 12:04:47PM -0700, William Dunlap wrote
gsub does this because the string dsff\nfsd
does not contain a backslash - the 5th character
is a newline. The deparsed representation (used
for printing the string) of a newline is \n but
the string itself has not backslash.
You can feed the output of deparse into message (or
cat) so they show
merge() can align the values in the obs columns
so those with the same date can be compared. E.g.,
set up the data with the following copy-and-pastable
code:
A - read.table(header=TRUE, textConnection( year mon day obs
2010 03 1212
2010 03 1822
2010 04 1262
2010 07
Try using normalizePath(foo.pdf) after creating
the file. It should return an absolute path to
an existing file.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Tyler
Look into merge() or, if you want to work at
a lower level, match().
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of David Winsemius
Sent: Sunday, August 28, 2011 11:07
It helps to consider a small self-containd example
where the correct answer is obvious. Does the following
look like your input and desired output?
a - rbind(c(obj1-ptA-x, obj1-ptA-y, obj1-ptA-z),
+c(obj1-ptB-x, obj1-ptB-y, obj1-ptB-z),
+c(obj2-ptA-x, obj2-ptA-y,
If you know much about what the elements of LIST look like
you can speed things up by not making R figure out what
you already know. E.g., if you know that LIST consists of
p numeric vectors, all of the same length, n, then the following
might be faster
matrix(unlist(LIST, use.names=FALSE),
Oops, I mixed up rbind and cbind. If LIST consists
of n numeric vectors, each of length p, try
matrix(unlilst(LIST, use.names=FALSE), nrow=n, byrow=TRUE)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: William Dunlap
Sent: Sunday, August 28, 2011 6
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Bert Gunter
Sent: Monday, August 29, 2011 7:07 AM
To: Jim Lemon
Cc: r-help@r-project.org
Subject: Re: [R] Asking Favor For Remove element with Particular Value In
Vector
apply() should come with a big warning that it was
written for matrices and can cause a lot of surprises
with data.frames. apply(X, ...) converts X to a
matrix and that is the kernel of your problem: if any
columns of X are not numeric then as.matrix(X) is
a character matrix whose columns are
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Heikki Kaskelma
Sent: Monday, August 29, 2011 6:13 PM
To: r-h...@stat.math.ethz.ch
Subject: Re: [R] weird apply() behavior
array chip:
Hi, I had a weird results from using
table(match(x, x)) gives you the numbers but the labels are
a bit more work.
E.g., I'll define another list
x - list(c(1, 2, 4), c(1, 2, 4), 2^(0:4), 3^(1:2), 2^(0:4))
tb - table(m - match(x, x))
m
[1] 1 1 3 4 3
tb
1 3 4
2 2 1
which says that the first element of x is seen
I'll put in a plug for vapply().
# 100,000 numbers in 17576 groups:
y - rep(do.call(paste, c(list(sep=),
expand.grid(LETTERS,letters,letters))), length=1e5)
x - seq_along(y)^2
system.time(val.vapply - vapply(split(x, y), FUN=sum, FUN.VALUE=0))
user system elapsed
0.18
I'll assume that all of an individual's data rows
are contiguous and that an individual always passes through
the groups in order (or, least, the individual
never leaves a group and then reenters it), so we
can find everything we need to know by comparing each
row with the previous row.
You can
There are no backslash characters in the string bla\ble\bli.
\b is used to indicate a backspace character, just
as \n is used to indicate a newline character.
You can get rid of the backslash characters with
gsub(\b,,bla\ble\bli)
[1] blaleli
or change them to b's with
Typo below:
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of William Dunlap
Sent: Wednesday, August 31, 2011 6:47 PM
To: . .; R-help@r-project.org
Subject: Re: [R] Removing special chars in strings?
There are no backslash
-Original Message-
From: . . [mailto:xkzi...@gmail.com]
Sent: Wednesday, August 31, 2011 6:59 PM
To: William Dunlap
Cc: R-help@r-project.org
Subject: Re: [R] Removing special chars in strings?
I got it!
Where did I find the table relating the code and the respective meaning
You could also use match() directly instead
of going through factors. Any of the following
would map your inputs to small integers
match(x, x)-1
[1] 0 1 0 3 0 5 0 7 8 9
match(x, unique(x))-1
[1] 0 1 0 2 0 3 0 4 5 6
match(x, sort(unique(x)))-1
[1] 3 4 3 6 3 2 3 0 5 1
Your
I have used do.call(substitute, ...) to work around the
fact that substitute does not evaluate its first argument:
R z - quote(func(arg))
R do.call(substitute, list(z, list(func=quote(myFunction),
arg=as.name(myArgument
myFunction(myArgument)
S+'s substitute (following S version 4)
You did not show the code you used to populate your
object, but consider the following ways to make as.list(1:5)
via repeated assignments:
system.time( { z0 - list() ; for(i in 1:5)z0[i] - list(i) } )
user system elapsed
13.340.00 13.42
system.time( { z1 - list() ;
- as.vector(table(match(read.genes, read.genes)))
length(duplicates)
[1] 1424
read.genes.uniq - unique(read.genes)
length(read.genes.uniq)
[1] 1469
sum(duplicates)
[1] 9945348
length(read.genes)
[1] 9945348
On Wed, Aug 31, 2011 at 12:42 PM, William Dunlap
wdun...@tibco.commailto:wdun
Spotfire, TIBCO Software
wdunlap tibco.com
From: zhenjiang xu [mailto:zhenjiang...@gmail.com]
Sent: Wednesday, September 07, 2011 8:04 PM
To: William Dunlap
Cc: r-help
Subject: Re: [R] counting the duplicates in an object of list
I tried converting the elements to strings before, but due to the large
Use gzcon() to make a compressed connection and any
function that write to a connection will write compressed
data. E.g.,
con - gzcon(file(tempfile.junk, wb))
x - as.integer(rep(c(-127, 1, 127), c(3,2,1)))
writeBin(x, con, size=1)
close(con)
q(no)
bill:158% zcat tempfile.junk |
In my.ls() you ought to convert the pos argument
to an environment and consistently use that environment
in the calls to eval, get, and ls in the function.
E.g., with the following modification
my.ls1 - function (pos = 1, sorted = FALSE, envir = as.environment(pos))
{
.result -
I thought that the main advantage of subset() over [()
is that you only mention the name of the data.frame once,
in the first argument, not in the second:
x - data.frame(xin=c(1, 8, 16, 1, 8, 16), xout=c(14, 5, 884, 14, 5, 884))
subset(x, xin 7, select = xout) # not x$xin 7
xout
2
Which of these functions is best really depends on
why you are interested in knowing whether a number
is integral or not. Why are people interested in this?
I can think of a few
1) I have a C routine with the prototype
void func(int *n)
and to call it with .C() I need to make sure
I may be wrong, but I thought the original poster
was trying to make a matrix ('metric' sp.) of values,
R, so that
R[i,j] == f(t[i], h[i])
for all i and j where
f - function(t, h)exp(-(t/eta*(1-h))^(beta*(1-h))
outer() would do that:
R - outer(t, h, function(t,
You can also use list syntax, env$name or env[[name]],
with environments to avoid the ugliness of get()
and assign() and to ease your way over to at
least using a dedicated environment (if not a list)
for your related objects instead of filling the
global environment to various collections of
I often use the following function
is.true - function(x) !is.na(x) x
and, less often,
is.false - function(x) !is.na(x) !x
to report if elements of a logical vector are TRUE (not
FALSE or NA) or FALSE (not TRUE or NA), respectively.
Do your complicated logical expression and apply is.true()
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of andrewH
Sent: Thursday, September 15, 2011 2:11 PM
To: r-help@r-project.org
Subject: [R] Returning the name of an object passed directly or from a list
by lapply
Dear folks:
Is th following the sort of thing you are looking for?
f - function (...)
{
unevaluatedArgs - substitute(...())
evaluatedArgs - list(...)
stopifnot(length(unevaluatedArgs) == length(evaluatedArgs))
tags - vapply(unevaluatedArgs, FUN=function(x) deparse(x)[1],
Wrap the call that may abort with try() or tryCatch().
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Rainer
Schuermann
Sent: Friday, September 16, 2011 1:09 PM
To:
To: William Dunlap; R help
Subject: Re: [R] Strplit code
Dear R- Splus experts,
In R, I have frequently used do.call with strsplit. and I have a hard time with
Splus.. any suggestions?
for example, the R code below:
do.call(rbind,strsplit(paste(letters[1:10],c(1:10)), ))
Thanks so much,
Santosh
You could use the na.action function on the fitted
object to see which observations were omitted. E.g.,
let's make a data.frame that we can actually do some
regressions with and try na.action():
d - data.frame(V1=11:15, V2=log(c(1,NA,NA,4,5)), V3=sqrt((-1):3),
V4=sin(1:5))
Warning message:
as
do.call(rbind, x)
would. If you don't know how many rows there
will be then do.call is the way to go.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: MK [mailto:mdkz...@aol.com]
Sent: Tuesday, September 20, 2011 5:13 PM
To: William Dunlap
Cc: Santosh; R
Didn't Columbia switch to a year of daylight
savings time at what would have been midnight
May 3, 1992 (so midnight did not exist that day)?
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org
Use do.call(func, listOfArgs) when you don't
know how many arguments will be passed to func.
E.g.,
x - cbind(round(sin(1:10)), round(cos(1:10)), round(tan(1:10)))
x[do.call(order, split(x, col(x))), , drop=FALSE]
[,1] [,2] [,3]
[1,] -1 -11
[2,] -1 -11
[3,]
If you draw the whiskers out to the extrema of the data
you may wish to omit the outliers, which are encoded
by the out and group components of boxplot's return value:
d - split(Nile, factor(time(Nile)1902, labels=c(pre-dam, post-dam)))
par(mfrow=c(1,2))
b - boxplot(d, main=Default Boxplot)
, September 26, 2011 1:30 PM
To: William Dunlap; r-help; m.marcinmichal
Subject: Re: [R] Triangular matrix upper to down
Nope, I was sloppy and missed that. Thanks
... forwarding to list and OP
Michael
On Mon, Sep 26, 2011 at 4:26 PM, William Dunlap
wdun...@tibco.commailto:wdun...@tibco.com wrote:
My r
regexpr() can be used instead of regMatchPos() in S+ and
R. (It has been in S+ longer than regMatchPos.)
Instead of using regexpr and substring I usually use sub()
or gsub(). E.g., since S+ 8.0 and a long time ago in
R you can do
x - c(Cycle 1 Day 15 Hour 2, Cycle 2 Day 3)
sub(^.*Day
: Wednesday, September 28, 2011 10:19 AM
To: William Dunlap
Subject: Re: [R] using the system command
Hi William,
When I use wait = TRUE I get a lot of intermediate commands from
StatTransfer. For example, if the files it's transferring to already
exist, it shows repeated messages asking if I want
is.element(myvector, seq(1,800,4))
or, if you like typing percent signs,
myvector %in% seq(1,800,4)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Alaios
Sent:
2:07 PM
To: William Dunlap; R-help@r-project.org
Subject: Re: [R] is member
Thanks a lot! This works.
Now I want to do the opposite
let's say that I have one sequence
for example
check in image
http://imageshack.us/photo/my-images/4/unleduso.png/
column A (this is a seq(1,113,4)
and I want when I
Start out with merge():
df - merge(df1, df2, all.x=TRUE) # could add by=location for emphasis
df
location Name Position Country
1 36 cristinaBNA
2 75 francescaA UK
You could make make your 'Match' column from is.na(df$Country) if you
knew
It looks like this code was written for S+ 4.5 (aka '2000')
or before, which was based on S version 3. Try changing
return(name1=value1, name2=value2)
to
return(list(name1=value1, name2=value2))
In S+ from 5.0 onwards return(name=value) or return(name1=value1,
name2=value2) throws away the
+ 4.5, from 1999 or so, to check this out.)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of William Dunlap
Sent: Wednesday, October 05, 2011 9:09 AM
To: Scott Raynaud; r-help
You can use [1] on the output of FUN to ensure that
exactly one value (perhaps NA from numeric(0)[1]) is
returned. E.g.
index - 1
sapply(list(c(1,2,3),c(1,2),c(1)),function(x){x[max(length(x)-index,0)][1]})
[1] 2 1 NA
I'll also put in a plug for vapply, which throws an
error if FUN
I took the original code, changed all return()
calls of the form return(n1=v1,n2=v2) to
return(list(n1=v1,n2=v2)) and then sshc(10,100)
chugged away and produced some plots and returned
something with no errors. It took a couple of minutes.
I also changed T-TRUE and F-FALSE, as that makes
the
I corrected your code a bit and put it into a function, f0, to
make testing easier. I also made a small dataset to make
testing easier. Then I made a new function f1 which does
what f0 does in a vectorized manner:
x - array(rnorm(50 * 50 * 50 * 91, 0, 2), dim=c(50, 50, 50, 91))
xsmall -
Creating expressions and functions dynamically can be
tricky. Usually I use functions like call(), substitute(),
and formals(); very occasionally I use parse(text=).
Here is one way to make a family of functions that differ
only in the default value their their argument:
funsA - lapply(1:3,
Avoid parsing strings to make expressions. It is easy
to do, but hard to do safely and readably.
In your case you could make a short loop out of it
result - x[,,,1]
for(i in seq_len(dim(x)[4])[-1]) {
result - result + x[,,,i]
}
result - result / dim(x)[4]
Bill Dunlap
You can work around the problem by making each
call to bquote() in a different environment, each
containing its own value of 'i'. E.g.,
par(mfrow=c(2,1))
for(i in 1:2) {
x - 1:100
rmse - sin(x/5) # fake data
plot(x,rmse)
str1 - local({ i - i ; bquote( paste(local RMSE(,theta,),
d - data.frame(A=c(a,b), B=2:3, C=c(1,3), D=c(4,5))
lengths - 1 + d$D - d$C
cbind(d[rep(seq_along(lengths), lengths),c(A,B)],
E=unlist(lapply(seq_along(lengths), function(i)seq(from=d$C[i], to=d$D[i]
A B E
1 a 2 1
1.1 a 2 2
1.2 a 2 3
1.3 a 2 4
2 b 3 3
2.1 b 3 4
2.2 b 3 5
Bill
To run a cmd.exe built-in function use 'cmd /c function arg1 ...', as in
status - system(cmd /C echo foo)
foo
status # 0 means all went well
[1] 0
as opposed to
status - system(echo foo)
status
[1] 127
You get a little more information about that 127 return value
by
The following avoids the overhead of data.frame methods
(and assumes the data.frame doesn't include matrices
or other data.frames) and relies on split(vector,factor)
quickly splitting a vector into a list of vectors.
For a 10^6 row by 10 column data.frame split in 10^5
groups this took 14.1
First define a function that returns TRUE if a column
should be dropped. E.g.,
has3Zeros.1 - function(x)
{
x - x[!is.na(x)] == 0 # drop NA's, convert 0's to TRUE, others to FALSE
if (length(x) 3) {
FALSE # you may want to further test short vectors
} else {
Is this the result you are after, where the event number
(within a group) are sorted according to the event/prev_event
pairs (prev_event in a row matches event of the previous row)?
ave(d, d$group, FUN=function(z) z[ match(tsort(z$prev_event, z$event)[-1],
z$event), ])
event prev_event
Be careful with the idiom
x[, -which(columnIsBad)]
If no columns are bad this leads to
x[, -integer(0)]
which is a data.rame with no columns,
exactly the opposite of what you want.
x[, !columnIsBad]
doesn't have that problem. However, if
you can't tell if a column is bad or not
(i.e.,
Functions with prototypes of the form
SEXP myfunc(SEXP, SEXP, ..., SEXP)
must be called via .Call(), not .C().
Also, you declared myfunction as returning
SEXP but returned nothing. Try ending the
function with
return R_NilValue;
You should change the default compiler flags
to report all
Have you tried setting
options(scipen=500) # big number of digits
? E.g.,
df - data.frame(x=pi*10^seq(-30,30,by=10), d=seq(-30,30,by=10),
s=state.name[31:37])
getOption(scipen)
[1] 0
write.csv(df, stdout())
,x,d,s
1,3.14159265358979e-30,-30,New Mexico
2,3.14159265358979e-20,-20,New York
Set the levels of the factor a$V1 to the order
in which you want them to be sorted. E.g.,
a - data.frame(V1=letters[rep(4:1,2)], V2=1001:1008)
a[do.call(order,a[c('V1','V2')]),]
V1 V2
4 a 1004
8 a 1008
3 b 1003
7 b 1007
2 c 1002
6 c 1006
1 d 1001
5 d 1005
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of David Winsemius
Sent: Sunday, October 16, 2011 1:59 PM
To: Jeff Newmiller
Cc: r-help@r-project.org; syrvn
Subject: Re: [R] Which function to use: grep, replace, substr etc.?
Often the summary() methods for the modelling functions
contain the kinds of details you are looking for. Sometimes
there are special extractor functions but often not.
You have to study the help file for summary.objectClass
and the vignettes for the package, and perhaps do a little
Try vectorizing it a bit by looping over the columns.
E.g.,
f1 - function (df)
{
# loop (backwards) over all columns in df whose
# names start with D to find the earliest one
# that is bigger than column thold. I tested with
# df being a data.frame but a matrix should
ans
}
On a 10,000 row data.frame f0 took 47.950 seconds and f0.a took
0.140 seconds. (f1, below, took 0.012 seconds.)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From: William Dunlap
Sent: Monday, October 17, 2011 11:00 AM
To: Nathan Piekielek
Cc
Dunlap
Subject: RE: [R] how to use 'which' inside of 'apply'?
Wow, that dramatically improves performance. I never realized data frames
were that inefficient.
Thanks for your help, incredible difference.
Nathan
-Original Message-
From: William Dunlap [mailto:wdun...@tibco.com
I don't think you said how you packaged this stuff up
and that is the critical part of your question.
Here is an example that does what I think you want to do.
It uses save() and load() for the serialization and I
serialize an environent full of data and functions.
In one session of R make some
I made a small vector consisting of ones and zeros.
Something like this x - c(0,1,0,1,0,0,1,0), and all I need is to count
how many times 0 becomes 1.
Since x consists solely of 0's and 1's this is same as
sum(diff(x)==1) # 3 in your example
Bill Dunlap
Spotfire, TIBCO Software
wdunlap
sprintf's %numbers format descriptor ignores initial 0's in number,
in C's sprintf and in R's. Here are 2 ways to do it:
z - c(5, 45, 345, 2345, 12345)
sprintf(%05d, as.integer(z))
[1] 5 00045 00345 02345 12345
gsub( , 0, sprintf(%5s, z))
[1] 5 00045 00345 02345 12345
Bill
## sapply is a good solution (the only one I could think of too), but
not always worth it:
Also look at vapply(). It is like sapply() but you have to tell
it what type and size of output you expect FUN to return. E.g.,
change
sapply(1:n, FUN=function(i) cor(x[,i],y[,i]))
to
vapply(1:n,
401 - 500 of 2201 matches
Mail list logo