Re: [R] ifelse for creating discriminating variable based on two conditions

2016-10-14 Thread PIKAL Petr
Hi

Another option is to use ave

1*(data2$molecule>ave(data2$molecule, data2$fruit, FUN=function(x) 2*sd(x)))

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jim Lemon
> Sent: Friday, October 14, 2016 10:51 AM
> To: Andreas Nord <andreas.n...@biol.lu.se>
> Cc: r-help@r-project.org
> Subject: Re: [R] ifelse for creating discriminating variable based on two
> conditions
>
> Hi Andreas,
> Try this:
>
> fruit_2sds<-by(data2$molecule,data2$fruit,sd)*2
> data2$newcol<-ifelse(data2$molecule>fruit_2sds[data2$fruit],1,0)
>
> or even just:
>
> data$newcol<-as.numeric(data2$molecule>fruit_2sds[data2$fruit])
>
> Jim
>
>
> On Fri, Oct 14, 2016 at 5:17 PM, Andreas Nord <andreas.n...@biol.lu.se>
> wrote:
> >
> > Dear list,
> >
> > Apologies for a likely naive question.
> >
> >
> > I am trying to create a discriminating dummy variable using 'ifelse' based 
> > on
> conditions in two variables.
> >
> >
> > Specifically, I want to assign levels in a new factor as '0' or '1' based 
> > on a
> user-defined cut off. I.e. something similar to:
> >
> >   >data1<-data.frame(molecule=runif(30,min=0,max=1e3))
> >
> >>data1$newcol<-ifelse(data1$molecule>2*sd(data1$molecule),1,0)
> >
> >
> > Which is all straightforward.
> >
> >
> > But how do I go on to assign values in variable 'molecule'based on the same
> cut off, but separately for each level of a second variable, in this case the
> factor 'fruit' with three levels. That is, how do I derive fruit-specific 
> cut-offs
> using a data frame with the general structure of that below?
> >
> >>data2<-
> data.frame(molecule=runif(30,min=0,max=1e3),fruit=factor(rep(c(
> >>'apple','pear','orange'),10)))
> >
> >
> > Many thanks in advance!
> >
> >
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > 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-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.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and witho

Re: [R] ifelse for creating discriminating variable based on two conditions

2016-10-14 Thread Jim Lemon
Hi Andreas,
Try this:

fruit_2sds<-by(data2$molecule,data2$fruit,sd)*2
data2$newcol<-ifelse(data2$molecule>fruit_2sds[data2$fruit],1,0)

or even just:

data$newcol<-as.numeric(data2$molecule>fruit_2sds[data2$fruit])

Jim


On Fri, Oct 14, 2016 at 5:17 PM, Andreas Nord  wrote:
>
> Dear list,
>
> Apologies for a likely naive question.
>
>
> I am trying to create a discriminating dummy variable using 'ifelse' based on 
> conditions in two variables.
>
>
> Specifically, I want to assign levels in a new factor as '0' or '1' based on 
> a user-defined cut off. I.e. something similar to:
>
>   >data1<-data.frame(molecule=runif(30,min=0,max=1e3))
>
>>data1$newcol<-ifelse(data1$molecule>2*sd(data1$molecule),1,0)
>
>
> Which is all straightforward.
>
>
> But how do I go on to assign values in variable 'molecule'based on the same 
> cut off, but separately for each level of a second variable, in this case the 
> factor 'fruit' with three levels. That is, how do I derive fruit-specific 
> cut-offs using a data frame with the general structure of that below?
>
>>data2<-data.frame(molecule=runif(30,min=0,max=1e3),fruit=factor(rep(c('apple','pear','orange'),10)))
>
>
> Many thanks in advance!
>
>
>
> [[alternative HTML version deleted]]
>
> __
> 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-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] ifelse for creating discriminating variable based on two conditions

2016-10-14 Thread Andreas Nord

Dear list,

Apologies for a likely naive question.


I am trying to create a discriminating dummy variable using 'ifelse' based on 
conditions in two variables.


Specifically, I want to assign levels in a new factor as '0' or '1' based on a 
user-defined cut off. I.e. something similar to:

  >data1<-data.frame(molecule=runif(30,min=0,max=1e3))

>data1$newcol<-ifelse(data1$molecule>2*sd(data1$molecule),1,0)


Which is all straightforward.


But how do I go on to assign values in variable 'molecule'based on the same cut 
off, but separately for each level of a second variable, in this case the 
factor 'fruit' with three levels. That is, how do I derive fruit-specific 
cut-offs using a data frame with the general structure of that below?

>data2<-data.frame(molecule=runif(30,min=0,max=1e3),fruit=factor(rep(c('apple','pear','orange'),10)))


Many thanks in advance!



[[alternative HTML version deleted]]

__
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.