[R] [R-pkgs] onetime 0.1.0: Run Code Only Once

2022-12-09 Thread David Hugh-Jones
Dear all,

Onetime 0.1.0, a utility package of interest to package developers, is now
on CRAN:

https://cran.r-project.org/package=onetime

Onetime uses lockfiles to perform an action only once (ever, or with an
expiry date) on a given computer. For example, it can send a message or
warning:

for (i in 1:10) {
  onetime_message("This will be shown only once", id = "my-message")
}

It also lets you send a message and confirm the user doesn't need to see it
again:

onetime_message_confirm("User can opt not to show this message again",
 id = "my-confirm-message")

Onetime checks that you have permission to store files on the user's
computer, and allows package authors to ask for this permission using
check_ok_to_store().

Package authors can use onetime to print one-off informative messages for
new users, or to do other one-off actions.

Documentation is available at https://hughjonesd.github.io/onetime/.

Cheers,
David

[[alternative HTML version deleted]]

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] [R-pkgs] Package huxtable 0.2.1 on CRAN

2017-04-26 Thread David Hugh-Jones
Hi all,

I'm happy to announce that the huxtable package version 0.2.1 is on CRAN.

huxtable is an R package to create LaTeX and HTML tables, with a friendly,
modern interface. Features of 0.2.1 include:

- Export to LaTeX, HTML, Word and Markdown
- Easy integration with knitr and rmarkdown documents
- Multirow and multicolumn cells
- Fine-grained control over cell background, spacing, alignment, size and
borders
- Control over text font, style, size, colour, alignment, number format and
rotation
- Table manipulation using standard R subsetting, or dplyr functions like
filter and select
- Easy conditional formatting based on table contents
- Quick table themes
- Automatic creation of regression output tables with the huxreg function

The CRAN link is https://cran.r-project.org/package=huxtable , and there's
a website with documentation at http://hughjonesd.github.io/huxtable

Comments, suggestions and bug reports are welcome and can be filed at
http://www.github.com/hughjonesd/huxtable/issues

Cheers,
David

[[alternative HTML version deleted]]

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] [R-pkgs] anim.plots - simple animated plots for R

2014-11-10 Thread David Hugh-Jones
Announcing the first release of anim.plots, a package for simple animated
plots in R, using Yihui Xie's animation package.

http://cran.r-project.org/web/packages/anim.plots/index.html

Functions are very similar to basic R graphics. Currently the package
includes animated versions of plot, barplot, persp, contour,
filled.contour, hist, curve, points, lines, text, symbols, segments, and
arrows.

There's a brief introduction here:
http://davidhughjones.blogspot.co.uk/2014/10/easy-animations-in-r.html

And an HTML presentation:
http://hughjonesd.github.io/anim-plots-presentation.html

Code is hosted on GitHub:
https://github.com/hughjonesd/anim.plots

Cheers,
David

[[alternative HTML version deleted]]

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages

__
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] importing data

2013-01-26 Thread David Hugh-Jones
Hi Ray,
Comments below:

On 26 January 2013 09:03, Ray Cheung  wrote:
[snip]
> ###FUNCTION TO READ FILES
> little_helpful <- function(n) {
> file_name <- paste0("C:/.../data", n, ".dat")
> read.table(file_name)
> }
>
> ###RETURN AN OBJECT WHICH CHECKS FOR THE EXISTENCE OF FILES
> check  <- function(n) {
> a <- ifelse(file.exists(paste0("C:/.../data", n, ".dat")), 1, 0)
> a
> }

Too complex. Why not just use file.exists directly?

> ###Combining the functions
> IMPORT <- function(n) {
>L <- check(1:n)
>for (i in 1:n) {
>   if (L[i] == 1)
>   list_of_datasets <- lapply(i, little_helpful) else list_of_datasets
> <- 0
>   }
>list_of_datasets
>}
>

Too complex here too. I suggest something like:

M <- list()
for (i in 1:n) {
> file_name <- paste0("C:/.../data", n, ".dat")
  if (file.exists(file_name)) M[i] <- read.table(file_name)
}

R gurus don't like for() loops, but they are easy for humans to understand.
If this doesn't work, post the error message.


> Thanks for all comments.
>
> Best Regards,
> Ray
>
> On Fri, Jan 25, 2013 at 5:48 PM, Ivan Calandra > wrote:
>
>> Hi,
>>
>> Not sure this is what you need, but what about list.files()?
>> It can get you all the files from a given folder, and you could then work
>> this list with regular expressions for example.
>>
>> HTH,
>> Ivan
>>
>> --
>> Ivan CALANDRA
>> Université de Bourgogne
>> UMR CNRS/uB 6282 Biogéosciences
>> 6 Boulevard Gabriel
>> 21000 Dijon, FRANCE
>> +33(0)3.80.39.63.06
>> ivan.calan...@u-bourgogne.fr
>> http://biogeosciences.u-**bourgogne.fr/calandra
>>
>> Le 25/01/13 10:00, R. Michael Weylandt a écrit :
>>
>>> On Fri, Jan 25, 2013 at 6:11 AM, Ray Cheung  wrote:
>>>
 Dear Michael,

 Thanks for your codes. However, lapply does not work in my case since
 I've
 some files missing in the data (say, the file data101.dat). Do you have
 any
 suggestions on this?? Thank you very much.

  You could simply add a test using file.exists() but I'm not sure what
>>> you want to do with the M matrix then -- omit the slice (so the others
>>> are all shifted down one) or fill it entirely with NA's.
>>>
>>> Michael
>>>
>>> __**
>>> R-help@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html 
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>> __**
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html 
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-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] Loop question?

2013-01-26 Thread David Hugh-Jones
Here's a toy example which you can apply the logic of:

dfr <- expand.grid(1:3,1:2)
results <- apply(dfr, 1, sum)
dfr[results==4,]





On 25 January 2013 22:19, Andras Farkas  wrote:
>
> Dear All
>
> I have the following data (somewhat simplyfied):
>
> TINF <-1
> a <-c(500,750,1000,1250,1500,1750,2000)
> b <-c(8,12,18,24,36,48,60,72,96)
>
> following function:
>
> infcprodessa <-function (D, tin, tau, ts)
>   (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048 * tau 
> * exp(-0.048 * (ts - tin))
>
> z <-sapply(1:1, function(n) infcprodessa(1000,TINF,12,12-TINF))
>
> is there a way to select the combination of respective a and b values that 
> would result in a calculated z that is between 15 and 20? In this case the a 
> would be 1000 and the b would be 12 (other combinations are also possible), 
> but how could I automatically find them? perhaps a loop?
>
> Apreciate the help,
>
> Sincerely,
>
> Andras
> [[alternative HTML version deleted]]
>
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-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] fixed effects Tobit, Honore style?

2011-07-15 Thread David Hugh-Jones
A cleaner and slightly more tested version is at

http://davidhughjones.blogspot.com/2011/07/honore-style-fixed-effects-estimators.html

David Hugh-Jones
Research Associate
CAGE, Department of Economics
University of Warwick
http://davidhughjones.googlepages.com



On 13 July 2011 15:33, David Hugh-Jones  wrote:

> True! Here's my attempt -- use at your own risk.
>
> honore <- function (b, dataset, x1, x2) {
> dxb <- (x2 - x1) %*% b
> y1 <- # insert your y variable here
> y2 <- # insert your y variable here
> sum(
> (pmax(y1, dxb) - pmax(y2, dxb) - dxb)^2 +
> 2*(y1 < dxb)*(dxb-y1)*y2 +
> 2*(y2 < -dxb)* (-dxb-y2)*y1
> )
> }
>
> fetobit <- function (dataset, form) {
> x2 <- model.matrix(form, dataset[,T=2])
> x1 <- model.matrix(form, dataset[,T=1])
># could maybe set initial values to something different
> res <- optim(rep(0, ncol(x1)), fn=honore, x1=x1, x2=x2,
> dataset=dataset, method="BFGS", control=list(maxit=1000))
> if (res$convergence != 0) warning("Didn't converge")
> res$par
> }
>
> For standard errors, bootstrap.
>
>
> David Hugh-Jones
> Research Associate
> CAGE, Department of Economics
> University of Warwick
> http://davidhughjones.googlepages.com
>
>
>
> On 12 July 2011 21:38, Daniel Malter  wrote:
>
>> Not that I know of, but the paper says that they are easy to compute. If
>> you
>> did, you could contribute the code.
>>
>> Best,
>> Daniel
>>
>>
>> David Hugh-Jones-3 wrote:
>> >
>> > Hi all,
>> >
>> > Is there any code to run fixed effects Tobit models in the style of
>> Honore
>> > (1992) in R?
>> > (The original Honore article is here:
>> >
>> http://www.jstor.org/sici?sici=0012-9682%28199205%2960%3A3%3C533%3ATLALSE%3E2.0.CO%3B2-2
>> )
>> >
>> > Cheers
>> > David
>> >
>> >   [[alternative HTML version deleted]]
>> >
>> > __
>> > R-help@r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/fixed-effects-Tobit-Honore-style-tp3662246p3663464.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] fixed effects Tobit, Honore style?

2011-07-13 Thread David Hugh-Jones
True! Here's my attempt -- use at your own risk.

honore <- function (b, dataset, x1, x2) {
dxb <- (x2 - x1) %*% b
y1 <- # insert your y variable here
y2 <- # insert your y variable here
sum(
(pmax(y1, dxb) - pmax(y2, dxb) - dxb)^2 +
2*(y1 < dxb)*(dxb-y1)*y2 +
2*(y2 < -dxb)* (-dxb-y2)*y1
)
}

fetobit <- function (dataset, form) {
x2 <- model.matrix(form, dataset[,T=2])
x1 <- model.matrix(form, dataset[,T=1])
   # could maybe set initial values to something different
res <- optim(rep(0, ncol(x1)), fn=honore, x1=x1, x2=x2,
dataset=dataset, method="BFGS", control=list(maxit=1000))
if (res$convergence != 0) warning("Didn't converge")
res$par
}

For standard errors, bootstrap.


David Hugh-Jones
Research Associate
CAGE, Department of Economics
University of Warwick
http://davidhughjones.googlepages.com



On 12 July 2011 21:38, Daniel Malter  wrote:

> Not that I know of, but the paper says that they are easy to compute. If
> you
> did, you could contribute the code.
>
> Best,
> Daniel
>
>
> David Hugh-Jones-3 wrote:
> >
> > Hi all,
> >
> > Is there any code to run fixed effects Tobit models in the style of
> Honore
> > (1992) in R?
> > (The original Honore article is here:
> >
> http://www.jstor.org/sici?sici=0012-9682%28199205%2960%3A3%3C533%3ATLALSE%3E2.0.CO%3B2-2
> )
> >
> > Cheers
> > David
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/fixed-effects-Tobit-Honore-style-tp3662246p3663464.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help in error removal

2011-07-12 Thread David Hugh-Jones
On 12 July 2011 12:27, Mitra, Sumona  wrote:

> Dear all,
>
> I am new to programming in R.
>


You sure are ;-)

 I deal with microarray data,which is a data frame object type. I need to
> carry out a few statistical procedures on this, one of them being the
> pearson corelation. I need to do this between each row which is a gene. So
> the desired result is a square matrix with the pearson corelation value
> between each row. So the first column would be (1,1)=0,(1,2),(1,3) and so
> on.
>
> I uploaded the data frame as "a":-
>
> a <- read.csv("a.csv", header= TRUE, row.names=1)
>
> and then I started the script:-
>
> pearson <- function(a){
>  r <- matrix[x,y]
>

I bet the problem you are getting is here. You want r to be a x by y matrix.
To do this, try r<- matrix(nrow=x, ncol=y). But you haven't defined the x
and y, unless we are missing that part of your code.

As I understand it, you want the correlation matrix between all the rows of
your matrix. If so, then look at the help file for cor. (ie type "?cor".)
You will find that it automatically prints the correlatoins between all
columns of a matrix. So, once your data is correctly read in, you should be
able to just do:

cor(t(a))






>  for(x in as.vector(a[,1], mode="double")){
>  x++{
>  for(y in as.vector(a[2,], mode="double")){
>  y <- x+1
>  x++
>

This code looks like a horrible mess. It's almost never right to loop
through your vectors. In addition, there is no such thing as "++", as
somebody mentioned.



>  {
>  r <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"),
> as.vector(as.matrix(a)[y,], mode="double"))$p.value)
>  }
>  }
>  }
>  r[x,y]==r[y,x]
>  }
>  return(r)
>  }
>
> However whenever I run it,I get the error:-
>
> > pearson(a)
> Error in matrix[x, y] : object of type 'closure' is not subsettable
>
> Please help!
>
> Best Regards
> Sumona Mitra
>
> __
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] fixed effects Tobit, Honore style?

2011-07-12 Thread David Hugh-Jones
Hi all,

Is there any code to run fixed effects Tobit models in the style of Honore
(1992) in R?
(The original Honore article is here:
http://www.jstor.org/sici?sici=0012-9682%28199205%2960%3A3%3C533%3ATLALSE%3E2.0.CO%3B2-2)

Cheers
David

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] table of Design regressions?

2011-05-26 Thread David Hugh-Jones
Hi all

Just a quick question which I can't find an answer for in the usual places:
I would like to create a table of regression output, with one or more
regressions in the columns, a la xtable. But I am using models from the
Design package. Is there anything out there that will play nicely with that?
xtable doesn't seem to do the trick.

Cheers,
David Hugh-Jones
Research Associate
CAGE, Department of Economics
University of Warwick
http://davidhughjones.googlepages.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] clogit comparison between Stata and R

2010-02-26 Thread David Hugh-Jones
Hi Thomas

Sorry to bug you again after 6 months! I just wondered if there is a
simple way to find the N after dropping groups with invariant outcomes
- I am reading coxph.object and the structure of the returned object,
but nothing jumps out at me.

With best wishes,
David Hugh-Jones


On 9 July 2009 14:37, Thomas Lumley  wrote:
> On Wed, 8 Jul 2009, David Hugh-Jones wrote:
>
>> Hello all
>>
>> I'm moving back and forth between stata and R at the moment - of course,
>> using R whenever possible :-)
>>
>> I'm running conditional logits on some panel data and I get slightly
>> different results and different N in the two programs.
>
> That's probably because you are using method="approximate" in R.
>
>>
>> I understand why Stata is dropping the groups with all outcomes the
>> same...
>> this is inevitable in a conditional logit, right?
>
> Yes.
>
>> Is R doing the same?
>
> Yes.
>
>        -thomas
>

__
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] model matrix with a spline

2009-12-18 Thread David Hugh-Jones
Hi all

I want to get the design matrix for a model, evaluated at a single value.
For example, if I pass in a data frame with a=2, b=2, y=3, and my
model is y ~ a+b+a:b, then I would like to get
the values 3, 2, 2, 4 out. I can do this with:

tmp <- model.matrix(fit, data=mydata)

or

tmp <- predict(fit, newdata=mydata, type="terms")

However, if the fit had a smoothing spline component, this fails. It
seems like the prediction function is trying to reevaluate the basis
for the spline, and as there is only one row in the new data, it can't
do that.

Is there a way I can get the value of the already-created spline? And
is there a simple way to do this programmatically so I don't need to
check each term of the formula individually?

Cheers,

David Hugh-Jones
Post-doctoral Researcher
Max Planck Institute of Economics, Jena
http://davidhughjones.googlepages.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to read point shp file to R?

2009-07-08 Thread David Hugh-Jones
You might also find the R wiki useful:
http://wiki.r-project.org/rwiki/doku.php?id=tips:spatial-data
http://wiki.r-project.org/rwiki/doku.php?id=tips:stats-spatial

David Hugh-Jones
Post-doctoral Researcher
Max Planck Institute of Economics, Jena
http://davidhughjones.googlepages.com


2009/7/7 Sunny 

> I am new with R and want do some analysis with a point vector data file.
> Any
> help is appreciate.  Sunny
>
>[[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] clogit comparison between Stata and R

2009-07-08 Thread David Hugh-Jones
Hello all

I'm moving back and forth between stata and R at the moment - of course,
using R whenever possible :-)

I'm running conditional logits on some panel data and I get slightly
different results and different N in the two programs.

In R I run
clogit(trans.dem ~ I(avg.gle_rgdp.500/gle_rgdp) + log(gle_rgdp) +
timesince.dem + I(timesince.dem^2) + timesince.dict + I(timesince.dict^2) +
p_polity2 + I(p_polity2^2)  + strata(ccodecow) + cluster(ccodecow),
method="approximate", data=univ)

and I get an n of 3747.

In Stata, I run
clogit trans_dem avg_gle_rgdp_ratio loggle_rgdp timesince_dem
timesince_demsq timesince_dict timesince_dictsq p_polity2 pol2sq,
group(ccodecow) vce(cluster ccodecow)

which I hope is the same model. I get a message "29 groups (935 obs) dropped
because of all positive or all negative outcomes", and an n of 2812. Also,
the coefficients are slightly different.

I understand why Stata is dropping the groups with all outcomes the same...
this is inevitable in a conditional logit, right? Is R doing the same? And
what might be the cause of the difference in coefficients?

Cheers
David Hugh-Jones
Post-doctoral Researcher
Max Planck Institute of Economics, Jena
http://davidhughjones.googlepages.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] simple question

2009-07-01 Thread David Hugh-Jones
Hello all

I have a fit resulting from a call to glm. Now, I would like to extract the
model frame MF, and add some variables
from the original data frame DF. To do this, I need to know which rows in DF
correspond to rows in MF (since some were dropped by na.omit). How can I do
this? It's probably simple but the information is hard to find.


David Hugh-Jones
Post-doctoral Researcher
Max Planck Institute of Economics, Jena
http://davidhughjones.googlepages.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] "by" question

2009-06-24 Thread David Hugh-Jones
That seems to work. I should add that to make "ave" work like "by" one can
do:

mydata$newvar <- ave(1:nrow(mydata), mydata$some_factor, FUN= function (x) {
  x <- ds[x,]
# ... etc...
})

Thanks!
David

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



[R] "by" question

2009-06-24 Thread David Hugh-Jones
Hello all

I have a big data frame and I regularly want to break it down into subsets,
calculate some new data, and add it back to the data frame.

At the moment my technique seems a bit ugly and embarrassing. Something
like:

result <- by(mydata, mydata$some_factor, function (x) {
  # do something to create a vector v with length(v) == nrow(x)
 return(v)
})
# now result has a big list, argh... how do I put it neatly back into the
mydata data frame?
for (i in unique(mydata$some_factor) {
mydata$newvar[mydata$somefactor ==i] <- result[[i]]
}

What should I be doing instead of this?

David Hugh-Jones
Post-doctoral Researcher
Max Planck Institute of Economics, Jena
http://davidhughjones.googlepages.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.