Re: [R] Ifelse statements and combining columns

2017-07-25 Thread PIKAL Petr
Hi

I may be completely wrong, but if your data was factor you could easily change 
its levels without any ifelse stuff.

Some sample data
> dat<-factor(paste("cond", sample(1:7, 20, replace=T), sep=""))
> dat
 [1] cond2 cond1 cond7 cond5 cond7 cond7 cond3 cond4 cond2 cond4 cond2 cond2
[13] cond6 cond2 cond3 cond6 cond2 cond4 cond6 cond4
Levels: cond1 cond2 cond3 cond4 cond5 cond6 cond7

Changing levels
> levels(dat)<- c(rep("uniform", 4), rep("BiasedLow",2), rep("BisaedHigh", 2))
> dat
 [1] uniformuniformBisaedHigh BiasedLow  BisaedHigh BisaedHigh
 [7] uniformuniformuniformuniformuniformuniform
[13] BiasedLow  uniformuniformBiasedLow  uniformuniform
[19] BiasedLow  uniform
Levels: uniform BiasedLow BisaedHigh

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Kirsten
> Morehouse
> Sent: Monday, July 24, 2017 2:24 PM
> To: R-help 
> Subject: [R] Ifelse statements and combining columns
>
> Hi everyone,
>
> I'm having some trouble with my ifelse statements.
>
> I'm trying to put 12 conditions within 3 groups. Here is the code I have so
> far:
>
> dat$cond <- ifelse(test = dat$cond == "cond1" | dat$cond == "cond2"  |
> dat$cond == "cond3" dat$cond == "cond4"
>yes = "Uniform"
>no = ifelse(test = dat$cond == "cond5" | dat$cond ==
> "cond6") | dat$cond == "cond7" dat$cond == "cond8"
>yes = "Biased Low"
>no = "Biased High" )
>
>
> I keep getting an error statement about an invalid ). I've tried several
> permutations to fix, but without luck.
>
> Also, can anyone help me bind columns together? I've tried:
>
>  cbind[, c(15:25)] but get the error:  object of type 'closure' is not 
> subsettable
>
> Thank you in advance!
>
> Kirsten
>
>   [[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.


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 without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is conclude

Re: [R] Ifelse statements and combining columns

2017-07-24 Thread Koustav Pal
That ifelse statement is a mess, it is missing brackets, comma separators
between arguments and & or | between conditions. The bracket error points
towards the invalid bracket you had in the second ifelse since it expects
and yes and a no argument alongside conditions.

ifelse(test = (dat$cond == "cond1" | dat$cond == "cond2"  | dat$cond ==
"cond3" | dat$cond == "cond4"),yes = "Uniform" , no =
ifelse(test = (dat$cond == "cond5" | dat$cond =="cond6" | dat$cond ==
"cond7" | dat$cond == "cond8"), yes = "Biased Low" ,no = "Biased High"))

This should be what you want.


cbind is a function, therefor it is not subsettable.

if you are passing 2 arguments to cbind then cbind takes the form
cbind(c(1:n),c(1:n)) and constructs a matrix of dimension 2 x n. For
unequal vectors the shorter vector will be recycled.


On 24 July 2017 at 14:23, Kirsten Morehouse  wrote:

> Hi everyone,
>
> I'm having some trouble with my ifelse statements.
>
> I'm trying to put 12 conditions within 3 groups. Here is the code I have so
> far:
>
> dat$cond <- ifelse(test = dat$cond == "cond1" | dat$cond == "cond2"  |
> dat$cond == "cond3" dat$cond == "cond4"
>yes = "Uniform"
>no = ifelse(test = dat$cond == "cond5" | dat$cond ==
> "cond6") | dat$cond == "cond7" dat$cond == "cond8"
>yes = "Biased Low"
>no = "Biased High" )
>
>
> I keep getting an error statement about an invalid ). I've tried several
> permutations to fix, but without luck.
>
> Also, can anyone help me bind columns together? I've tried:
>
>  cbind[, c(15:25)] but get the error:  object of type 'closure' is not
> subsettable
>
> Thank you in advance!
>
> Kirsten
>
> [[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.
>

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


Re: [R] Ifelse statements and combining columns

2017-07-24 Thread Duncan Murdoch

On 24/07/2017 8:57 AM, Jeff Newmiller wrote:

Not a reproducible example, so a bit of guessing here, but

a) don't try to assign results to variables inside the ifelse. That is,  remove all the 
single-equals signs and "test" variables. If you really need to conditionally assign 
variables then use "if"... but chances are good you don't need that.


Those weren't assignments.  The arguments to ifelse() are named "test", 
"yes", and "no".


You're right though that the expressions were messed up.  Commas are 
missing between arguments, and the parentheses don't appear to be in the 
right place.


One suggestion for Kirsten:  instead of

dat$cond == "cond1" | dat$cond == "cond2"  |
dat$cond == "cond3" | dat$cond == "cond4"

it is shorter and a bit clearer to write

dat$cond %in% c("cond1", "cond2", "cond3", "cond4")

or maybe even

dat$cond %in% paste0("cond", 1:4)

Duncan Murdoch


b) "closure" is effectively another word for"function"... functions like cbind 
are called with argument lists delimited by parentheses, not brackets, and having a missing 
argument to the cbind function will be of no use.



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


Re: [R] Ifelse statements and combining columns

2017-07-24 Thread Jeff Newmiller
Not a reproducible example, so a bit of guessing here, but

a) don't try to assign results to variables inside the ifelse. That is,  remove 
all the single-equals signs and "test" variables. If you really need to 
conditionally assign variables then use "if"... but chances are good you don't 
need that. 

b) "closure" is effectively another word for"function"... functions like cbind 
are called with argument lists delimited by parentheses, not brackets, and 
having a missing argument to the cbind function will be of no use. 
-- 
Sent from my phone. Please excuse my brevity.

On July 24, 2017 5:23:57 AM PDT, Kirsten Morehouse  
wrote:
>Hi everyone,
>
>I'm having some trouble with my ifelse statements.
>
>I'm trying to put 12 conditions within 3 groups. Here is the code I
>have so
>far:
>
>dat$cond <- ifelse(test = dat$cond == "cond1" | dat$cond == "cond2"  |
>dat$cond == "cond3" dat$cond == "cond4"
>   yes = "Uniform"
>   no = ifelse(test = dat$cond == "cond5" | dat$cond ==
>"cond6") | dat$cond == "cond7" dat$cond == "cond8"
>   yes = "Biased Low"
>   no = "Biased High" )
>
>
>I keep getting an error statement about an invalid ). I've tried
>several
>permutations to fix, but without luck.
>
>Also, can anyone help me bind columns together? I've tried:
>
> cbind[, c(15:25)] but get the error:  object of type 'closure' is not
>subsettable
>
>Thank you in advance!
>
>Kirsten
>
>   [[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.


Re: [R] Ifelse statements and combining columns

2017-07-24 Thread Rui Barradas

Hello,

Your ifelse statement is a mess. I cannot make sense of it. Let me try 
to explain where I've lost it.


dat$cond <- ifelse(test = dat$cond == "cond1"
This is already very bad, do you mean dat$cond == "cond1" ?
Maybe you mean that condition OR the others below, but then there's one 
'|' missing, just before the last condition.


| dat$cond == "cond2"  |
   dat$cond == "cond3" dat$cond == "cond4"

After this your code should have a comma. It's missing.
Maybe you can tell us in plain english the conditions your ifelse should 
process.


Hope this helps,

Rui Barradas

Em 24-07-2017 13:23, Kirsten Morehouse escreveu:

Hi everyone,

I'm having some trouble with my ifelse statements.

I'm trying to put 12 conditions within 3 groups. Here is the code I have so
far:

dat$cond <- ifelse(test = dat$cond == "cond1" | dat$cond == "cond2"  |
dat$cond == "cond3" dat$cond == "cond4"
yes = "Uniform"
no = ifelse(test = dat$cond == "cond5" | dat$cond ==
"cond6") | dat$cond == "cond7" dat$cond == "cond8"
yes = "Biased Low"
no = "Biased High" )


I keep getting an error statement about an invalid ). I've tried several
permutations to fix, but without luck.

Also, can anyone help me bind columns together? I've tried:

  cbind[, c(15:25)] but get the error:  object of type 'closure' is not
subsettable

Thank you in advance!

Kirsten

[[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 statements and combining columns

2017-07-24 Thread Kirsten Morehouse
Hi everyone,

I'm having some trouble with my ifelse statements.

I'm trying to put 12 conditions within 3 groups. Here is the code I have so
far:

dat$cond <- ifelse(test = dat$cond == "cond1" | dat$cond == "cond2"  |
dat$cond == "cond3" dat$cond == "cond4"
   yes = "Uniform"
   no = ifelse(test = dat$cond == "cond5" | dat$cond ==
"cond6") | dat$cond == "cond7" dat$cond == "cond8"
   yes = "Biased Low"
   no = "Biased High" )


I keep getting an error statement about an invalid ). I've tried several
permutations to fix, but without luck.

Also, can anyone help me bind columns together? I've tried:

 cbind[, c(15:25)] but get the error:  object of type 'closure' is not
subsettable

Thank you in advance!

Kirsten

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


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 
> 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 
> 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,

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.


Re: [R] Ifelse statement on a factor level data frame

2014-09-28 Thread William Dunlap
ifelse() often has problems constructing the right type of return value.

if you want to keep the data as a factor (with its existing levels)
use x[condition] <- value instead of ifelse(condition, value, x).  E.g.,
   > x <- factor(c("Large","Small","Small","XLarge"),
levels=c("Small","Med","Large","XLarge"))
   > x
   [1] Large  Small  Small  XLarge
   Levels: Small Med Large XLarge
   > XLarge2Large <- function(x) { x[x=="XLarge"] <- "Large" ; x }
   > XLarge2Large(x)
   [1] Large Small Small Large
   Levels: Small Med Large XLarge
instead of things like
   > ifelse(x=="XLarge", "Large", x)
   [1] "3" "1" "1" "Large"

If you don't care about the factor levels, then convert x to a character vector.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, Sep 27, 2014 at 10:13 PM, Jeff Newmiller
 wrote:
> Not reproducible, ball in your court. However, in the meantime, my suggestion 
> is to not do that. Convert to character before you alter the factor, then 
> convert back when you are done.
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live Go...
>   Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> ---
> Sent from my phone. Please excuse my brevity.
>
> On September 27, 2014 9:49:41 PM PDT, Kate Ignatius  
> wrote:
>>Quick question:
>>
>>I am running the following code on some variables that are factors:
>>
>>dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
>>as.character(dbpmn[,(21)]), dbpmn[,20], '')
>>
>>Instead of returning some value it gives me this:
>>
>>c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
>>
>>Playing around with the code, gives me some kind of variation to it.
>>Is there some way to get me what I want.  The variable that its
>>suppose to give back is a bunch of sampleIDs.
>>
>>Thanks!
>>
>>__
>>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.

__
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] Ifelse statement on a factor level data frame

2014-09-28 Thread Kate Ignatius
Apologies - you're right.  Missed it in the pdf.

K.

On Sun, Sep 28, 2014 at 10:22 AM, Bert Gunter  wrote:
> Inline.
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
> (650) 467-7374
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> Clifford Stoll
>
>
>
>
> On Sun, Sep 28, 2014 at 6:38 AM, Kate Ignatius  
> wrote:
>> Strange that,
>>
>> I did put everything with as.character but all I got was the same...
>>
>> class of dbpmn[,2]) = factor
>> class of dbpmn[,21]  = factor
>> class of  dbpmn[,20] = data.frame
>>
>> This has to be a problem ???
>
> Indeed -- your failure to read documentation.
>
> I suggest you do your due diligence, read Pat Burns's link, and follow
> the advice given you by posting a reproducible example. More than
> likely the last will be unnecessary as you will figure it out in the
> course of doing what you should do.
>
> Cheers,
> Bert
>
>>
>> I can put reproducible output here but not sure if this going to of
>> help here. I think its all about factors and data frames and
>> characters...
>>
>> K.
>>
>> On Sun, Sep 28, 2014 at 1:15 AM, Jim Lemon  wrote:
>>> On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
 Quick question:

 I am running the following code on some variables that are factors:

 dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
 as.character(dbpmn[,(21)]), dbpmn[,20], '')

 Instead of returning some value it gives me this:

 c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

 Playing around with the code, gives me some kind of variation to it.
 Is there some way to get me what I want.  The variable that its
 suppose to give back is a bunch of sampleIDs.

>>> Hi Kate,
>>> If I create a little example:
>>>
>>> dbpmn<-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
>>>   V2=factor(sample(LETTERS[1:4],20,TRUE)),
>>>   V3=factor(sample(LETTERS[1:4],20,TRUE)))
>>> dbpmn[4]<-
>>>  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
>>>  dbpmn[,3],"")
>>> dbpmn
>>>V1 V2 V3 V4
>>> 1   B  D  C
>>> 2   C  A  D
>>> 3   C  B  A
>>> 4   A  B  C
>>> 5   B  D  B
>>> 6   D  D  A  1
>>> 7   D  D  D  4
>>> 8   B  C  A
>>> 9   B  D  B
>>> 10  D  C  A
>>> 11  A  D  C
>>> 12  A  C  B
>>> 13  A  A  A  1
>>> 14  D  C  A
>>> 15  C  D  B
>>> 16  A  A  B  2
>>> 17  A  C  C
>>> 18  B  B  C  3
>>> 19  C  C  C  3
>>> 20  D  D  D  4
>>>
>>> I get what I expect, the numeric value of the third element in dbpmn
>>> where the first two elements are equal. I think what you want is:
>>>
>>> dbpmn[4]<-
>>>  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
>>>  as.character(dbpmn[,3]),"")
>>> dbpmn
>>>V1 V2 V3 V4
>>> 1   B  D  C
>>> 2   C  A  D
>>> 3   C  B  A
>>> 4   A  B  C
>>> 5   B  D  B
>>> 6   D  D  A  A
>>> 7   D  D  D  D
>>> 8   B  C  A
>>> 9   B  D  B
>>> 10  D  C  A
>>> 11  A  D  C
>>> 12  A  C  B
>>> 13  A  A  A  A
>>> 14  D  C  A
>>> 15  C  D  B
>>> 16  A  A  B  B
>>> 17  A  C  C
>>> 18  B  B  C  C
>>> 19  C  C  C  C
>>> 20  D  D  D  D
>>>
>>> Jim
>>>
>>
>> __
>> 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] Ifelse statement on a factor level data frame

2014-09-28 Thread Bert Gunter
Inline.

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Sun, Sep 28, 2014 at 6:38 AM, Kate Ignatius  wrote:
> Strange that,
>
> I did put everything with as.character but all I got was the same...
>
> class of dbpmn[,2]) = factor
> class of dbpmn[,21]  = factor
> class of  dbpmn[,20] = data.frame
>
> This has to be a problem ???

Indeed -- your failure to read documentation.

I suggest you do your due diligence, read Pat Burns's link, and follow
the advice given you by posting a reproducible example. More than
likely the last will be unnecessary as you will figure it out in the
course of doing what you should do.

Cheers,
Bert

>
> I can put reproducible output here but not sure if this going to of
> help here. I think its all about factors and data frames and
> characters...
>
> K.
>
> On Sun, Sep 28, 2014 at 1:15 AM, Jim Lemon  wrote:
>> On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
>>> Quick question:
>>>
>>> I am running the following code on some variables that are factors:
>>>
>>> dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
>>> as.character(dbpmn[,(21)]), dbpmn[,20], '')
>>>
>>> Instead of returning some value it gives me this:
>>>
>>> c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
>>>
>>> Playing around with the code, gives me some kind of variation to it.
>>> Is there some way to get me what I want.  The variable that its
>>> suppose to give back is a bunch of sampleIDs.
>>>
>> Hi Kate,
>> If I create a little example:
>>
>> dbpmn<-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
>>   V2=factor(sample(LETTERS[1:4],20,TRUE)),
>>   V3=factor(sample(LETTERS[1:4],20,TRUE)))
>> dbpmn[4]<-
>>  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
>>  dbpmn[,3],"")
>> dbpmn
>>V1 V2 V3 V4
>> 1   B  D  C
>> 2   C  A  D
>> 3   C  B  A
>> 4   A  B  C
>> 5   B  D  B
>> 6   D  D  A  1
>> 7   D  D  D  4
>> 8   B  C  A
>> 9   B  D  B
>> 10  D  C  A
>> 11  A  D  C
>> 12  A  C  B
>> 13  A  A  A  1
>> 14  D  C  A
>> 15  C  D  B
>> 16  A  A  B  2
>> 17  A  C  C
>> 18  B  B  C  3
>> 19  C  C  C  3
>> 20  D  D  D  4
>>
>> I get what I expect, the numeric value of the third element in dbpmn
>> where the first two elements are equal. I think what you want is:
>>
>> dbpmn[4]<-
>>  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
>>  as.character(dbpmn[,3]),"")
>> dbpmn
>>V1 V2 V3 V4
>> 1   B  D  C
>> 2   C  A  D
>> 3   C  B  A
>> 4   A  B  C
>> 5   B  D  B
>> 6   D  D  A  A
>> 7   D  D  D  D
>> 8   B  C  A
>> 9   B  D  B
>> 10  D  C  A
>> 11  A  D  C
>> 12  A  C  B
>> 13  A  A  A  A
>> 14  D  C  A
>> 15  C  D  B
>> 16  A  A  B  B
>> 17  A  C  C
>> 18  B  B  C  C
>> 19  C  C  C  C
>> 20  D  D  D  D
>>
>> Jim
>>
>
> __
> 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] Ifelse statement on a factor level data frame

2014-09-28 Thread Kate Ignatius
Strange that,

I did put everything with as.character but all I got was the same...

class of dbpmn[,2]) = factor
class of dbpmn[,21]  = factor
class of  dbpmn[,20] = data.frame

This has to be a problem ???

I can put reproducible output here but not sure if this going to of
help here. I think its all about factors and data frames and
characters...

K.

On Sun, Sep 28, 2014 at 1:15 AM, Jim Lemon  wrote:
> On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
>> Quick question:
>>
>> I am running the following code on some variables that are factors:
>>
>> dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
>> as.character(dbpmn[,(21)]), dbpmn[,20], '')
>>
>> Instead of returning some value it gives me this:
>>
>> c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
>>
>> Playing around with the code, gives me some kind of variation to it.
>> Is there some way to get me what I want.  The variable that its
>> suppose to give back is a bunch of sampleIDs.
>>
> Hi Kate,
> If I create a little example:
>
> dbpmn<-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
>   V2=factor(sample(LETTERS[1:4],20,TRUE)),
>   V3=factor(sample(LETTERS[1:4],20,TRUE)))
> dbpmn[4]<-
>  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
>  dbpmn[,3],"")
> dbpmn
>V1 V2 V3 V4
> 1   B  D  C
> 2   C  A  D
> 3   C  B  A
> 4   A  B  C
> 5   B  D  B
> 6   D  D  A  1
> 7   D  D  D  4
> 8   B  C  A
> 9   B  D  B
> 10  D  C  A
> 11  A  D  C
> 12  A  C  B
> 13  A  A  A  1
> 14  D  C  A
> 15  C  D  B
> 16  A  A  B  2
> 17  A  C  C
> 18  B  B  C  3
> 19  C  C  C  3
> 20  D  D  D  4
>
> I get what I expect, the numeric value of the third element in dbpmn
> where the first two elements are equal. I think what you want is:
>
> dbpmn[4]<-
>  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
>  as.character(dbpmn[,3]),"")
> dbpmn
>V1 V2 V3 V4
> 1   B  D  C
> 2   C  A  D
> 3   C  B  A
> 4   A  B  C
> 5   B  D  B
> 6   D  D  A  A
> 7   D  D  D  D
> 8   B  C  A
> 9   B  D  B
> 10  D  C  A
> 11  A  D  C
> 12  A  C  B
> 13  A  A  A  A
> 14  D  C  A
> 15  C  D  B
> 16  A  A  B  B
> 17  A  C  C
> 18  B  B  C  C
> 19  C  C  C  C
> 20  D  D  D  D
>
> Jim
>

__
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] Ifelse statement on a factor level data frame

2014-09-28 Thread Patrick Burns

I believe you are in Circle 8.2.7 of
The R Inferno.

http://www.burns-stat.com/documents/books/the-r-inferno/

Pat

On 28/09/2014 05:49, Kate Ignatius wrote:

Quick question:

I am running the following code on some variables that are factors:

dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
as.character(dbpmn[,(21)]), dbpmn[,20], '')

Instead of returning some value it gives me this:

c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

Playing around with the code, gives me some kind of variation to it.
Is there some way to get me what I want.  The variable that its
suppose to give back is a bunch of sampleIDs.

Thanks!

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



--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
 'Impatient R'
 'The R Inferno'
 'Tao Te Programming')

__
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] Ifelse statement on a factor level data frame

2014-09-27 Thread Jim Lemon
On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
> Quick question:
> 
> I am running the following code on some variables that are factors:
> 
> dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
> as.character(dbpmn[,(21)]), dbpmn[,20], '')
> 
> Instead of returning some value it gives me this:
> 
> c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
> 
> Playing around with the code, gives me some kind of variation to it.
> Is there some way to get me what I want.  The variable that its
> suppose to give back is a bunch of sampleIDs.
> 
Hi Kate,
If I create a little example:

dbpmn<-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
  V2=factor(sample(LETTERS[1:4],20,TRUE)),
  V3=factor(sample(LETTERS[1:4],20,TRUE)))
dbpmn[4]<-
 ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
 dbpmn[,3],"")
dbpmn
   V1 V2 V3 V4
1   B  D  C   
2   C  A  D   
3   C  B  A   
4   A  B  C   
5   B  D  B   
6   D  D  A  1
7   D  D  D  4
8   B  C  A   
9   B  D  B   
10  D  C  A   
11  A  D  C   
12  A  C  B   
13  A  A  A  1
14  D  C  A   
15  C  D  B   
16  A  A  B  2
17  A  C  C   
18  B  B  C  3
19  C  C  C  3
20  D  D  D  4

I get what I expect, the numeric value of the third element in dbpmn 
where the first two elements are equal. I think what you want is:

dbpmn[4]<-
 ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
 as.character(dbpmn[,3]),"")
dbpmn
   V1 V2 V3 V4
1   B  D  C   
2   C  A  D   
3   C  B  A   
4   A  B  C   
5   B  D  B   
6   D  D  A  A
7   D  D  D  D
8   B  C  A   
9   B  D  B   
10  D  C  A   
11  A  D  C   
12  A  C  B   
13  A  A  A  A
14  D  C  A   
15  C  D  B   
16  A  A  B  B
17  A  C  C   
18  B  B  C  C
19  C  C  C  C
20  D  D  D  D

Jim

__
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] Ifelse statement on a factor level data frame

2014-09-27 Thread Jeff Newmiller
Not reproducible, ball in your court. However, in the meantime, my suggestion 
is to not do that. Convert to character before you alter the factor, then 
convert back when you are done.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On September 27, 2014 9:49:41 PM PDT, Kate Ignatius  
wrote:
>Quick question:
>
>I am running the following code on some variables that are factors:
>
>dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
>as.character(dbpmn[,(21)]), dbpmn[,20], '')
>
>Instead of returning some value it gives me this:
>
>c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
>
>Playing around with the code, gives me some kind of variation to it.
>Is there some way to get me what I want.  The variable that its
>suppose to give back is a bunch of sampleIDs.
>
>Thanks!
>
>__
>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.


[R] Ifelse statement on a factor level data frame

2014-09-27 Thread Kate Ignatius
Quick question:

I am running the following code on some variables that are factors:

dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) ==
as.character(dbpmn[,(21)]), dbpmn[,20], '')

Instead of returning some value it gives me this:

c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

Playing around with the code, gives me some kind of variation to it.
Is there some way to get me what I want.  The variable that its
suppose to give back is a bunch of sampleIDs.

Thanks!

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

2014-01-16 Thread Duncan Murdoch

On 16/01/2014 8:46 AM, ONKELINX, Thierry wrote:

You want
y <- ifelse(x == 'a', 1,  2)


or use if, rather than ifelse, i.e.

if (x == 'a') {
  y <- 1
} else {
  y <- 2
}

ifelse() is mainly used when you want to work with whole vectors of 
decisions, e.g.


x <- 1:10
ifelse(x > 5, 1, 0)

Duncan Murdoch

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

2014-01-16 Thread ONKELINX, Thierry
You want
y <- ifelse(x == 'a', 1,  2)

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and 
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
thierry.onkel...@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than 
asking him to perform a post-mortem examination: he may be able to say what the 
experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure 
that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-Oorspronkelijk bericht-
Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens 
Tim Smith
Verzonden: donderdag 16 januari 2014 14:32
Aan: r
Onderwerp: [R] ifelse...

Hi,

Sorry for the newbie question! My code:

x <- 'a'
ifelse(x == 'a',y <- 1, y <- 2)
print(y)

Shouldn't this assign a value of 1? When I execute this I get:

> x <- 'a'
> ifelse(x == 'a',y <- 1, y <- 2)
[1] 1
> print(y)
[1] 2


Am I doing something really daft???

thanks!



> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] splines   parallel  stats graphics  grDevices utils datasets  
methods   base

other attached packages:
 [1] Biostrings_2.30.1 XVector_0.2.0 IRanges_1.20.6
ggplot2_0.9.3.1   sda_1.3.2 fdrtool_1.2.11corpcor_1.6.6
 [8] entropy_1.2.0 scatterplot3d_0.3-34  pdist_1.2 
hash_2.2.6DAAG_1.18 multicore_0.1-7   
multtest_2.18.0 [15] XML_3.95-0.2  hgu133a.db_2.10.1 affy_1.40.0
   genefilter_1.44.0 GOstats_2.28.0graph_1.40.1  
Category_2.28.0 [22] GO.db_2.10.1  venneuler_1.1-0   rJava_0.9-6
   colorRamps_2.3RColorBrewer_1.0-5sparcl_1.0.3  
gap_1.1-10 [29] plotrix_3.5-2 som_0.3-5 pvclust_1.2-2   
  lsr_0.3.1 compute.es_0.2-2  sm_2.2-5.3
imputation_2.0.1 [36] locfit_1.5-9.1TimeProjection_0.2.0  
Matrix_1.1-1.1timeDate_3010.98  lubridate_1.3.3   gbm_2.1   
lattice_0.20-24 [43] survival_2.37-4   RobustRankAggreg_1.1  
impute_1.36.0 reshape_0.8.4 plyr_1.8  zoo_1.7-10
data.table_1.8.10 [50] foreach_1.4.1 foreign_0.8-57
languageR_1.4.1   pr!
 eprocessCore_1.24.0 gtools_3.1.1  BiocInstaller_1.12.0  
org.Hs.eg.db_2.10.1 [57] RSQLite_0.11.4DBI_0.2-7 
AnnotationDbi_1.24.0  Biobase_2.22.0BiocGenerics_0.8.0biomaRt_2.18.0

loaded via a namespace (and not attached):
 [1] affyio_1.30.0 annotate_1.40.0   AnnotationForge_1.4.4 
codetools_0.2-8   colorspace_1.2-4  dichromat_2.0-0   digest_0.6.4
 [8] grid_3.0.2GSEABase_1.24.0   gtable_0.1.2  
iterators_1.0.6   labeling_0.2  latticeExtra_0.6-26   MASS_7.3-29 
[15] munsell_0.4.2 proto_0.3-10  RBGL_1.38.0   
RCurl_1.95-4.1reshape2_1.2.2scales_0.2.3  stats4_3.0.2 
[22] stringr_0.6.2 tools_3.0.2   xtable_1.7-1  
zlibbioc_1.8.0

[[alternative HTML version deleted]]

* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en 
binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is 
door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the 
writer and may not be regarded as stating an official position of INBO, as long 
as the message is not confirmed by a duly signed document.

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

2014-01-16 Thread Tim Smith
Hi,

Sorry for the newbie question! My code:

x <- 'a'
ifelse(x == 'a',y <- 1, y <- 2)
print(y)

Shouldn't this assign a value of 1? When I execute this I get:

> x <- 'a'
> ifelse(x == 'a',y <- 1, y <- 2)
[1] 1
> print(y)
[1] 2


Am I doing something really daft???

thanks!



> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] splines   parallel  stats graphics  grDevices utils datasets  
methods   base 

other attached packages:
 [1] Biostrings_2.30.1 XVector_0.2.0 IRanges_1.20.6    
ggplot2_0.9.3.1   sda_1.3.2 fdrtool_1.2.11    corpcor_1.6.6 
   
 [8] entropy_1.2.0 scatterplot3d_0.3-34  pdist_1.2 
hash_2.2.6    DAAG_1.18 multicore_0.1-7   
multtest_2.18.0  
[15] XML_3.95-0.2  hgu133a.db_2.10.1 affy_1.40.0   
genefilter_1.44.0 GOstats_2.28.0    graph_1.40.1  
Category_2.28.0  
[22] GO.db_2.10.1  venneuler_1.1-0   rJava_0.9-6   
colorRamps_2.3    RColorBrewer_1.0-5    sparcl_1.0.3  gap_1.1-10
   
[29] plotrix_3.5-2 som_0.3-5 pvclust_1.2-2 
lsr_0.3.1 compute.es_0.2-2  sm_2.2-5.3    
imputation_2.0.1 
[36] locfit_1.5-9.1    TimeProjection_0.2.0  Matrix_1.1-1.1    
timeDate_3010.98  lubridate_1.3.3   gbm_2.1   
lattice_0.20-24  
[43] survival_2.37-4   RobustRankAggreg_1.1  impute_1.36.0 
reshape_0.8.4 plyr_1.8  zoo_1.7-10    
data.table_1.8.10    
[50] foreach_1.4.1 foreign_0.8-57    languageR_1.4.1   
preprocessCore_1.24.0 gtools_3.1.1  BiocInstaller_1.12.0  
org.Hs.eg.db_2.10.1  
[57] RSQLite_0.11.4    DBI_0.2-7 AnnotationDbi_1.24.0  
Biobase_2.22.0    BiocGenerics_0.8.0    biomaRt_2.18.0   

loaded via a namespace (and not attached):
 [1] affyio_1.30.0 annotate_1.40.0   AnnotationForge_1.4.4 
codetools_0.2-8   colorspace_1.2-4  dichromat_2.0-0   digest_0.6.4  
   
 [8] grid_3.0.2    GSEABase_1.24.0   gtable_0.1.2  
iterators_1.0.6   labeling_0.2  latticeExtra_0.6-26   MASS_7.3-29   
   
[15] munsell_0.4.2 proto_0.3-10  RBGL_1.38.0   
RCurl_1.95-4.1    reshape2_1.2.2    scales_0.2.3  stats4_3.0.2  
   
[22] stringr_0.6.2 tools_3.0.2   xtable_1.7-1  
zlibbioc_1.8.0

[[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] ifelse statement with two vectors of different length

2013-12-18 Thread arun
Hi Adel,

If the problem is the spacing, then
library(stringr)
1*(long_df$country_name %in% str_trim(countrydiff))
# [1] 1 0 0 1 1 0 0 0 0 0
A.K.


Dear Arun 

Thanks for your reply, it made me realize that the problem was 
not in the code but in the levels() of the factors. Some countries had 
some extra spacing which made the ifelse() function not work. So if I 
modify your code (added space to countrydiff), it will then look 
something like this: 

countrydiff <- c("Albania    ", "Algeria    ", "Belarus    ", "Canada   ", 
"Germany   ") 
long_df <- data.frame(country_name = c("Algeria", "Guyana", 
"Hungary", "Algeria", "Canada", "Iran", "Iran", "Norway","Uruguay", 
"Zimbabwe") ) 

I had to use the gsub to fix this first. 


Interestingly, the setdiff() function did not react on 
spacing difference which I used before coming to the ifelse statement 
and therefore I did not react on this in the first place 

#no reaction from R on spacing diff. 
setdiff(countrydiff, long_df$country_name) 


Nevertheless, thanks again for being helpful! 
Adel 


On Wednesday, December 18, 2013 9:58 AM, Adel  
wrote:

Dear list-members,

I have the following problem: I have a vector (countrydiff) with length 72
and another vector (long_df$country_name) which is about 12000 long.
Basically what I want to do is to if the factor level (or string name) in
long_df$country_name appears on the countrydiff, then long_df$povdat should
be equal to 1, if it does not appear on the countrydiff vector then
long_df$povdat should be equal to zero. I have tried different combinations
and read some. The following code should in my mind do it, but it doesn’t:

long_df$povdat<-ifelse(long_df$country_name == countrydiff, 1, 0)

long_df$povdat<-ifelse(long_df$country_name %in% countrydiff, 1, 0)

Additional information: the factor vector countrydiff contains unique
country names (Albania, Zimbabwe etc.), whereas long_df$country_name also
contains country names albeit not unique since it is in longform. The unique
names that appear in long_df$country_name is around 200.


Any suggestions?
Thanks in advance.

Best
Adel




--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-with-two-vectors-of-different-length-tp4682401.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.


__
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] ifelse statement with two vectors of different length

2013-12-18 Thread Adel

Dear Arun

Thanks for your reply, it made me realize that the problem was not in the
code but in the levels() of the factors. Some countries had some extra
spacing which made the ifelse() function not work. So if I modify your code
(added space to countrydiff), it will then look something like this:

countrydiff <- c("Albania", "Algeria", "Belarus", "Canada   ",
"Germany   ") 
long_df <- data.frame(country_name = c("Algeria", "Guyana", "Hungary",
"Algeria", "Canada", "Iran", "Iran", "Norway","Uruguay", "Zimbabwe") ) 

I had to use the gsub to fix this first.


Interestingly, the setdiff() function did not react on spacing difference
which I used before coming to the ifelse statement and therefore I did not
react on this in the first place

#no reaction from R on spacing diff.
setdiff(countrydiff, long_df$country_name)


Nevertheless, thanks again for being helpful!
Adel




--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-with-two-vectors-of-different-length-tp4682401p4682403.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.


Re: [R] ifelse statement with two vectors of different length

2013-12-18 Thread Sarah Goslee
Hi,

Suggestion 1: read
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
and bookmark it for future reference.

Suggestion 2:
set.seed(123)
countrydiff <- letters[1:5]
long_df <- data.frame(country_name = sample(letters[1:8], 20, replace=TRUE))

long_df$povdat <- as.numeric(long_df$country_name %in% countrydiff)

Sarah

On Wed, Dec 18, 2013 at 8:57 AM, Adel  wrote:
>
> Dear list-members,
>
> I have the following problem: I have a vector (countrydiff) with length 72
> and another vector (long_df$country_name) which is about 12000 long.
> Basically what I want to do is to if the factor level (or string name) in
> long_df$country_name appears on the countrydiff, then long_df$povdat should
> be equal to 1, if it does not appear on the countrydiff vector then
> long_df$povdat should be equal to zero. I have tried different combinations
> and read some. The following code should in my mind do it, but it doesn’t:
>
> long_df$povdat<-ifelse(long_df$country_name == countrydiff, 1, 0)
>
> long_df$povdat<-ifelse(long_df$country_name %in% countrydiff, 1, 0)
>
> Additional information: the factor vector countrydiff contains unique
> country names (Albania, Zimbabwe etc.), whereas long_df$country_name also
> contains country names albeit not unique since it is in longform. The unique
> names that appear in long_df$country_name is around 200.
>
>
> Any suggestions?
> Thanks in advance.
>
> Best
> Adel
>
>

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] ifelse statement with two vectors of different length

2013-12-18 Thread Adel

Dear list-members,

I have the following problem: I have a vector (countrydiff) with length 72
and another vector (long_df$country_name) which is about 12000 long.
Basically what I want to do is to if the factor level (or string name) in
long_df$country_name appears on the countrydiff, then long_df$povdat should
be equal to 1, if it does not appear on the countrydiff vector then
long_df$povdat should be equal to zero. I have tried different combinations
and read some. The following code should in my mind do it, but it doesn’t:

long_df$povdat<-ifelse(long_df$country_name == countrydiff, 1, 0)

long_df$povdat<-ifelse(long_df$country_name %in% countrydiff, 1, 0)

Additional information: the factor vector countrydiff contains unique
country names (Albania, Zimbabwe etc.), whereas long_df$country_name also
contains country names albeit not unique since it is in longform. The unique
names that appear in long_df$country_name is around 200.


Any suggestions?
Thanks in advance.

Best
Adel




--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-with-two-vectors-of-different-length-tp4682401.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.


Re: [R] ifelse statement with two vectors of different length

2013-12-18 Thread arun
Hi,
Please show a reproducible example.

countrydiff <- c("Albania", "Algeria", "Belarus", "Canada", "Germany")
long_df <- data.frame(country_name = c("Algeria", "Guyana", "Hungary", 
"Algeria", "Canada", "Iran", "Iran", "Norway","Uruguay", "Zimbabwe") )
 ifelse(long_df$country_name %in% countrydiff,1,0)
# [1] 1 0 0 1 1 0 0 0 0 0
#or
1*(long_df$country_name %in% countrydiff)
# [1] 1 0 0 1 1 0 0 0 0 0
A.K.




Dear list-members, 

I have the following problem: I have a vector (countrydiff) with
 length 72 and another vector (long_df$country_name) which is about 
12000 long. Basically what I want to do is to if the factor level (or 
string name) in long_df$country_name appears on the countrydiff, then 
long_df$povdat should be equal to 1, if it does not appear on the 
countrydiff vector then long_df$povdat should be equal to zero. I have 
tried different combinations and read some. The following code should in
 my mind do it, but it doesn’t: 

long_df$povdat<-ifelse(long_df$country_name == countrydiff, 1, 0) 

long_df$povdat<-ifelse(long_df$country_name %in% countrydiff, 1, 0) 

Additional information: the factor vector countrydiff contains 
unique country names (Albania, Zimbabwe etc.), whereas 
long_df$country_name also contains country names albeit not unique since
 it is in longform. The unique names that appear in long_df$country_name
 is around 200. 


Any suggestions? 
Thanks in advance. 

Best 
Adel

__
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] ifelse -does it "manage the indexing"?

2013-12-03 Thread David Winsemius

On Dec 3, 2013, at 5:37 AM, Hadley Wickham wrote:

> A better solution to this problem is to use character indexing:
> 
> x <- c("Tuesday", "Thursday", "Sunday")
> c(Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5,
> Saturday = 6, Sunday = 7)[x]
> 
> http://adv-r.had.co.nz/Subsetting.html#lookup-tables-character-subsetting

That does seem more expressive than using match:

x <- c("Tuesday", "Thursday", "Sunday")
match(x, c('Monday', 'Tuesday', 'Wednesday' ,'Thursday' ,
   'Friday',  'Saturday', 'Sunday') ) 
[1] 2 4 7

It would also lend itself well to grouping operations

x <- c("Tuesday", "Thursday", "Sunday")
c(Monday = 'first half', Tuesday = 'first half',
  Wednesday = "hump", 
  Thursday = 'second half', Friday = 'second half',
  Saturday = "weekend", Sunday = "weekend")[x]

  Tuesday  ThursdaySunday 
 "first half" "second half" "weekend" 

The corresponding use case with match would be:

c(rep('first half',2),"hump", rep('second half',2), rep("weekend",2) )[
   match(x, c('Monday', 'Tuesday', 'Wednesday' ,'Thursday' ,
'Friday',  'Saturday', 'Sunday') ) ]
[1] "first half"  "second half" "weekend"

And your recommended method has the virtue of performing the same partial 
matching allowed by charmatch:

 x2 <- substr(x, 1,3)
 charmatch(x2, c('Monday', 'Tuesday', 'Wednesday' ,'Thursday' ,
'Friday',  'Saturday', 'Sunday') )
#[1] 2 4 7
 c(Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5,
 Saturday = 6, Sunday = 7)[x]
# Tuesday Thursday   Sunday 
#   247 

-- 
David.

> 
> Hadley
> 
> On Mon, Dec 2, 2013 at 6:33 PM, Bill  wrote:
>> ifelse ((day_of_week == "Monday"),1,
>>  ifelse ((day_of_week == "Tuesday"),2,
>>  ifelse ((day_of_week == "Wednesday"),3,
>>  ifelse ((day_of_week == "Thursday"),4,
>>  ifelse ((day_of_week == "Friday"),5,
>>  ifelse ((day_of_week == "Saturday"),6,7)))
>> 
>> 
>>  In code like the above, day_of_week is a vector and so day_of_week ==
>> "Monday" will result in a boolean vector. Suppose day_of_week is Monday,
>> Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
>> True,False,False,False. I think that ifelse will test the first element and
>> it will generate a 1. At this point it will not have run day_of_week ==
>> "Tuesday" yet. Then it will test the second element of day_of_week and it
>> will be false and this will cause it to evaluate day_of_week == "Tuesday".
>> My question would be, does the evaluation of day_of_week == "Tuesday"
>> result in the generation of an entire boolean vector (which would be in
>> this case False,False,False,True) or does the ifelse "manage the indexing"
>> so that it only tests the second element of the original vector (which is
>> Thursday) and for that matter does it therefore not even bother to generate
>> the first boolean vector I mentioned above (True,False,False,False) but
>> rather just checks the first element?
>>  Not sure if I have explained this well but if you understand I would
>> appreciate a reply.
>>  Thanks.
>> 
>>[[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.
> 
> 
> 
> -- 
> http://had.co.nz/
> 
> __
> 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.

David Winsemius
Alameda, CA, USA

__
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] ifelse -does it "manage the indexing"?

2013-12-03 Thread Hadley Wickham
A better solution to this problem is to use character indexing:

x <- c("Tuesday", "Thursday", "Sunday")
c(Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5,
Saturday = 6, Sunday = 7)[x]

http://adv-r.had.co.nz/Subsetting.html#lookup-tables-character-subsetting

Hadley

On Mon, Dec 2, 2013 at 6:33 PM, Bill  wrote:
> ifelse ((day_of_week == "Monday"),1,
>   ifelse ((day_of_week == "Tuesday"),2,
>   ifelse ((day_of_week == "Wednesday"),3,
>   ifelse ((day_of_week == "Thursday"),4,
>   ifelse ((day_of_week == "Friday"),5,
>   ifelse ((day_of_week == "Saturday"),6,7)))
>
>
>   In code like the above, day_of_week is a vector and so day_of_week ==
> "Monday" will result in a boolean vector. Suppose day_of_week is Monday,
> Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
> True,False,False,False. I think that ifelse will test the first element and
> it will generate a 1. At this point it will not have run day_of_week ==
> "Tuesday" yet. Then it will test the second element of day_of_week and it
> will be false and this will cause it to evaluate day_of_week == "Tuesday".
> My question would be, does the evaluation of day_of_week == "Tuesday"
> result in the generation of an entire boolean vector (which would be in
> this case False,False,False,True) or does the ifelse "manage the indexing"
> so that it only tests the second element of the original vector (which is
> Thursday) and for that matter does it therefore not even bother to generate
> the first boolean vector I mentioned above (True,False,False,False) but
> rather just checks the first element?
>   Not sure if I have explained this well but if you understand I would
> appreciate a reply.
>   Thanks.
>
> [[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.



-- 
http://had.co.nz/

__
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] ifelse -does it "manage the indexing"?

2013-12-02 Thread Bill
Duncan,

Your solution seems so much simpler. I was reading The Art of R Programming
and it says the following:

g=c("M","F","F","I","M","M","F",

ifelse (g == "M", 1, ifelse (g == "F", 2, 3))

[1] 1 2 2 3 1 1 2

What actually happens in that nested ifelse () ? Let’s take a careful look.
First, for the sake of concreteness, let’s find what the formal argument
names are in the function ifelse ():

args(ifelse)

function (test, yes, no)

NULL

Remember, for each element of test that is true, the function evaluates to
the corresponding element in yes. Similarly, if test [1] is false, the
function evaluates to no [1] . All values so generated are returned together
in a vector.
**
MY COMMENT: so the first vector seems to be conditional seems to be
evaluated in full to the full boolean vector.

In our case here, R will execute the outer ifelse () call first, in which
test
is g == "M", and yes is 1 (recycled); no will (later) be the result of
executing
ifelse (g=="F", 2, 3). Now since test [11 is true, we generate yes [1]
which is
1. So, the first element of the return value of our outer call will be 1.

Next R will evaluate test[2] . That is false, so R needs to find no[2] . R
now
needs to execute the inner ifelse () call. It hasn’t done so before, because
it hasn’t needed it until now. R uses the principle of lazy evaluation,
meaning that an expression is not computed until it is needed.
***
MY COMMENT: The above sentence seems to be relevant.
**

R will now evaluate ifelse (g=="F", 2, 3), yielding (3,2,2,3,3,3,2); this
is no
for the outer ifelse () call, so the latter’s second return element will be
 the second element of (3,2,2,3,3,3,2), which is 2.
***
MY COMMENT: So this is evaluated at least this once completely.
*


When the outer ifelse () call gets to test [41 , it will see that value to
be
false and thus will return no [41 . Since R had already computed no, it has
the value needed, which is 3.
***
MY COMMENT: I am not sure if the implecation here is that the indexing is
managed or not.



On Mon, Dec 2, 2013 at 5:16 PM, William Dunlap  wrote:

> > It seems so inefficient.
>
> But ifelse knows nothing about the expressions given
> as its second and third arguments -- it only sees their
> values after they are evaluated.  Even if it could see the
> expressions, it would not be able to assume that f(x[i])
> is the same as f(x)[i] or things like
>ifelse(x>0, cumsum(x), cumsum(-x))
> would not work.
>
> You can avoid the computing all of f(x) and then extracting
> a few elements from it by doing something like
>x <- c("Wednesday", "Monday", "Wednesday")
>z1 <- character(length(x))
>z1[x=="Monday"] <- "Mon"
>z1[x=="Tuesday"] <- "Tue"
>z1[x=="Wednesday"] <- "Wed"
> or
>LongDayNames <- c("Monday","Tuesday","Wednesday")
>ShortDayNames <- c("Mon", "Tue", "Wed")
>z2 <- character(length(x))
>for(i in seq_along(LongDayNames)) {
>   z2[x==LongDayNames[i]] <- ShortDayNames[i]
>}
>
> To avoid the repeated x==value[i] you can use match(x, values).
>z3 <- ShortDayNames[match(x, LongDayNames)]
>
> z1, z2, and z3 are identical  character vectors.
>
> Or, you can use factors.
>> factor(x, levels=LongDayNames, labels=ShortDayNames)
>[1] Wed Mon Wed
>Levels: Mon Tue Wed
>
> 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 Bill
> > Sent: Monday, December 02, 2013 4:50 PM
> > To: Duncan Murdoch
> > Cc: r-help@r-project.org
> > Subject: Re: [R] ifelse -does it "manage the indexing"?
> >
> > It seems so inefficient. I mean the whole first vector will be evaluated.
> > Then if the second if is run the whole vector will be evaluated again.
> Then
> > if the next if is run the whole vector will be evaluted again. And so on.
> > And this could be only to test the first element (if it is false for each
> > if statement). Then this would be repeated again and again. Is that
> really
> > the way it works? Or am I not thinking clearly?
> >
> >
> > On Mon, Dec 2, 2013 at 4:48 PM, Duncan Murdoch
> > wrote:
> >
> > > On 13-12-02 7:33 PM, Bill wrote:
> > >
> > >> ifelse ((day_of_week == "Monday"),1,
> > >>ifelse ((day_of_week == "Tuesday&quo

Re: [R] ifelse -does it "manage the indexing"?

2013-12-02 Thread Bill
Duncan,

Your solution seems so much simpler. I was reading The Art of R Programming
and it says the following:


g=c("M","F","F","I","M","M","F",

ifelse (g == "M", 1, ifelse (g == "F", 2, 3))

[1] 1 2 2 3 1 1 2

What actually happens in that nested ifelse () ? Let’s take a careful look.
First, for the sake of concreteness, let’s find what the formal argument
names are in the function ifelse ():

args(ifelse)

function (test, yes, no)

NULL

Remember, for each element of test that is true, the function evaluates to
the corresponding element in yes. Similarly, if test [11 is false, the
function evaluates to no [1] . All values so generated are returned together
in a vector.
**
MY COMMENT: so the first vector seems to be conditional seems to be
evaluated in full to the full boolean vector.

In our case here, R will execute the outer ifelse () call first, in which
test
is g == "M", and yes is 1 (recycled); no will (later) be the result of
executing
ifelse (g=="F", 2, 3). Now since test [11 is true, we generate yes [1]
which is
1. So, the first element of the return value of our outer call will be 1.

Next R will evaluate test[2] . That is false, so R needs to find no[2] . R
now
needs to execute the inner ifelse () call. It hasn’t done so before, because
it hasn’t needed it until now. R uses the principle of lazy evaluation,
meaning that an expression is not computed until it is needed.
***
MY COMMENT: The above sentence seems to be relevant.
**

R will now evaluate ifelse (g=="F", 2, 3), yielding (3,2,2,3,3,3,2); this
is no
for the outer ifelse () call, so the latter’s second return element will be
 the second element of (3,2,2,3,3,3,2), which is 2.
***
MY COMMENT: So this is evaluated at least this once completely.
*


When the outer ifelse () call gets to test [41 , it will see that value to
be
false and thus will return no [41 . Since R had already computed no, it has
the value needed, which is 3.
***
MY COMMENT: I am not sure if the implecation here is that the indexing is
managed or not.


On Mon, Dec 2, 2013 at 5:16 PM, William Dunlap  wrote:

> > It seems so inefficient.
>
> But ifelse knows nothing about the expressions given
> as its second and third arguments -- it only sees their
> values after they are evaluated.  Even if it could see the
> expressions, it would not be able to assume that f(x[i])
> is the same as f(x)[i] or things like
>ifelse(x>0, cumsum(x), cumsum(-x))
> would not work.
>
> You can avoid the computing all of f(x) and then extracting
> a few elements from it by doing something like
>x <- c("Wednesday", "Monday", "Wednesday")
>z1 <- character(length(x))
>z1[x=="Monday"] <- "Mon"
>z1[x=="Tuesday"] <- "Tue"
>z1[x=="Wednesday"] <- "Wed"
> or
>LongDayNames <- c("Monday","Tuesday","Wednesday")
>ShortDayNames <- c("Mon", "Tue", "Wed")
>z2 <- character(length(x))
>for(i in seq_along(LongDayNames)) {
>   z2[x==LongDayNames[i]] <- ShortDayNames[i]
>}
>
> To avoid the repeated x==value[i] you can use match(x, values).
>z3 <- ShortDayNames[match(x, LongDayNames)]
>
> z1, z2, and z3 are identical  character vectors.
>
> Or, you can use factors.
>> factor(x, levels=LongDayNames, labels=ShortDayNames)
>[1] Wed Mon Wed
>Levels: Mon Tue Wed
>
> 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 Bill
> > Sent: Monday, December 02, 2013 4:50 PM
> > To: Duncan Murdoch
> > Cc: r-help@r-project.org
> > Subject: Re: [R] ifelse -does it "manage the indexing"?
> >
> > It seems so inefficient. I mean the whole first vector will be evaluated.
> > Then if the second if is run the whole vector will be evaluated again.
> Then
> > if the next if is run the whole vector will be evaluted again. And so on.
> > And this could be only to test the first element (if it is false for each
> > if statement). Then this would be repeated again and again. Is that
> really
> > the way it works? Or am I not thinking clearly?
> >
> >
> > On Mon, Dec 2, 2013 at 4:48 PM, Duncan Murdoch
> > wrote:
> >
> > > On 13-12-02 7:33 PM, Bill wrote:
> > >
> > >> ifelse ((day_of_week == "Monday"),1,
> > >>ifelse ((day_of_week == "Tuesday&quo

Re: [R] ifelse -does it "manage the indexing"?

2013-12-02 Thread William Dunlap
> It seems so inefficient.

But ifelse knows nothing about the expressions given
as its second and third arguments -- it only sees their
values after they are evaluated.  Even if it could see the
expressions, it would not be able to assume that f(x[i])
is the same as f(x)[i] or things like
   ifelse(x>0, cumsum(x), cumsum(-x))
would not work.

You can avoid the computing all of f(x) and then extracting
a few elements from it by doing something like
   x <- c("Wednesday", "Monday", "Wednesday")
   z1 <- character(length(x))
   z1[x=="Monday"] <- "Mon"
   z1[x=="Tuesday"] <- "Tue"
   z1[x=="Wednesday"] <- "Wed"
or
   LongDayNames <- c("Monday","Tuesday","Wednesday")
   ShortDayNames <- c("Mon", "Tue", "Wed")
   z2 <- character(length(x))
   for(i in seq_along(LongDayNames)) {
  z2[x==LongDayNames[i]] <- ShortDayNames[i]
   }

To avoid the repeated x==value[i] you can use match(x, values).
   z3 <- ShortDayNames[match(x, LongDayNames)]

z1, z2, and z3 are identical  character vectors.

Or, you can use factors.
   > factor(x, levels=LongDayNames, labels=ShortDayNames)
   [1] Wed Mon Wed
   Levels: Mon Tue Wed

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 Bill
> Sent: Monday, December 02, 2013 4:50 PM
> To: Duncan Murdoch
> Cc: r-help@r-project.org
> Subject: Re: [R] ifelse -does it "manage the indexing"?
> 
> It seems so inefficient. I mean the whole first vector will be evaluated.
> Then if the second if is run the whole vector will be evaluated again. Then
> if the next if is run the whole vector will be evaluted again. And so on.
> And this could be only to test the first element (if it is false for each
> if statement). Then this would be repeated again and again. Is that really
> the way it works? Or am I not thinking clearly?
> 
> 
> On Mon, Dec 2, 2013 at 4:48 PM, Duncan Murdoch
> wrote:
> 
> > On 13-12-02 7:33 PM, Bill wrote:
> >
> >> ifelse ((day_of_week == "Monday"),1,
> >>ifelse ((day_of_week == "Tuesday"),2,
> >>ifelse ((day_of_week == "Wednesday"),3,
> >>ifelse ((day_of_week == "Thursday"),4,
> >>ifelse ((day_of_week == "Friday"),5,
> >>ifelse ((day_of_week == "Saturday"),6,7)))
> >>
> >>
> >>In code like the above, day_of_week is a vector and so day_of_week ==
> >> "Monday" will result in a boolean vector. Suppose day_of_week is Monday,
> >> Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
> >> True,False,False,False. I think that ifelse will test the first element
> >> and
> >> it will generate a 1. At this point it will not have run day_of_week ==
> >> "Tuesday" yet. Then it will test the second element of day_of_week and it
> >> will be false and this will cause it to evaluate day_of_week == "Tuesday".
> >> My question would be, does the evaluation of day_of_week == "Tuesday"
> >> result in the generation of an entire boolean vector (which would be in
> >> this case False,False,False,True) or does the ifelse "manage the indexing"
> >> so that it only tests the second element of the original vector (which is
> >> Thursday) and for that matter does it therefore not even bother to
> >> generate
> >> the first boolean vector I mentioned above (True,False,False,False) but
> >> rather just checks the first element?
> >>Not sure if I have explained this well but if you understand I would
> >> appreciate a reply.
> >>
> >
> > See the help for the function.  If any element of the test is true, the
> > full first vector will be evaluated.  If any element is false, the second
> > one will be evaluated.  There are no shortcuts of the kind you describe.
> >
> > Duncan Murdoch
> >
> >
> 
>   [[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] ifelse -does it "manage the indexing"?

2013-12-02 Thread Bill
Hi. Thanks. Which part? I looked at the below but I don't think it clearly
addresses the nested ifelse situation.


ifelse {base}R DocumentationConditional Element Selection Description

ifelse returns a value with the same shape as test which is filled with
elements selected from either yes or no depending on whether the element of
test is TRUE or FALSE.
Usage

ifelse(test, yes, no)

Argumentstest

an object which can be coerced to logical mode.
yes

return values for true elements of test.
no

return values for false elements of test.
Details

If yes or no are too short, their elements are recycled. yes will be
evaluated if and only if any element of test is true, and analogously for no
.

Missing values in test give missing values in the result.
Value

A vector of the same length and attributes (including dimensions and "class")
as test and data values from the values of yes or no. The mode of the
answer will be coerced from logical to accommodate first any values taken
from yes and then any values taken from no.
Warning

The mode of the result may depend on the value of test (see the examples),
and the class attribute (see
oldClass)
of the result is taken from testand may be inappropriate for the values
selected from yes and no.

Sometimes it is better to use a construction such as

  (tmp <- yes; tmp[!test] <- no[!test]; tmp)

, possibly extended to handle missing values in test. References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) *The New S Language*.
Wadsworth & Brooks/Cole.
See Also

if .
Examples

x <- c(6:-4)
sqrt(x)  #- gives warning
sqrt(ifelse(x >= 0, x, NA))  # no warning

## Note: the following also gives the warning !
ifelse(x >= 0, sqrt(x), NA)

## example of different return modes:
yes <- 1:3
no <- pi^(0:3)
typeof(ifelse(NA, yes, no))# logical
typeof(ifelse(TRUE, yes, no))  # integer
typeof(ifelse(FALSE, yes, no)) # double



On Mon, Dec 2, 2013 at 5:09 PM, Duncan Murdoch wrote:

> On 13-12-02 7:49 PM, Bill wrote:
>
>> It seems so inefficient. I mean the whole first vector will be
>> evaluated. Then if the second if is run the whole vector will be
>> evaluated again. Then if the next if is run the whole vector will be
>> evaluted again. And so on. And this could be only to test the first
>> element (if it is false for each if statement). Then this would be
>> repeated again and again. Is that really the way it works? Or am I not
>> thinking clearly?
>>
>
> Read the manual.
>
> Duncan Murdoch
>
>
>>
>> On Mon, Dec 2, 2013 at 4:48 PM, Duncan Murdoch > > wrote:
>>
>> On 13-12-02 7:33 PM, Bill wrote:
>>
>> ifelse ((day_of_week == "Monday"),1,
>> ifelse ((day_of_week == "Tuesday"),2,
>> ifelse ((day_of_week == "Wednesday"),3,
>> ifelse ((day_of_week == "Thursday"),4,
>> ifelse ((day_of_week == "Friday"),5,
>> ifelse ((day_of_week == "Saturday"),6,7)))
>>
>>
>> In code like the above, day_of_week is a vector and so
>> day_of_week ==
>> "Monday" will result in a boolean vector. Suppose day_of_week is
>> Monday,
>> Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
>> True,False,False,False. I think that ifelse will test the first
>> element and
>> it will generate a 1. At this point it will not have run
>> day_of_week ==
>> "Tuesday" yet. Then it will test the second element of
>> day_of_week and it
>> will be false and this will cause it to evaluate day_of_week ==
>> "Tuesday".
>> My question would be, does the evaluation of day_of_week ==
>> "Tuesday"
>> result in the generation of an entire boolean vector (which
>> would be in
>> this case False,False,False,True) or does the ifelse "manage the
>> indexing"
>> so that it only tests the second element of the original vector
>> (which is
>> Thursday) and for that matter does it therefore not even bother
>> to generate
>> the first boolean vector I mentioned above
>> (True,False,False,False) but
>> rather just checks the first element?
>> Not sure if I have explained this well but if you understand
>> I would
>> appreciate a reply.
>>
>>
>> See the help for the function.  If any element of the test is true,
>> the full first vector will be evaluated.  If any element is false,
>> the second one will be evaluated.  There are no shortcuts of the
>> kind you describe.
>>
>> Duncan Murdoch
>>
>>
>>
>

[[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 commente

Re: [R] ifelse -does it "manage the indexing"?

2013-12-02 Thread Duncan Murdoch

On 13-12-02 7:49 PM, Bill wrote:

It seems so inefficient. I mean the whole first vector will be
evaluated. Then if the second if is run the whole vector will be
evaluated again. Then if the next if is run the whole vector will be
evaluted again. And so on. And this could be only to test the first
element (if it is false for each if statement). Then this would be
repeated again and again. Is that really the way it works? Or am I not
thinking clearly?


Read the manual.

Duncan Murdoch




On Mon, Dec 2, 2013 at 4:48 PM, Duncan Murdoch mailto:murdoch.dun...@gmail.com>> wrote:

On 13-12-02 7:33 PM, Bill wrote:

ifelse ((day_of_week == "Monday"),1,
ifelse ((day_of_week == "Tuesday"),2,
ifelse ((day_of_week == "Wednesday"),3,
ifelse ((day_of_week == "Thursday"),4,
ifelse ((day_of_week == "Friday"),5,
ifelse ((day_of_week == "Saturday"),6,7)))


In code like the above, day_of_week is a vector and so
day_of_week ==
"Monday" will result in a boolean vector. Suppose day_of_week is
Monday,
Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
True,False,False,False. I think that ifelse will test the first
element and
it will generate a 1. At this point it will not have run
day_of_week ==
"Tuesday" yet. Then it will test the second element of
day_of_week and it
will be false and this will cause it to evaluate day_of_week ==
"Tuesday".
My question would be, does the evaluation of day_of_week ==
"Tuesday"
result in the generation of an entire boolean vector (which
would be in
this case False,False,False,True) or does the ifelse "manage the
indexing"
so that it only tests the second element of the original vector
(which is
Thursday) and for that matter does it therefore not even bother
to generate
the first boolean vector I mentioned above
(True,False,False,False) but
rather just checks the first element?
Not sure if I have explained this well but if you understand
I would
appreciate a reply.


See the help for the function.  If any element of the test is true,
the full first vector will be evaluated.  If any element is false,
the second one will be evaluated.  There are no shortcuts of the
kind you describe.

Duncan Murdoch




__
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] ifelse -does it "manage the indexing"?

2013-12-02 Thread Bill
It seems so inefficient. I mean the whole first vector will be evaluated.
Then if the second if is run the whole vector will be evaluated again. Then
if the next if is run the whole vector will be evaluted again. And so on.
And this could be only to test the first element (if it is false for each
if statement). Then this would be repeated again and again. Is that really
the way it works? Or am I not thinking clearly?


On Mon, Dec 2, 2013 at 4:48 PM, Duncan Murdoch wrote:

> On 13-12-02 7:33 PM, Bill wrote:
>
>> ifelse ((day_of_week == "Monday"),1,
>>ifelse ((day_of_week == "Tuesday"),2,
>>ifelse ((day_of_week == "Wednesday"),3,
>>ifelse ((day_of_week == "Thursday"),4,
>>ifelse ((day_of_week == "Friday"),5,
>>ifelse ((day_of_week == "Saturday"),6,7)))
>>
>>
>>In code like the above, day_of_week is a vector and so day_of_week ==
>> "Monday" will result in a boolean vector. Suppose day_of_week is Monday,
>> Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
>> True,False,False,False. I think that ifelse will test the first element
>> and
>> it will generate a 1. At this point it will not have run day_of_week ==
>> "Tuesday" yet. Then it will test the second element of day_of_week and it
>> will be false and this will cause it to evaluate day_of_week == "Tuesday".
>> My question would be, does the evaluation of day_of_week == "Tuesday"
>> result in the generation of an entire boolean vector (which would be in
>> this case False,False,False,True) or does the ifelse "manage the indexing"
>> so that it only tests the second element of the original vector (which is
>> Thursday) and for that matter does it therefore not even bother to
>> generate
>> the first boolean vector I mentioned above (True,False,False,False) but
>> rather just checks the first element?
>>Not sure if I have explained this well but if you understand I would
>> appreciate a reply.
>>
>
> See the help for the function.  If any element of the test is true, the
> full first vector will be evaluated.  If any element is false, the second
> one will be evaluated.  There are no shortcuts of the kind you describe.
>
> Duncan Murdoch
>
>

[[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] ifelse -does it "manage the indexing"?

2013-12-02 Thread Duncan Murdoch

On 13-12-02 7:33 PM, Bill wrote:

ifelse ((day_of_week == "Monday"),1,
   ifelse ((day_of_week == "Tuesday"),2,
   ifelse ((day_of_week == "Wednesday"),3,
   ifelse ((day_of_week == "Thursday"),4,
   ifelse ((day_of_week == "Friday"),5,
   ifelse ((day_of_week == "Saturday"),6,7)))


   In code like the above, day_of_week is a vector and so day_of_week ==
"Monday" will result in a boolean vector. Suppose day_of_week is Monday,
Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
True,False,False,False. I think that ifelse will test the first element and
it will generate a 1. At this point it will not have run day_of_week ==
"Tuesday" yet. Then it will test the second element of day_of_week and it
will be false and this will cause it to evaluate day_of_week == "Tuesday".
My question would be, does the evaluation of day_of_week == "Tuesday"
result in the generation of an entire boolean vector (which would be in
this case False,False,False,True) or does the ifelse "manage the indexing"
so that it only tests the second element of the original vector (which is
Thursday) and for that matter does it therefore not even bother to generate
the first boolean vector I mentioned above (True,False,False,False) but
rather just checks the first element?
   Not sure if I have explained this well but if you understand I would
appreciate a reply.


See the help for the function.  If any element of the test is true, the 
full first vector will be evaluated.  If any element is false, the 
second one will be evaluated.  There are no shortcuts of the kind you 
describe.


Duncan Murdoch

__
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] ifelse -does it "manage the indexing"?

2013-12-02 Thread Bill
ifelse ((day_of_week == "Monday"),1,
  ifelse ((day_of_week == "Tuesday"),2,
  ifelse ((day_of_week == "Wednesday"),3,
  ifelse ((day_of_week == "Thursday"),4,
  ifelse ((day_of_week == "Friday"),5,
  ifelse ((day_of_week == "Saturday"),6,7)))


  In code like the above, day_of_week is a vector and so day_of_week ==
"Monday" will result in a boolean vector. Suppose day_of_week is Monday,
Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
True,False,False,False. I think that ifelse will test the first element and
it will generate a 1. At this point it will not have run day_of_week ==
"Tuesday" yet. Then it will test the second element of day_of_week and it
will be false and this will cause it to evaluate day_of_week == "Tuesday".
My question would be, does the evaluation of day_of_week == "Tuesday"
result in the generation of an entire boolean vector (which would be in
this case False,False,False,True) or does the ifelse "manage the indexing"
so that it only tests the second element of the original vector (which is
Thursday) and for that matter does it therefore not even bother to generate
the first boolean vector I mentioned above (True,False,False,False) but
rather just checks the first element?
  Not sure if I have explained this well but if you understand I would
appreciate a reply.
  Thanks.

[[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] ifelse, apply, if

2013-11-27 Thread Andrea Lamont
Hello:

This seems like an obvious question, but I am having trouble answering it.
I am new to R, so I apologize if its too simple to be posting. I have
searched for solutions to no avail.

I have data that I am trying to set up for further analysis ("training
data"). What I need is 12 groups based on patterns of 4 variables. The
complication comes in when missing data is present. Let  me describe with
an example - focusing on just 3 of the 12 groups:

vec=c(1,1,1,1,1,1,NA,NA,1,1,0,0,1,NA,1,1,1,NA,0,0,1,NA,1,0,0,0,0,1,0,0,0,0,NA,NA,NA,NA,1,NA,0,NA,1,NA,1,NA)
> a=matrix(vec, ncol=4,nrow=11, byrow=T)
> edit(a)
  col1 col2 col3 col4
 [1,]1111
 [2,]11   NA   NA
 [3,]1100
 [4,]1   NA11
 [5,]1   NA00
 [6,]1   NA10
 [7,]0001
 [8,]0000
 [9,]   NA   NA   NA   NA
[10,]1   NA   0   NA
[11,]1   NA1   NA

Here are 11 individuals. I want the following groups (coded as three
separate binary variables):
Group1 - scored a 1 on col1 and multiple time
Group2 - scored a 1 on col1 but only once
Group3 - did not score a 1 in col1

This seems straightforward, except missingness complicates it.  Take
individual 5 - this person should be placed in Groups 1 AND 2 because we
don'tknow the score col2.  Same with individual 10, though the response
pattern differs.

I tried using if statments, but am running into the problem that if is not
vecotrized, and I can't seem to make if run with apply. I can use ifelse,
but its very clunky and inefficient to list all possible patterns:

(Note this is not complete of all patterns, its just an example of what
Ivebeen doing)
dd$TEST1=ifelse(is.na(d$C8W1raw),1,
(ifelse(d$C8W1raw==1 & is.na(d$C9W1raw) & is.na(d$C11AW1raw) & is.na
(d$C12AW1rraw),777899,
 (ifelse((d$C8W1raw==1 & d$C9W1raw==1)| (d$C8W1raw==1 & d$C11AW1raw==1)
|(d$C8W1raw==1 & d$C12AW1rraw==1),1,
(ifelse(d$C8W1raw==1 & ((is.na(d$C9W1raw) | d$C9W1raw==0) & (is.na(d$C11AW1raw)
| d$C11AW1raw==0)& (is.na(d$C12AW1rraw)|d$C12AW1rraw==0)),777899,
 0)))

Any ideas on how to approach this efficiently?

Thanks,
Andrea

-- 
Andrea Lamont, MA
Clinical-Community Psychology
University of South Carolina
Barnwell College
Columbia, SC 29208

Please consider the environment before printing this email.

CONFIDENTIAL: This transmission is intended for the use of the
individual(s) or entity to which it is addressed, and may contain
information that is privileged, confidential, and exempt from disclosure
under applicable law. Should the reader of this message not be the intended
recipient(s), you are hereby notified that any dissemination, distribution,
or copying of this communication is strictly prohibited.  If you are not
the intended recipient, please contact the sender by reply email and
destroy/delete all copies of the original message.

[[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] ifelse question (I'm not sure why this is working)...

2013-09-10 Thread William Dunlap
> remainderFunction<-function(x,d)
> {
>ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
> }
> remainderFunction(x=c(23:47),d=3)

The above call returns c(2, 5, 8, 11, 14, 17, 20, 23), the value of (23:47)%%3.
Note that remainderFunction(integer(0), 3) returns logical(0).

The return() calls in the call to ifelse cause the problem.  The one used
for the second argument to ifelse causes remainderFunction to return,
abandoning the evaluation of ifself, as soon as ifelse evaluates its second
argument.

I think that using return this way is bad practice, but have seen it in code 
like
   tryCatch(return(something),
error=function(e)"Error in something")
which is common in support code for RStudio.

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 Jonathan Greenberg
> Sent: Tuesday, September 10, 2013 12:40 PM
> To: r-help
> Subject: [R] ifelse question (I'm not sure why this is working)...
> 
> R-helpers:
> 
> One of my intrepid students came up with a solution to a problem where
> they need to write a function that takes a vector x and a "scalar" d,
> and return the indices of the vector x where x %% d is equal to 0 (x
> is evenly divisible by d).  I thought I had a good handle on the
> potential solutions, but one of my students sent me a function that
> WORKS, but for the life of me I can't figure out WHY.  Here is the
> solution:
> 
> remainderFunction<-function(x,d)
> {
>ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
> }
> remainderFunction(x=c(23:47),d=3)
> 
> I've never seen an ifelse statement used that way, and I was fully
> expecting that to NOT work, or to place the output of which(x%%d==0)
> in each location where the statement x%%d==0 was true.
> 
> Any ideas on deconstructing this?
> 
> --j
> 
> --
> Jonathan A. Greenberg, PhD
> Assistant Professor
> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
> Department of Geography and Geographic Information Science
> University of Illinois at Urbana-Champaign
> 607 South Mathews Avenue, MC 150
> Urbana, IL 61801
> Phone: 217-300-1924
> http://www.geog.illinois.edu/~jgrn/
> AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007
> 
> __
> 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.


[R] ifelse question (I'm not sure why this is working)...

2013-09-10 Thread Jonathan Greenberg
R-helpers:

One of my intrepid students came up with a solution to a problem where
they need to write a function that takes a vector x and a "scalar" d,
and return the indices of the vector x where x %% d is equal to 0 (x
is evenly divisible by d).  I thought I had a good handle on the
potential solutions, but one of my students sent me a function that
WORKS, but for the life of me I can't figure out WHY.  Here is the
solution:

remainderFunction<-function(x,d)
{
   ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
}
remainderFunction(x=c(23:47),d=3)

I've never seen an ifelse statement used that way, and I was fully
expecting that to NOT work, or to place the output of which(x%%d==0)
in each location where the statement x%%d==0 was true.

Any ideas on deconstructing this?

--j

-- 
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
607 South Mathews Avenue, MC 150
Urbana, IL 61801
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007

__
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] ifelse question (I'm not sure why this is working)...

2013-09-10 Thread Jim Lemon

On 09/11/2013 05:40 AM, Jonathan Greenberg wrote:

R-helpers:

One of my intrepid students came up with a solution to a problem where
they need to write a function that takes a vector x and a "scalar" d,
and return the indices of the vector x where x %% d is equal to 0 (x
is evenly divisible by d).  I thought I had a good handle on the
potential solutions, but one of my students sent me a function that
WORKS, but for the life of me I can't figure out WHY.  Here is the
solution:

remainderFunction<-function(x,d)
{
ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
}
remainderFunction(x=c(23:47),d=3)

I've never seen an ifelse statement used that way, and I was fully
expecting that to NOT work, or to place the output of which(x%%d==0)
in each location where the statement x%%d==0 was true.

Any ideas on deconstructing this?

--j


Hi Jonathan,
While this has already been answered, the question was "why does it 
work?". As Bill Dunlap pointed out, it is because the "return" does not 
allow the ifelse to complete. That was not a problem for the student, 
for it did do what was requested. It is just an unnecessary elaboration 
of the code, for:


remainderFunction<-function(x,d) {
 which(x%%d==0)
}

works just as well. I think Bill was pointing out the the order of 
evaluation was important, for:


remainderFunction<-function(x,d) {
 which(return(x%%d==0))
}

doesn't work. The student probably deserves a Rube Goldberg award.

Jim

__
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] ifelse question (I'm not sure why this is working)...

2013-09-10 Thread David Winsemius

On Sep 10, 2013, at 12:40 PM, Jonathan Greenberg wrote:

> R-helpers:
> 
> One of my intrepid students came up with a solution to a problem where
> they need to write a function that takes a vector x and a "scalar" d,
> and return the indices of the vector x where x %% d is equal to 0 (x
> is evenly divisible by d).  I thought I had a good handle on the
> potential solutions, but one of my students sent me a function that
> WORKS, but for the life of me I can't figure out WHY.  Here is the
> solution:
> 
> remainderFunction<-function(x,d)
> {
>   ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
> }
> remainderFunction(x=c(23:47),d=3)
> 
> I've never seen an ifelse statement used that way, and I was fully
> expecting that to NOT work, or to place the output of which(x%%d==0)
> in each location where the statement x%%d==0 was true.

I think it did what you expected (at east your second expectation). Look at:

c(NULL, 1,2,3, NULL, 4,5,6, NULL)

# [1] 1 2 3 4 5 6

Obviously teh ifelse is not needed since this is cleaner code:

remainderFunction<-function(x,d)
{
   which(x%%d==0)
 }
 remainderFunction(x=c(23:47),d=3)
# [1]  2  5  8 11 14 17 20 23


-- 
David.
> 
> Any ideas on deconstructing this?
> 
> --j
> 
> -- 
> Jonathan A. Greenberg, PhD
> Assistant Professor
> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
> Department of Geography and Geographic Information Science
> University of Illinois at Urbana-Champaign
> 607 South Mathews Avenue, MC 150
> Urbana, IL 61801
> Phone: 217-300-1924
> http://www.geog.illinois.edu/~jgrn/
> AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007
> 
> __
> 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.

David Winsemius
Alameda, CA, USA

__
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] ifelse question (I'm not sure why this is working)...

2013-09-10 Thread peter dalgaard

On Sep 10, 2013, at 23:39 , Jim Lemon wrote:

> On 09/11/2013 05:40 AM, Jonathan Greenberg wrote:
>> R-helpers:
>> 
>> One of my intrepid students came up with a solution to a problem where
>> they need to write a function that takes a vector x and a "scalar" d,
>> and return the indices of the vector x where x %% d is equal to 0 (x
>> is evenly divisible by d).  I thought I had a good handle on the
>> potential solutions, but one of my students sent me a function that
>> WORKS, but for the life of me I can't figure out WHY.  Here is the
>> solution:
>> 
>> remainderFunction<-function(x,d)
>> {
>>ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
>> }
>> remainderFunction(x=c(23:47),d=3)
>> 
>> I've never seen an ifelse statement used that way, and I was fully
>> expecting that to NOT work, or to place the output of which(x%%d==0)
>> in each location where the statement x%%d==0 was true.
>> 
>> Any ideas on deconstructing this?
>> 
>> --j
>> 
> Hi Jonathan,
> While this has already been answered, the question was "why does it work?". 
> As Bill Dunlap pointed out, it is because the "return" does not allow the 
> ifelse to complete. That was not a problem for the student, for it did do 
> what was requested. It is just an unnecessary elaboration of the code, for:
> 
> remainderFunction<-function(x,d) {
> which(x%%d==0)
> }
> 
> works just as well. I think Bill was pointing out the the order of evaluation 
> was important, for:
> 
> remainderFunction<-function(x,d) {
> which(return(x%%d==0))
> }
> 
> doesn't work. The student probably deserves a Rube Goldberg award.


It's slightly trickier: the yes= part of an ifelse is not evaluated unless at 
least one element of the test is TRUE, so I think the equivalence is closer to

remainderFunction<-function(x,d)
  if (any(x %% d == 0) ) which(x %% d==0) 

except that ifelse() has the curious feature of returning logical(0) if the 
test vector is logical(0) (evaluating neither of its arguments).

(If you want to be really picky, the above returns NULL _invisibly_ if the 
any() condition fails. Whether or not the student deserves a little credit for 
thinking about that case is up in the air, I think. He/she may have failed to 
grasp the meaning of integer(0).)

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

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.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] ifelse question (I'm not sure why this is working)...

2013-09-10 Thread William Dunlap
Here is the same issue in a simpler form:
   > f <- function(x) log(return(x))
   > f(10)
   [1] 10
f's 'x' is returned before log does anything with it.

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, September 10, 2013 12:59 PM
> To: Jonathan Greenberg; r-help
> Subject: Re: [R] ifelse question (I'm not sure why this is working)...
> 
> > remainderFunction<-function(x,d)
> > {
> >ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
> > }
> > remainderFunction(x=c(23:47),d=3)
> 
> The above call returns c(2, 5, 8, 11, 14, 17, 20, 23), the value of 
> (23:47)%%3.
> Note that remainderFunction(integer(0), 3) returns logical(0).
> 
> The return() calls in the call to ifelse cause the problem.  The one used
> for the second argument to ifelse causes remainderFunction to return,
> abandoning the evaluation of ifself, as soon as ifelse evaluates its second
> argument.
> 
> I think that using return this way is bad practice, but have seen it in code 
> like
>tryCatch(return(something),
> error=function(e)"Error in something")
> which is common in support code for RStudio.
> 
> 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 Jonathan Greenberg
> > Sent: Tuesday, September 10, 2013 12:40 PM
> > To: r-help
> > Subject: [R] ifelse question (I'm not sure why this is working)...
> >
> > R-helpers:
> >
> > One of my intrepid students came up with a solution to a problem where
> > they need to write a function that takes a vector x and a "scalar" d,
> > and return the indices of the vector x where x %% d is equal to 0 (x
> > is evenly divisible by d).  I thought I had a good handle on the
> > potential solutions, but one of my students sent me a function that
> > WORKS, but for the life of me I can't figure out WHY.  Here is the
> > solution:
> >
> > remainderFunction<-function(x,d)
> > {
> >ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL))
> > }
> > remainderFunction(x=c(23:47),d=3)
> >
> > I've never seen an ifelse statement used that way, and I was fully
> > expecting that to NOT work, or to place the output of which(x%%d==0)
> > in each location where the statement x%%d==0 was true.
> >
> > Any ideas on deconstructing this?
> >
> > --j
> >
> > --
> > Jonathan A. Greenberg, PhD
> > Assistant Professor
> > Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
> > Department of Geography and Geographic Information Science
> > University of Illinois at Urbana-Champaign
> > 607 South Mathews Avenue, MC 150
> > Urbana, IL 61801
> > Phone: 217-300-1924
> > http://www.geog.illinois.edu/~jgrn/
> > AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007
> >
> > __
> > 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.

__
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] ifelse() applied on function

2013-08-11 Thread William Dunlap
> ifelse(ChooseFn, lapply, sfLapply)(MyList, function(x) {
> ...
> return())

Use 'if', not 'ifelse', and wrap it in parentheses to get the precendence right
  (if (ChooseFn) lapply else sfLapply)(MyList, function(x){...})

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 Ron Michael
> Sent: Sunday, August 11, 2013 1:18 PM
> To: r-help@r-project.org
> Subject: [R] ifelse() applied on function
> 
> Hi,
> 
> How can I apply ifelse function to chose appropriate function?
> 
> My goal is user will chose lapply() function if "ChooseFn = T" otherwise to 
> chose
> sfLapply() from snowfall package.
> 
> I am basically trying to avoid repeatation to write all internal steps, if 
> user choses lapply
> or sfLapply. For example, I am wondering if there is any posibility to write 
> something like:
> 
> ifelse(ChooseFn, lapply, sfLapply)(MyList, function(x) {
> 
> ...
> ..
> return())
> 
> Really appreciate if someone helps me out.
> 
> __
> 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] ifelse() applied on function

2013-08-11 Thread Steve Lianoglou
Hi,

On Sun, Aug 11, 2013 at 1:18 PM, Ron Michael  wrote:
> Hi,
>
> How can I apply ifelse function to chose appropriate function?
>
> My goal is user will chose lapply() function if "ChooseFn = T" otherwise to 
> chose sfLapply() from snowfall package.
>
> I am basically trying to avoid repeatation to write all internal steps, if 
> user choses lapply or sfLapply. For example, I am wondering if there is any 
> posibility to write something like:
>
> ifelse(ChooseFn, lapply, sfLapply)(MyList, function(x) {
>
> ...
> ..
> return())
>
> Really appreciate if someone helps me out.

How about something like:

loop <- if (ChooseFn) lapply else sfLapply

result <- loop(MyList, function(x) {
  ## ...
})

Should work as long as `sfLapply` has same function signature as lapply.

HTH,

-steve

-- 
Steve Lianoglou
Computational Biologist
Bioinformatics and Computational Biology
Genentech

__
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] ifelse() applied on function

2013-08-11 Thread Ron Michael
Hi,
 
How can I apply ifelse function to chose appropriate function?
 
My goal is user will chose lapply() function if "ChooseFn = T" otherwise to 
chose sfLapply() from snowfall package.
 
I am basically trying to avoid repeatation to write all internal steps, if user 
choses lapply or sfLapply. For example, I am wondering if there is any 
posibility to write something like:
 
ifelse(ChooseFn, lapply, sfLapply)(MyList, function(x) {
 
...
..
return())
 
Really appreciate if someone helps me out.

__
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] Ifelse leading to inconsistent result

2013-06-26 Thread Neville O'Reilly
You are right, I inadvertently deleted part of the script - so I ended up with 
non-mutually exclusive regions. My apologies


Neville O'Reilly, Ph.D 
Associate Director, Financial Statistics & Risk Management, 
Rm 479 Hill Center, Rutgers University, 110 Freylinghuysen Rd., 
Piscataway, NJ 08854. 
848-445-7669 
http://fsrm.rutgers.edu/ 

- Original Message -
From: "David Carlson" 
To: "Neville O'Reilly" , r-help@r-project.org
Sent: Wednesday, June 26, 2013 1:15:06 PM
Subject: RE: [R] Ifelse leading to inconsistent result

I don't see that you have set up mutually exclusive ranges. If we
modify your code so save the maxlogP and minlogP values:

niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
maxlogP <- rep(0, ninter)
minlogP <- rep(0, ninter)
set.seed(2009) # enables reproducibility of result if script run
again"
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP[i] = max(logPrice) # max price over next 100 days
  minlogP[i] = min(logPrice)
  CountLoss[i] <- ifelse (minlogP[i] < log(95), 1, ifelse
(maxlogP[i] > log (100), 0, 1))
  CountProf[i] <- ifelse (maxlogP[i] < log (110),0,1)
}
both <- which(CountLoss+CountProf>1)
length(both)
# 18484
head(both)
# [1]  1  5  9 12 15 25
ifelse (minlogP[1] < log(95), 1, ifelse (maxlogP[1] > log
(100), 0, 1))
# [1] 1
ifelse (maxlogP[1] < log(110),0,1)
# [1] 1
exp(maxlogP[1])
# [1] 1204589
exp(minlogP[1])
# [1] 932747.5

Your first simulation meets both criteria along 18483 others!

-
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Neville O'Reilly
Sent: Wednesday, June 26, 2013 10:41 AM
To: r-help@r-project.org
Subject: [R] Ifelse leading to inconsistent result

I have used ifelse in count variables to count the number of times
in a simulation the values of a vector of logprice fall within
mutually exclusive ranges. However, there is a double count in the
result i.e. i am getting output indicating values falling in
mutually exclusive ranges. Here is the code and result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run
again"
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP = max(logPrice) # max price over next 100 days
  minlogP = min(logPrice)
  CountLoss[i] <- ifelse (minlogP < log(95), 1, ifelse (maxlogP
> log (100), 0, 1))
  CountProf[i] <- ifelse (maxlogP < log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold
for a loss in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold
for a profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
> mean(CountLoss) # fraction of times out of niter that stock is
sold for a loss in a 100 day period
[1] 0.64246
> sum(CountProf)
[1] 51857
> mean(CountProf) # fraction of times out of niter that stock is
sold for a profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of
interations. When I troubleshoot by reducing the number of
iterations and that size of the logprice, I can't reproduce the
contradicion.

__
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] Ifelse leading to inconsistent result

2013-06-26 Thread David Carlson
I don't see that you have set up mutually exclusive ranges. If we
modify your code so save the maxlogP and minlogP values:

niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
maxlogP <- rep(0, ninter)
minlogP <- rep(0, ninter)
set.seed(2009) # enables reproducibility of result if script run
again"
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP[i] = max(logPrice) # max price over next 100 days
  minlogP[i] = min(logPrice)
  CountLoss[i] <- ifelse (minlogP[i] < log(95), 1, ifelse
(maxlogP[i] > log (100), 0, 1))
  CountProf[i] <- ifelse (maxlogP[i] < log (110),0,1)
}
both <- which(CountLoss+CountProf>1)
length(both)
# 18484
head(both)
# [1]  1  5  9 12 15 25
ifelse (minlogP[1] < log(95), 1, ifelse (maxlogP[1] > log
(100), 0, 1))
# [1] 1
ifelse (maxlogP[1] < log(110),0,1)
# [1] 1
exp(maxlogP[1])
# [1] 1204589
exp(minlogP[1])
# [1] 932747.5

Your first simulation meets both criteria along 18483 others!

-
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Neville O'Reilly
Sent: Wednesday, June 26, 2013 10:41 AM
To: r-help@r-project.org
Subject: [R] Ifelse leading to inconsistent result

I have used ifelse in count variables to count the number of times
in a simulation the values of a vector of logprice fall within
mutually exclusive ranges. However, there is a double count in the
result i.e. i am getting output indicating values falling in
mutually exclusive ranges. Here is the code and result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run
again"
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP = max(logPrice) # max price over next 100 days
  minlogP = min(logPrice)
  CountLoss[i] <- ifelse (minlogP < log(95), 1, ifelse (maxlogP
> log (100), 0, 1))
  CountProf[i] <- ifelse (maxlogP < log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold
for a loss in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold
for a profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
> mean(CountLoss) # fraction of times out of niter that stock is
sold for a loss in a 100 day period
[1] 0.64246
> sum(CountProf)
[1] 51857
> mean(CountProf) # fraction of times out of niter that stock is
sold for a profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of
interations. When I troubleshoot by reducing the number of
iterations and that size of the logprice, I can't reproduce the
contradicion.

__
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] Ifelse leading to inconsistent result

2013-06-26 Thread peter dalgaard

On Jun 26, 2013, at 17:40 , Neville O'Reilly wrote:

> I have used ifelse in count variables to count the number of times in a 
> simulation the values of a vector of logprice fall within mutually exclusive 
> ranges. However, there is a double count in the result i.e. i am getting 
> output indicating values falling in mutually exclusive ranges. Here is the 
> code and result
> R script

Don't use ifelse, it just confuses the logic.

As far as I can tell, the code is equivalent (except for integer conversion) to

 CountLoss[i] <- (minlogP < log(95)) | (maxlogP <= log (100))
 CountProf[i] <- (maxlogP >= log (110))

and that doesn't look mutually exclusive to me. 

> niter = 1e5 # number of iterations is 10^5
> CountLoss = rep(0,niter)
> CountProf = rep (0,niter)
> set.seed(2009) # enables reproducibility of result if script run again"
> for (i in 1:niter)
> {
>  r = rnorm(100,mean=.05/253,
>sd=.23/sqrt(253)) # generate 100 random normal numbers
>  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
>  maxlogP = max(logPrice) # max price over next 100 days
>  minlogP = min(logPrice)
>  CountLoss[i] <- ifelse (minlogP < log(95), 1, ifelse (maxlogP > log 
> (100), 0, 1))
>  CountProf[i] <- ifelse (maxlogP < log (110),0,1)
> }
> sum(CountLoss)
> mean(CountLoss) # fraction of times out of niter that stock is sold for a 
> loss in a 100 day period
> sum(CountProf)
> mean(CountProf) # fraction of times out of niter that stock is sold for a 
> profit in a 100 day period
> 
> Output
> sum(CountLoss)
> [1] 64246
>> mean(CountLoss) # fraction of times out of niter that stock is sold for a 
>> loss in a 100 day period
> [1] 0.64246
>> sum(CountProf)
> [1] 51857
>> mean(CountProf) # fraction of times out of niter that stock is sold for a 
>> profit in a 100 day period
> [1] 0.51857
> 
> CountLoss and CountProf should sum to less than the number of interations. 
> When I troubleshoot by reducing the number of iterations and that size of the 
> logprice, I can't reproduce the contradicion.
> 
> __
> 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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.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] Ifelse leading to inconsistent result

2013-06-26 Thread Duncan Murdoch

On 26/06/2013 11:40 AM, Neville O'Reilly wrote:

I have used ifelse in count variables to count the number of times in a 
simulation the values of a vector of logprice fall within mutually exclusive 
ranges. However, there is a double count in the result i.e. i am getting output 
indicating values falling in mutually exclusive ranges. Here is the code and 
result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run again"
for (i in 1:niter)
{
   r = rnorm(100,mean=.05/253,
 sd=.23/sqrt(253)) # generate 100 random normal numbers
   logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
   maxlogP = max(logPrice) # max price over next 100 days
   minlogP = min(logPrice)
   CountLoss[i] <- ifelse (minlogP < log(95), 1, ifelse (maxlogP > log 
(100), 0, 1))
   CountProf[i] <- ifelse (maxlogP < log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold for a loss 
in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold for a 
profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
> mean(CountLoss) # fraction of times out of niter that stock is sold for a 
loss in a 100 day period
[1] 0.64246
> sum(CountProf)
[1] 51857
> mean(CountProf) # fraction of times out of niter that stock is sold for a 
profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of interations. When 
I troubleshoot by reducing the number of iterations and that size of the 
logprice, I can't reproduce the contradicion.


I don't see a contradiction.  Both CountLoss and CountProf are less than 
niter.  The logic of your test doesn't imply that sum(CountLoss) + 
sum(CountProf) should be less than niter; e.g. a case where minlogP is 
less than log(95) and maxlogP > log(110) would be counted in both.


Duncan Murdoch

__
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] Ifelse leading to inconsistent result

2013-06-26 Thread Neville O'Reilly
I have used ifelse in count variables to count the number of times in a 
simulation the values of a vector of logprice fall within mutually exclusive 
ranges. However, there is a double count in the result i.e. i am getting output 
indicating values falling in mutually exclusive ranges. Here is the code and 
result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run again"
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP = max(logPrice) # max price over next 100 days
  minlogP = min(logPrice)
  CountLoss[i] <- ifelse (minlogP < log(95), 1, ifelse (maxlogP > log 
(100), 0, 1))
  CountProf[i] <- ifelse (maxlogP < log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold for a loss 
in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold for a 
profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
> mean(CountLoss) # fraction of times out of niter that stock is sold for a 
> loss in a 100 day period
[1] 0.64246
> sum(CountProf)
[1] 51857
> mean(CountProf) # fraction of times out of niter that stock is sold for a 
> profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of interations. When 
I troubleshoot by reducing the number of iterations and that size of the 
logprice, I can't reproduce the contradicion.

__
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] ifelse to speed up loop?

2013-01-24 Thread arun
Hi,
If variable `V1` is factor and it follows the same pattern as in your example:
test<- structure(list(V1 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L), .Label = c("a", "b"), class = "factor")), .Names = "V1", class 
= "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))

test$group<-cumsum(c(1,abs(diff(as.integer(test[,1]) #works

#But, if it is like this:
test1<- structure(list(V1 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 3L, 3L, 3L), .Label = c("a", "b", "d"), class = "factor")), .Names = "V1", 
class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))
 test1$group<-cumsum(c(1,abs(diff(as.integer(test1[,1]) #may not work well 
(depends on your expected result)
 v<- test1[,1] #Bill's solution
test1$group2<-cumsum(c(TRUE, v[-length(v)] != v[-1] ))# works
 test1
#  V1 group group2
#1   a 1      1
#2   a 1  1
#3   a 1  1
#4   b 2  2
#5   b 2  2
#6   b 2  2
#7   a 3  3
#8   a 3  3
#9   a 3  3
#10  d 5  4
#11  d 5  4
#12  d 5  4

A.K.







- Original Message -
From: Jeff 
To: arun 
Cc: Jeffrey Fuerte ; R help 
Sent: Thursday, January 24, 2013 7:08 PM
Subject: Re: [R] ifelse to speed up loop?

Thank you for the tips. I should mention that the V1 is numeric in this case 
but I am trying to automate the overall procedure so it may not be numeric 
always. Would that matter with these functions?

Sent from my iPhone

On Jan 24, 2013, at 6:34 PM, arun  wrote:

> Hi,
> You could do this:
> test<-read.table(text="
>   V1
> 1  1
> 2  1
> 3  1
> 4  2
> 5  2
> 6  2
> 7  1
> 8  1
> 9  1
> 10  2
> 11  2
> 12  2
> ",sep="",header=T)
> 
>  test$Group<-cumsum(abs(c(0,diff(test[,1]+1
> test
> #   V1 Group
> #1   1     1
> #2   1     1
> #3   1     1
> #4   2     2
> #5   2     2
> #6   2     2
> #7   1     3
> #   1     3
> #9   1     3
> #10  2     4
> #11  2     4
> #12  2     4
> A.K.
> 
> 
> 
> - Original Message -
> From: Jeffrey Fuerte 
> To: r-help@r-project.org
> Cc: 
> Sent: Thursday, January 24, 2013 4:20 PM
> Subject: [R] ifelse to speed up loop?
> 
> Hello,
> 
> I'm not sure how to explain what I'm looking for but essentially I have a
> test dataset that looks like this:
> 
> test:
>    V1
> 1   1
> 2   1
> 3   1
> 4   2
> 5   2
> 6   2
> 7   1
> 8   1
> 9   1
> 10  2
> 11  2
> 12  2
> 
> And what I want to be able to do is create another column that captures a
> "grouping" variable that looks like this:
> 
> test
>    V1 group
> 1   1  1
> 2   1  1
> 3   1  1
> 4   2  2
> 5   2  2
> 6   2  2
> 7   1 3
> 8   1  3
> 9   1  3
> 10  2  4
> 11  2  4
> 12  2  4
> 
> So, it's keeping track of the changes in V1, but even though V1 could be
> the same in different instances, the group is treating it as a new group.
> I have written a loop that does what I want, but this takes too long to
> run, so I was hoping for either a faster approach or an ifelse statement.
> Any ideas?
> 
> By the loop I was using looks like this:
> 
> groupings <- 1
> test$group[1] <- groupings
> for (i in 2:length(test$V1))
> {
> if (test$V1[i]==test$V1[i-1])
> {
> test$group[i] <- groupings
> } else {
> groupings <- groupings+1
> test$group[i] <- groupings
> }
> }
> 
> Thanks for the help.
> 
>     [[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] ifelse to speed up loop?

2013-01-24 Thread William Dunlap
rle() or c(TRUE, cumsum( v[-length(v)] != v[-1] )) will work on any
type for which != is defined.  The ones involving diff() require numeric inputs.

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 Jeff
> Sent: Thursday, January 24, 2013 4:08 PM
> To: arun
> Cc: R help; Jeffrey Fuerte
> Subject: Re: [R] ifelse to speed up loop?
> 
> Thank you for the tips. I should mention that the V1 is numeric in this case 
> but I am trying
> to automate the overall procedure so it may not be numeric always. Would that 
> matter
> with these functions?
> 
> Sent from my iPhone
> 
> On Jan 24, 2013, at 6:34 PM, arun  wrote:
> 
> > Hi,
> > You could do this:
> > test<-read.table(text="
> >   V1
> > 1  1
> > 2  1
> > 3  1
> > 4  2
> > 5  2
> > 6  2
> > 7  1
> > 8  1
> > 9  1
> > 10  2
> > 11  2
> > 12  2
> > ",sep="",header=T)
> >
> >  test$Group<-cumsum(abs(c(0,diff(test[,1]+1
> > test
> > #   V1 Group
> > #1   1 1
> > #2   1 1
> > #3   1 1
> > #4   2 2
> > #5   2 2
> > #6   2 2
> > #7   1 3
> > #   1     3
> > #9   1 3
> > #10  2 4
> > #11  2 4
> > #12  2 4
> > A.K.
> >
> >
> >
> > - Original Message -
> > From: Jeffrey Fuerte 
> > To: r-help@r-project.org
> > Cc:
> > Sent: Thursday, January 24, 2013 4:20 PM
> > Subject: [R] ifelse to speed up loop?
> >
> > Hello,
> >
> > I'm not sure how to explain what I'm looking for but essentially I have a
> > test dataset that looks like this:
> >
> > test:
> >V1
> > 1   1
> > 2   1
> > 3   1
> > 4   2
> > 5   2
> > 6   2
> > 7   1
> > 8   1
> > 9   1
> > 10  2
> > 11  2
> > 12  2
> >
> > And what I want to be able to do is create another column that captures a
> > "grouping" variable that looks like this:
> >
> > test
> >V1 group
> > 1   1  1
> > 2   1  1
> > 3   1  1
> > 4   2  2
> > 5   2  2
> > 6   2  2
> > 7   1 3
> > 8   1  3
> > 9   1  3
> > 10  2  4
> > 11  2  4
> > 12  2  4
> >
> > So, it's keeping track of the changes in V1, but even though V1 could be
> > the same in different instances, the group is treating it as a new group.
> > I have written a loop that does what I want, but this takes too long to
> > run, so I was hoping for either a faster approach or an ifelse statement.
> > Any ideas?
> >
> > By the loop I was using looks like this:
> >
> > groupings <- 1
> > test$group[1] <- groupings
> > for (i in 2:length(test$V1))
> > {
> > if (test$V1[i]==test$V1[i-1])
> > {
> > test$group[i] <- groupings
> > } else {
> > groupings <- groupings+1
> > test$group[i] <- groupings
> > }
> > }
> >
> > Thanks for the help.
> >
> > [[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.

__
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] ifelse to speed up loop?

2013-01-24 Thread Jeff
Thank you for the tips. I should mention that the V1 is numeric in this case 
but I am trying to automate the overall procedure so it may not be numeric 
always. Would that matter with these functions?

Sent from my iPhone

On Jan 24, 2013, at 6:34 PM, arun  wrote:

> Hi,
> You could do this:
> test<-read.table(text="
>   V1
> 1  1
> 2  1
> 3  1
> 4  2
> 5  2
> 6  2
> 7  1
> 8  1
> 9  1
> 10  2
> 11  2
> 12  2
> ",sep="",header=T)
> 
>  test$Group<-cumsum(abs(c(0,diff(test[,1]+1
> test
> #   V1 Group
> #1   1 1
> #2   1 1
> #3   1 1
> #4   2 2
> #5   2 2
> #6   2 2
> #7   1 3
> #   1 3
> #9   1 3
> #10  2 4
> #11  2 4
> #12  2 4
> A.K.
> 
> 
> 
> ----- Original Message -
> From: Jeffrey Fuerte 
> To: r-help@r-project.org
> Cc: 
> Sent: Thursday, January 24, 2013 4:20 PM
> Subject: [R] ifelse to speed up loop?
> 
> Hello,
> 
> I'm not sure how to explain what I'm looking for but essentially I have a
> test dataset that looks like this:
> 
> test:
>V1
> 1   1
> 2   1
> 3   1
> 4   2
> 5   2
> 6   2
> 7   1
> 8   1
> 9   1
> 10  2
> 11  2
> 12  2
> 
> And what I want to be able to do is create another column that captures a
> "grouping" variable that looks like this:
> 
> test
>V1 group
> 1   1  1
> 2   1  1
> 3   1  1
> 4   2  2
> 5   2  2
> 6   2  2
> 7   1 3
> 8   1  3
> 9   1  3
> 10  2  4
> 11  2  4
> 12  2  4
> 
> So, it's keeping track of the changes in V1, but even though V1 could be
> the same in different instances, the group is treating it as a new group.
> I have written a loop that does what I want, but this takes too long to
> run, so I was hoping for either a faster approach or an ifelse statement.
> Any ideas?
> 
> By the loop I was using looks like this:
> 
> groupings <- 1
> test$group[1] <- groupings
> for (i in 2:length(test$V1))
> {
> if (test$V1[i]==test$V1[i-1])
> {
> test$group[i] <- groupings
> } else {
> groupings <- groupings+1
> test$group[i] <- groupings
> }
> }
> 
> Thanks for the help.
> 
> [[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] ifelse to speed up loop?

2013-01-24 Thread arun
Hi,

I guess you meant:
c(TRUE, cumsum( v[-length(v)] != v[-1] ))
 [1] 1 0 0 1 1 1 2 2 2 3 3 3
this:
 cumsum(c(TRUE, v[-length(v)] != v[-1] ))
# [1] 1 1 1 2 2 2 3 3 3 4 4 4
A.K.




- Original Message -
From: William Dunlap 
To: arun ; Jeffrey Fuerte 
Cc: R help 
Sent: Thursday, January 24, 2013 6:48 PM
Subject: RE: [R] ifelse to speed up loop?

I like
   v <- test[,1]
   c(TRUE, cumsum( v[-length(v)] != v[-1] ))
(R's arithmetic on logicals treats TRUE as 1 and FALSE as 0.)

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 arun
> Sent: Thursday, January 24, 2013 3:37 PM
> To: Jeffrey Fuerte
> Cc: R help
> Subject: Re: [R] ifelse to speed up loop?
> 
> HI,
> 
> This might be better.
> test$group<-cumsum(abs(c(1,diff(test[,1]
> A.K.
> 
> 
> 
> 
> - Original Message -
> From: Jeffrey Fuerte 
> To: r-help@r-project.org
> Cc:
> Sent: Thursday, January 24, 2013 4:20 PM
> Subject: [R] ifelse to speed up loop?
> 
> Hello,
> 
> I'm not sure how to explain what I'm looking for but essentially I have a
> test dataset that looks like this:
> 
> test:
>    V1
> 1   1
> 2   1
> 3   1
> 4   2
> 5   2
> 6   2
> 7   1
> 8   1
> 9   1
> 10  2
> 11  2
> 12  2
> 
> And what I want to be able to do is create another column that captures a
> "grouping" variable that looks like this:
> 
> test
>    V1 group
> 1   1  1
> 2   1  1
> 3   1  1
> 4   2  2
> 5   2  2
> 6   2  2
> 7   1 3
> 8   1  3
> 9   1  3
> 10  2  4
> 11  2  4
> 12  2  4
> 
> So, it's keeping track of the changes in V1, but even though V1 could be
> the same in different instances, the group is treating it as a new group.
> I have written a loop that does what I want, but this takes too long to
> run, so I was hoping for either a faster approach or an ifelse statement.
> Any ideas?
> 
> By the loop I was using looks like this:
> 
> groupings <- 1
> test$group[1] <- groupings
> for (i in 2:length(test$V1))
> {
> if (test$V1[i]==test$V1[i-1])
> {
> test$group[i] <- groupings
> } else {
> groupings <- groupings+1
> test$group[i] <- groupings
> }
> }
> 
> Thanks for the help.
> 
>     [[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.


__
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] ifelse to speed up loop?

2013-01-24 Thread William Dunlap
Yes.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: arun [mailto:smartpink...@yahoo.com]
> Sent: Thursday, January 24, 2013 3:53 PM
> To: William Dunlap
> Cc: R help
> Subject: Re: [R] ifelse to speed up loop?
> 
> Hi,
> 
> I guess you meant:
> c(TRUE, cumsum( v[-length(v)] != v[-1] ))
>  [1] 1 0 0 1 1 1 2 2 2 3 3 3
> this:
>  cumsum(c(TRUE, v[-length(v)] != v[-1] ))
> # [1] 1 1 1 2 2 2 3 3 3 4 4 4
> A.K.
> 
> 
> 
> 
> - Original Message -
> From: William Dunlap 
> To: arun ; Jeffrey Fuerte 
> Cc: R help 
> Sent: Thursday, January 24, 2013 6:48 PM
> Subject: RE: [R] ifelse to speed up loop?
> 
> I like
>    v <- test[,1]
>    c(TRUE, cumsum( v[-length(v)] != v[-1] ))
> (R's arithmetic on logicals treats TRUE as 1 and FALSE as 0.)
> 
> 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 arun
> > Sent: Thursday, January 24, 2013 3:37 PM
> > To: Jeffrey Fuerte
> > Cc: R help
> > Subject: Re: [R] ifelse to speed up loop?
> >
> > HI,
> >
> > This might be better.
> > test$group<-cumsum(abs(c(1,diff(test[,1]
> > A.K.
> >
> >
> >
> >
> > - Original Message -
> > From: Jeffrey Fuerte 
> > To: r-help@r-project.org
> > Cc:
> > Sent: Thursday, January 24, 2013 4:20 PM
> > Subject: [R] ifelse to speed up loop?
> >
> > Hello,
> >
> > I'm not sure how to explain what I'm looking for but essentially I have a
> > test dataset that looks like this:
> >
> > test:
> >    V1
> > 1   1
> > 2   1
> > 3   1
> > 4   2
> > 5   2
> > 6   2
> > 7   1
> > 8   1
> > 9   1
> > 10  2
> > 11  2
> > 12  2
> >
> > And what I want to be able to do is create another column that captures a
> > "grouping" variable that looks like this:
> >
> > test
> >    V1 group
> > 1   1  1
> > 2   1  1
> > 3   1  1
> > 4   2  2
> > 5   2  2
> > 6   2  2
> > 7   1 3
> > 8   1  3
> > 9   1  3
> > 10  2  4
> > 11  2  4
> > 12  2  4
> >
> > So, it's keeping track of the changes in V1, but even though V1 could be
> > the same in different instances, the group is treating it as a new group.
> > I have written a loop that does what I want, but this takes too long to
> > run, so I was hoping for either a faster approach or an ifelse statement.
> > Any ideas?
> >
> > By the loop I was using looks like this:
> >
> > groupings <- 1
> > test$group[1] <- groupings
> > for (i in 2:length(test$V1))
> > {
> > if (test$V1[i]==test$V1[i-1])
> > {
> > test$group[i] <- groupings
> > } else {
> > groupings <- groupings+1
> > test$group[i] <- groupings
> > }
> > }
> >
> > Thanks for the help.
> >
> >     [[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.

__
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] ifelse to speed up loop?

2013-01-24 Thread William Dunlap
I like
   v <- test[,1]
   c(TRUE, cumsum( v[-length(v)] != v[-1] ))
(R's arithmetic on logicals treats TRUE as 1 and FALSE as 0.)

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 arun
> Sent: Thursday, January 24, 2013 3:37 PM
> To: Jeffrey Fuerte
> Cc: R help
> Subject: Re: [R] ifelse to speed up loop?
> 
> HI,
> 
> This might be better.
> test$group<-cumsum(abs(c(1,diff(test[,1]
> A.K.
> 
> 
> 
> 
> - Original Message -
> From: Jeffrey Fuerte 
> To: r-help@r-project.org
> Cc:
> Sent: Thursday, January 24, 2013 4:20 PM
> Subject: [R] ifelse to speed up loop?
> 
> Hello,
> 
> I'm not sure how to explain what I'm looking for but essentially I have a
> test dataset that looks like this:
> 
> test:
>    V1
> 1   1
> 2   1
> 3   1
> 4   2
> 5   2
> 6   2
> 7   1
> 8   1
> 9   1
> 10  2
> 11  2
> 12  2
> 
> And what I want to be able to do is create another column that captures a
> "grouping" variable that looks like this:
> 
> test
>    V1 group
> 1   1  1
> 2   1  1
> 3   1  1
> 4   2  2
> 5   2  2
> 6   2  2
> 7   1 3
> 8   1  3
> 9   1  3
> 10  2  4
> 11  2  4
> 12  2  4
> 
> So, it's keeping track of the changes in V1, but even though V1 could be
> the same in different instances, the group is treating it as a new group.
> I have written a loop that does what I want, but this takes too long to
> run, so I was hoping for either a faster approach or an ifelse statement.
> Any ideas?
> 
> By the loop I was using looks like this:
> 
> groupings <- 1
> test$group[1] <- groupings
> for (i in 2:length(test$V1))
> {
> if (test$V1[i]==test$V1[i-1])
> {
> test$group[i] <- groupings
> } else {
> groupings <- groupings+1
> test$group[i] <- groupings
> }
> }
> 
> Thanks for the help.
> 
>     [[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.

__
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] ifelse to speed up loop?

2013-01-24 Thread arun
HI,

This might be better.
test$group<-cumsum(abs(c(1,diff(test[,1]
A.K.




- Original Message -
From: Jeffrey Fuerte 
To: r-help@r-project.org
Cc: 
Sent: Thursday, January 24, 2013 4:20 PM
Subject: [R] ifelse to speed up loop?

Hello,

I'm not sure how to explain what I'm looking for but essentially I have a
test dataset that looks like this:

test:
   V1
1   1
2   1
3   1
4   2
5   2
6   2
7   1
8   1
9   1
10  2
11  2
12  2

And what I want to be able to do is create another column that captures a
"grouping" variable that looks like this:

test
   V1 group
1   1  1
2   1  1
3   1  1
4   2  2
5   2  2
6   2  2
7   1 3
8   1  3
9   1  3
10  2  4
11  2  4
12  2  4

So, it's keeping track of the changes in V1, but even though V1 could be
the same in different instances, the group is treating it as a new group.
I have written a loop that does what I want, but this takes too long to
run, so I was hoping for either a faster approach or an ifelse statement.
Any ideas?

By the loop I was using looks like this:

groupings <- 1
test$group[1] <- groupings
for (i in 2:length(test$V1))
{
if (test$V1[i]==test$V1[i-1])
{
test$group[i] <- groupings
} else {
groupings <- groupings+1
test$group[i] <- groupings
}
}

Thanks for the help.

    [[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] ifelse to speed up loop?

2013-01-24 Thread Bert Gunter
I should add that if your V1 column is numeric, fiddling around with
?diff or equivalent will also identify the groupings for you and may
be faster than rle().

-- Bert

On Thu, Jan 24, 2013 at 3:32 PM, Bert Gunter  wrote:
> Your query is a little unclear to me, but I suspect
> ?rle
> is what you want.
>
> -- Bert
>
> On Thu, Jan 24, 2013 at 1:20 PM, Jeffrey Fuerte  wrote:
>> Hello,
>>
>> I'm not sure how to explain what I'm looking for but essentially I have a
>> test dataset that looks like this:
>>
>>  test:
>>V1
>> 1   1
>> 2   1
>> 3   1
>> 4   2
>> 5   2
>> 6   2
>> 7   1
>> 8   1
>> 9   1
>> 10  2
>> 11  2
>> 12  2
>>
>> And what I want to be able to do is create another column that captures a
>> "grouping" variable that looks like this:
>>
>>  test
>>V1 group
>> 1   1  1
>> 2   1  1
>> 3   1  1
>> 4   2  2
>> 5   2  2
>> 6   2  2
>> 7   1 3
>> 8   1  3
>> 9   1  3
>> 10  2  4
>> 11  2  4
>> 12  2  4
>>
>> So, it's keeping track of the changes in V1, but even though V1 could be
>> the same in different instances, the group is treating it as a new group.
>>  I have written a loop that does what I want, but this takes too long to
>> run, so I was hoping for either a faster approach or an ifelse statement.
>>  Any ideas?
>>
>> By the loop I was using looks like this:
>>
>> groupings <- 1
>> test$group[1] <- groupings
>> for (i in 2:length(test$V1))
>> {
>> if (test$V1[i]==test$V1[i-1])
>> {
>> test$group[i] <- groupings
>> } else {
>> groupings <- groupings+1
>> test$group[i] <- groupings
>> }
>> }
>>
>> Thanks for the help.
>>
>> [[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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] ifelse to speed up loop?

2013-01-24 Thread arun
Hi,
You could do this:
test<-read.table(text="
  V1
1  1
2  1
3  1
4  2
5  2
6  2
7  1
8  1
9  1
10  2
11  2
12  2
",sep="",header=T)

 test$Group<-cumsum(abs(c(0,diff(test[,1]+1
test
#   V1 Group
#1   1 1
#2   1 1
#3   1 1
#4   2 2
#5   2 2
#6   2 2
#7   1 3
#   1 3
#9   1 3
#10  2 4
#11  2 4
#12  2 4
A.K.



- Original Message -
From: Jeffrey Fuerte 
To: r-help@r-project.org
Cc: 
Sent: Thursday, January 24, 2013 4:20 PM
Subject: [R] ifelse to speed up loop?

Hello,

I'm not sure how to explain what I'm looking for but essentially I have a
test dataset that looks like this:

test:
   V1
1   1
2   1
3   1
4   2
5   2
6   2
7   1
8   1
9   1
10  2
11  2
12  2

And what I want to be able to do is create another column that captures a
"grouping" variable that looks like this:

test
   V1 group
1   1  1
2   1  1
3   1  1
4   2  2
5   2  2
6   2  2
7   1 3
8   1  3
9   1  3
10  2  4
11  2  4
12  2  4

So, it's keeping track of the changes in V1, but even though V1 could be
the same in different instances, the group is treating it as a new group.
I have written a loop that does what I want, but this takes too long to
run, so I was hoping for either a faster approach or an ifelse statement.
Any ideas?

By the loop I was using looks like this:

groupings <- 1
test$group[1] <- groupings
for (i in 2:length(test$V1))
{
if (test$V1[i]==test$V1[i-1])
{
test$group[i] <- groupings
} else {
groupings <- groupings+1
test$group[i] <- groupings
}
}

Thanks for the help.

    [[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] ifelse to speed up loop?

2013-01-24 Thread Bert Gunter
Your query is a little unclear to me, but I suspect
?rle
is what you want.

-- Bert

On Thu, Jan 24, 2013 at 1:20 PM, Jeffrey Fuerte  wrote:
> Hello,
>
> I'm not sure how to explain what I'm looking for but essentially I have a
> test dataset that looks like this:
>
>  test:
>V1
> 1   1
> 2   1
> 3   1
> 4   2
> 5   2
> 6   2
> 7   1
> 8   1
> 9   1
> 10  2
> 11  2
> 12  2
>
> And what I want to be able to do is create another column that captures a
> "grouping" variable that looks like this:
>
>  test
>V1 group
> 1   1  1
> 2   1  1
> 3   1  1
> 4   2  2
> 5   2  2
> 6   2  2
> 7   1 3
> 8   1  3
> 9   1  3
> 10  2  4
> 11  2  4
> 12  2  4
>
> So, it's keeping track of the changes in V1, but even though V1 could be
> the same in different instances, the group is treating it as a new group.
>  I have written a loop that does what I want, but this takes too long to
> run, so I was hoping for either a faster approach or an ifelse statement.
>  Any ideas?
>
> By the loop I was using looks like this:
>
> groupings <- 1
> test$group[1] <- groupings
> for (i in 2:length(test$V1))
> {
> if (test$V1[i]==test$V1[i-1])
> {
> test$group[i] <- groupings
> } else {
> groupings <- groupings+1
> test$group[i] <- groupings
> }
> }
>
> Thanks for the help.
>
> [[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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] ifelse to speed up loop?

2013-01-24 Thread Jeffrey Fuerte
Hello,

I'm not sure how to explain what I'm looking for but essentially I have a
test dataset that looks like this:

 test:
   V1
1   1
2   1
3   1
4   2
5   2
6   2
7   1
8   1
9   1
10  2
11  2
12  2

And what I want to be able to do is create another column that captures a
"grouping" variable that looks like this:

 test
   V1 group
1   1  1
2   1  1
3   1  1
4   2  2
5   2  2
6   2  2
7   1 3
8   1  3
9   1  3
10  2  4
11  2  4
12  2  4

So, it's keeping track of the changes in V1, but even though V1 could be
the same in different instances, the group is treating it as a new group.
 I have written a loop that does what I want, but this takes too long to
run, so I was hoping for either a faster approach or an ifelse statement.
 Any ideas?

By the loop I was using looks like this:

groupings <- 1
test$group[1] <- groupings
for (i in 2:length(test$V1))
{
if (test$V1[i]==test$V1[i-1])
{
test$group[i] <- groupings
} else {
groupings <- groupings+1
test$group[i] <- groupings
}
}

Thanks for the help.

[[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] ifelse + numeric

2012-11-22 Thread djbanana
Thanks, it worked without quotation marks.



--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-numeric-tp4650390p4650482.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.


Re: [R] ifelse + numeric

2012-11-21 Thread jim holtman
Try leaving the quote marks off the NA:

data[,4] <- ifelse(data[,3]<1,data[,1]/(1-data[,3]),NA)

You are putting a character value with the "NA" which converts
everything else to a character.  If you had examined the dataframe
with 'str(data)' you would probably have seen the problem.

On Wed, Nov 21, 2012 at 5:34 PM, djbanana  wrote:
> Hi,
>
> I have a data frame and I need to add another column.
>
> I am using: data[,4] <- ifelse(data[,3]<1,data[,1]/(1-data[,3]),"NA")
>
> This is returning values but are in quotation marks. The error I am getting
> is "non-numeric argument to binary operator"
>
> I need column four in data to be numeric. Tried as.numeric but it did not
> work well either.
>
> Would appreciate any help.
>
> Thanks.
>
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/ifelse-numeric-tp4650390.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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

__
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] ifelse + numeric

2012-11-21 Thread djbanana
Hi,

I have a data frame and I need to add another column.

I am using: data[,4] <- ifelse(data[,3]<1,data[,1]/(1-data[,3]),"NA")

This is returning values but are in quotation marks. The error I am getting
is "non-numeric argument to binary operator"

I need column four in data to be numeric. Tried as.numeric but it did not
work well either.

Would appreciate any help.

Thanks.



--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-numeric-tp4650390.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.


Re: [R] ifelse reformulation

2012-10-25 Thread arun
Hi,

Did you mean this?
group1<-c(40,50,"60",70)
#or
group2<-c(50,"var1","var2",60)

In either of the above cases, when you check
str(group1) # all are converted to character.
# chr [1:4] "40" "50" "60" "70"

 str(group2)
# chr [1:4] "50" "var1" "var2" "60"

Suppose, I am comparing the test1 dataset (x4: x6 columns) with group2
 apply(test1,1,function(x) ifelse(x[5:7]%in%group2,0,1)) # 2nd row is 50 for x4 
column
   #  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#[1,] 1 0 1 1 1 1 1 1 1  1  1  1  1  1  1
#[2,] 1 1 1 1 1 1 1 1 1  1  1  1  1  1  1
#[3,] 1 1 1 1 1 1 1 1 1  1  1  1  1  1  1

# final result
as.vector(apply(test1,1,function(x) ifelse(any(x[5:7]%in%group2),0,1)))
# [1] 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1

I guess it works.

BTW, in my previous reply, I made a mistake
as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]%in%group),0,1)))  
   ^^^ 
It should be 5:7.
group<-c(40,50,60,70)
as.vector(apply(test1,1,function(x) ifelse(any(x[5:7]%in%group),0,1)))
 #[1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1

ifelse(test1$x4==40|test1$x4==50|test1$x4==60|test1$x4==70|test1$x5==40|test1$x5==50|test1$x5==60|test1$x5==70|test1$x6==40|test1$x6==50|test1$x6==60|test1$x7==70,0,1)
# [1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1

 as.vector(apply(test1,1,function(x) 
ifelse(any(x[5:7]==40|x[5:7]==50|x[5:7]==60|x[5:7]==70),0,1)))
 #[1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1


A.K.


















- Original Message -
From: brunosm 
To: r-help@r-project.org
Cc: 
Sent: Thursday, October 25, 2012 12:55 PM
Subject: Re: [R] ifelse reformulation

Arun, thank you very much... but a new problem arose...

What if... in the variable group that i want to compare, there is numeric
and non numeric types?

Or, if you think its better, can i have two variables, one numeric and one
non numeric, and made the comparision?

Best regards,

Bruno

2012/10/12 arun kirshna [via R] 

> HI,
> Try this:
> test1<-read.table(text="
>    id x1 x2 x3 x4 x5 x6 x7
> 1   1 36 26 21 32 31 27 31
> 2   2 45 21 46 50 22 36 29
> 3   3 49 47 35 44 33 31 46
> 4   4 42 32 38 28 39 45 32
> 5   5 29 42 39 48 25 35 34
> 6   6 39 31 30 37 46 43 44
> 7   7 41 40 25 23 42 40 24
> 8   8 27 29 47 34 26 38 28
> 9   9 25 35 29 36 43 34 23
> 10 10 24 44 37 26 27 46 22
> 11 11 38 50 32 49 37 24 40
> 12 12 20 34 48 25 30 41 36
> 13 13 26 46 20 40 29 20 43
> 14 14 33 37 49 31 47 30 30
> 15 15 43 39 27 35 48 47 27
> ",sep="",header=TRUE)
> count40<-
> ifelse(test1$x1==40|test1$x2==40|test1$x3==40|test1$x4==40|test1$x5==40|test1$x6==40|test1$x7==40,0,1)
>
> count40
>  #[1] 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1
> #if you want to get the same result,
>  apply(test1,1,function(x) ifelse(any(x==40),0,1))
> # 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
>  #1  1  1  1  1  1  0  1  1  1  0  1  0  1  1
> A.K.
>
>
>
>
>
> --
>  If you reply to this email, your message will be added to the discussion
> below:
> http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645988.html
>  To unsubscribe from ifelse reformulation, click 
>here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4645981&code=YnJ1bm9zbTg3QGdtYWlsLmNvbXw0NjQ1OTgxfDIwMjc4MjE3MDg=>
> .
> NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4647431.html
Sent from the R help mailing list archive at Nabble.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-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] ifelse reformulation

2012-10-24 Thread arun



Hi,
Try this:
test1<-read.table(text=" 
   id x1 x2 x3 x4 x5 x6 x7 
1   1 36 26 21 32 31 27 31 
2   2 45 21 46 50 22 36 29 
3   3 49 47 35 44 33 31 46 
4   4 42 32 38 28 39 45 32 
5   5 29 42 39 48 25 35 34 
6   6 39 31 30 37 46 43 44 
7   7 41 40 25 23 42 40 24 
8   8 27 29 47 34 26 38 28 
9   9 25 35 29 36 43 34 23 
10 10 24 44 37 26 27 46 22 
11 11 38 50 32 49 37 24 40 
12 12 20 34 48 25 30 41 36 
13 13 26 46 20 40 29 20 43 
14 14 33 37 49 31 47 30 30 
15 15 43 39 27 35 48 47 27 
",sep="",header=TRUE) 


group<-c(40,50,60,70)
as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]%in%group),0,1)))
# [1] 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1

#Comparing the results individually

ifelse(test1$x4==40|test1$x4==50|test1$x4==60|test1$x4==70|test1$x5==40|test1$x5==50|test1$x5==60|test1$x5==70|test1$x6==40|test1$x6==50|test1$x6==60|test1$x7==70,0,1)
# [1] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1

 as.vector(apply(test1,1,function(x) 
ifelse(any(x[4:6]==40|x[4:6]==50|x[4:6]==60|x[4:6]==70),0,1)))
 #[1] 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1
A.K.

From: Bruno Marques 
To: arun  
Sent: Wednesday, October 24, 2012 12:47 PM
Subject: Re: [R] ifelse reformulation


Hi Arun, let me ask you just one more thing, please.

If i want to compare, not one but a group of values, can i do this:

group<-c(40,50,60,70)

as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]==group),0,1)))

What i want is: IF it finds any of the values in group, then 0.
I mean, it has to work like a OR... but i think this code will work like an 
AND... am i wrong?

Sorry for my bad english!

Best regards,

Bruno


2012/10/12 arun 

HI,
>Try this:
>count40<- ifelse(test1$x3==40|test1$x4==40|test1$x5==40,0,1)
>count40
>
># [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
>as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]==40),0,1)))
># [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
>A.K.
>
>
>
>
>- Original Message -
>From: brunosm 
>To: r-help@r-project.org
>Cc:
>Sent: Friday, October 12, 2012 10:18 AM
>Subject: Re: [R] ifelse reformulation
>
>That was exacly what i was looking for! Thanks a lot!
>Cheers!
>
>
>
>--
>View this message in context: 
>http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645990.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.
>
>

__
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] ifelse reformulation

2012-10-12 Thread Pieter Schoonees
You can use subscripting on the matrix, e.g. ind <- test == 40 and the 
test[ind] <- 1. Just deal with the "id" column when you set the rest to 0.

> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of brunosm
> Sent: Friday 12 October 2012 11:51
> To: r-help@r-project.org
> Subject: [R] ifelse reformulation
> 
> Hi, i'm trying to simplify some R code but i got stucked in this:
> 
> test<-data.frame(cbind(id,x1,x2,x3,x4,x5,x6,x7))
> test
> 
> > test
>id x1 x2 x3 x4 x5 x6 x7
> 1   1 36 26 21 32 31 27 31
> 2   2 45 21 46 50 22 36 29
> 3   3 49 47 35 44 33 31 46
> 4   4 42 32 38 28 39 45 32
> 5   5 29 42 39 48 25 35 34
> 6   6 39 31 30 37 46 43 44
> 7   7 41 40 25 23 42 40 24
> 8   8 27 29 47 34 26 38 28
> 9   9 25 35 29 36 43 34 23
> 10 10 24 44 37 26 27 46 22
> 11 11 38 50 32 49 37 24 40
> 12 12 20 34 48 25 30 41 36
> 13 13 26 46 20 40 29 20 43
> 14 14 33 37 49 31 47 30 30
> 15 15 43 39 27 35 48 47 27
> 
> count40<-
> ifelse(test$x1==40|test$x2==40|test$x3==40|test$x4==40|test$x5==40|test
> $x6==40|test$x7==40,0,1)
> count40
> 
> I'm trying to remake that ifelse. If any variable from x1 to x7 equals
> to 40
> --> 0, else --> 1
> 
> I was trying to do something like:
> 
> count40aux<-ifelse(test[,2:8]==40,0,1)
> count40aux
> 
> It doesn't work as i expected...
> 
> Can anyone help me please?
> 
> Regards,
> 
> Bruno
> 
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/ifelse-
> reformulation-tp4645981.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.

__
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] ifelse reformulation

2012-10-12 Thread brunosm
Hi arun kirshna

just let me ask you something else.

Imagin that i only want to "search" in the variables x3,x4 and x5. How can i
do this?

Regards,

Bruno



--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4646013.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.


Re: [R] ifelse reformulation

2012-10-12 Thread arun
HI, 
Try this: 
count40<- ifelse(test1$x3==40|test1$x4==40|test1$x5==40,0,1) 
count40 
# [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 
as.vector(apply(test1,1,function(x) ifelse(any(x[4:6]==40),0,1))) 
# [1] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 
A.K. 



- Original Message -
From: brunosm 
To: r-help@r-project.org
Cc: 
Sent: Friday, October 12, 2012 10:18 AM
Subject: Re: [R] ifelse reformulation

That was exacly what i was looking for! Thanks a lot!
Cheers!



--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645990.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.

__
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] ifelse reformulation

2012-10-12 Thread brunosm
That was exacly what i was looking for! Thanks a lot!
Cheers!



--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981p4645990.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.


[R] ifelse reformulation

2012-10-12 Thread brunosm
Hi, i'm trying to simplify some R code but i got stucked in this:

test<-data.frame(cbind(id,x1,x2,x3,x4,x5,x6,x7))
test

> test
   id x1 x2 x3 x4 x5 x6 x7
1   1 36 26 21 32 31 27 31
2   2 45 21 46 50 22 36 29
3   3 49 47 35 44 33 31 46
4   4 42 32 38 28 39 45 32
5   5 29 42 39 48 25 35 34
6   6 39 31 30 37 46 43 44
7   7 41 40 25 23 42 40 24
8   8 27 29 47 34 26 38 28
9   9 25 35 29 36 43 34 23
10 10 24 44 37 26 27 46 22
11 11 38 50 32 49 37 24 40
12 12 20 34 48 25 30 41 36
13 13 26 46 20 40 29 20 43
14 14 33 37 49 31 47 30 30
15 15 43 39 27 35 48 47 27

count40<-ifelse(test$x1==40|test$x2==40|test$x3==40|test$x4==40|test$x5==40|test$x6==40|test$x7==40,0,1)
count40

I'm trying to remake that ifelse. If any variable from x1 to x7 equals to 40
--> 0, else --> 1

I was trying to do something like:

count40aux<-ifelse(test[,2:8]==40,0,1)
count40aux

It doesn't work as i expected...

Can anyone help me please?

Regards,

Bruno





--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-reformulation-tp4645981.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.


Re: [R] Ifelse Execution

2012-10-01 Thread Jeff Newmiller
Ifelse is a vector function and is absolutely inappropriate for that use.
Use "if" instead.
Also, read the help for Sys.sleep... you need to tell it how long you want to 
sleep. You should compute how long that is from now and sleep that long.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Bhupendrasinh Thakre  wrote:

>Hi Everyone,
>
>I am trying to run a time based query and need some of your help.
>Not much of data or packages.
>
>Just a simple one.
>Query I am trying to execute.
>
>ifelse ((as.numeric(as.POSIXct("2012-10-01 20:38:00"))),
>(rnorm(1,2,1)),(Sys.sleep()))
>
>Note. Why I am using as.numeric is as I have a list of time at which I
>wanted to run the command.
>
>Something like 
>
>1349142243
>1349138667
>
>Question.
>
>1. The query is running before the time reaches in both R-Studio and
>R-Terminal in Mac based System.
>2. Sys.sleep () is ineffective in putting R on sleep till the time
>comes. 
>
>
>Bhupendrasinh Thakre
>
>
>
>
>
>
>   [[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.


[R] Ifelse Execution

2012-10-01 Thread Bhupendrasinh Thakre
Hi Everyone,

I am trying to run a time based query and need some of your help.
Not much of data or packages.

Just a simple one.
Query I am trying to execute.

ifelse ((as.numeric(as.POSIXct("2012-10-01 20:38:00"))), 
(rnorm(1,2,1)),(Sys.sleep()))

Note. Why I am using as.numeric is as I have a list of time at which I wanted 
to run the command.

Something like 

1349142243
1349138667

Question.

1. The query is running before the time reaches in both R-Studio and R-Terminal 
in Mac based System.
2. Sys.sleep () is ineffective in putting R on sleep till the time comes. 


Bhupendrasinh Thakre






[[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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Hah - I guess I didn't mean I understood it in full as I expect I will
run into it again without anticipating it.

But, now that I know the "old adage" I will look there first when I
run into a problem.

Also, I used the square root of machine precision instead - thanks for
that, too.

Thank you, thank you!  I really appreciate all the help.

Best,

Jen

On Fri, Aug 24, 2012 at 10:31 PM, R. Michael Weylandt
 wrote:
> sqrt(.Machine$double.eps)

__
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] ifelse problem - bug or operator error

2012-08-24 Thread R. Michael Weylandt
On Fri, Aug 24, 2012 at 7:29 PM, Jennifer Sabatier
 wrote:
> AHHH I GOT IT!!
>
> And I *think* I understand about floating point arithmetic..

Well then you're doing much better than the rest of us: it's quite a
difficult subject and only gets trickier as you think about it more.
(Numerical analysis generally, not the definition of an IEEE754 / ISO
6059 double) You even get such fun as

-1 * 0 != 1 * 0.

under some interpretations.

>
> In this case vn$PM.DIST.TOT is the sum of proportions.  So, it should
> be anywhere 0 and 1.
>
> In our case, if it's anything other than 1 when vn$PM.EXP is greater
> than 0  then it means something is wrong with one of the variables
> used to sum vn$PM.DIST.TOT.
>
> I was worried making it an integer will cause cases of 0.4 to be 0 and
> look legal, when it's not (though it doesn't actually seem to be a
> problem).
>
> So, I just did what Michael and Peter suggested, after reading up on
> floating points.
>
> fpf <- 1e-05   # fpf = floating point fuzz

Though I sugested 1e-05 here, usually one uses slightly more stringent
testing: a general rule of thumb is the square root of machine
precision. In R terms,

sqrt(.Machine$double.eps)

>
> vn$PM.DIST_flag<-ifelse(vn$PM.EXP > 0 & abs(vn$PM.DIST.TOT - 1) > fpf , 1, 0)
>
> YAY
>
> Thanks, solved AND I learned something new.
>
> Thanks, alll, and have a GREAT weekend!
>
> Jen

Just for the "macro-take-away": this is the reason we don't really
like console printout instead of dput() to show a problem: if you dput
the original not-yet-ifelse-d numbers, you'll see that they really
aren't 1's, but that they are truncated upon regular printing.

Cheers and don't forget the old adage: 0.1*10 is hardly ever 1,
Michael

>
>
> On Fri, Aug 24, 2012 at 6:27 PM, Peter Ehlers  wrote:
>> I see that you got other responses while I was composing an answer.
>> Your 'example.csv' did come through for me, but I still can't
>> replicate your PM.DIST_flag variable. Specifically, observations
>> 30, 33, 36 and 40 are wrong.
>>
>> I agree with Rui, that there's something else going on. The data
>> you've sent can't be the data that yielded the 'flag' variable
>> or you didn't use the ifelse() function in the way that you've
>> shown.
>>
>> I would start with a clean R session and I would use the 'convert
>> logical to numeric' idea (or keep a logical rather than numeric
>> flag):
>>
>>   vn <- transform(vn,
>>   my_flag = ( (PM.EXP > 0) & (PM.DIST.TOT != 1) ) * 1 )
>>
>> It looks as though your PM.DIST.TOT variable is meant to be
>> integer. If so, you might want to ensure that it is that type.
>> Otherwise, you might want to use Michael's suggestion of using
>> abs(... - 1) < 1e-05.
>>
>> Peter Ehlers
>>
>>
>> On 2012-08-24 14:56, Jennifer Sabatier wrote:
>>>
>>> Hi Michael,
>>>
>>> Thanks for letting me know how to post data.  I will try to upload it
>>> that way in a second.
>>>
>>> I can usually use code to make a reproducible dataset but this time
>>> with the ifelse behaving strangely (perhaps, it's probably me) I
>>> didn't think I could do it easily so I figured I would just put my
>>> data up.
>>>
>>> I will check out the R FAQ you mentioned.
>>>
>>> Thanks, again,
>>>
>>> Jen
>>>
>>>
>>>
>>> On Fri, Aug 24, 2012 at 5:50 PM, R. Michael Weylandt
>>>  wrote:

 On Fri, Aug 24, 2012 at 4:46 PM, Jennifer Sabatier
  wrote:
>
> Hi Michael,
>
> No, I never use attach(), exactly for the reasons you state.  To do
> due diligence I did a search of code for the function and it didn't
> come up (I would have been shocked because I never us it!).
>
> Now that real data is up, does your suggestion still apply?  I am
> reading it now.
>

 If you mean the data you sent to Peter, it got scrubbed by the list
 servers as well (they are somewhat draconian, but appropriately so in
 the long run). The absolute best way to send R data via email (esp on
 this list) is to use the dput() function which will create a plain
 text representation of your data _exactly_ as R sees it. It's a little
 hard for the untrained eye to parse (I can usually get about 90% of
 what it all means but there's some stuff with rownames = NA I've never
 looked into) but it's perfectly reproducible to a different R session.
 Then us having the same data is a simple copy+paste away.

 For more on dput() and reproducibility generally, see

 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

 It could be the floating point thing (it's hard to say without knowing
 how your data was calculated), but Rui seems to think not.

 M

> Thanks,
>
> Jen
>
> On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
>  wrote:
>>
>> Off the wall / wild guess, do you use attach() frequently? Not
>> entirely sure how it would come up, but it tends to make weird errors
>> li

Re: [R] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
AHHH I GOT IT!!

And I *think* I understand about floating point arithmetic..

In this case vn$PM.DIST.TOT is the sum of proportions.  So, it should
be anywhere 0 and 1.

In our case, if it's anything other than 1 when vn$PM.EXP is greater
than 0  then it means something is wrong with one of the variables
used to sum vn$PM.DIST.TOT.

I was worried making it an integer will cause cases of 0.4 to be 0 and
look legal, when it's not (though it doesn't actually seem to be a
problem).

So, I just did what Michael and Peter suggested, after reading up on
floating points.

fpf <- 1e-05   # fpf = floating point fuzz

vn$PM.DIST_flag<-ifelse(vn$PM.EXP > 0 & abs(vn$PM.DIST.TOT - 1) > fpf , 1, 0)

YAY

Thanks, solved AND I learned something new.

Thanks, alll, and have a GREAT weekend!

Jen


On Fri, Aug 24, 2012 at 6:27 PM, Peter Ehlers  wrote:
> I see that you got other responses while I was composing an answer.
> Your 'example.csv' did come through for me, but I still can't
> replicate your PM.DIST_flag variable. Specifically, observations
> 30, 33, 36 and 40 are wrong.
>
> I agree with Rui, that there's something else going on. The data
> you've sent can't be the data that yielded the 'flag' variable
> or you didn't use the ifelse() function in the way that you've
> shown.
>
> I would start with a clean R session and I would use the 'convert
> logical to numeric' idea (or keep a logical rather than numeric
> flag):
>
>   vn <- transform(vn,
>   my_flag = ( (PM.EXP > 0) & (PM.DIST.TOT != 1) ) * 1 )
>
> It looks as though your PM.DIST.TOT variable is meant to be
> integer. If so, you might want to ensure that it is that type.
> Otherwise, you might want to use Michael's suggestion of using
> abs(... - 1) < 1e-05.
>
> Peter Ehlers
>
>
> On 2012-08-24 14:56, Jennifer Sabatier wrote:
>>
>> Hi Michael,
>>
>> Thanks for letting me know how to post data.  I will try to upload it
>> that way in a second.
>>
>> I can usually use code to make a reproducible dataset but this time
>> with the ifelse behaving strangely (perhaps, it's probably me) I
>> didn't think I could do it easily so I figured I would just put my
>> data up.
>>
>> I will check out the R FAQ you mentioned.
>>
>> Thanks, again,
>>
>> Jen
>>
>>
>>
>> On Fri, Aug 24, 2012 at 5:50 PM, R. Michael Weylandt
>>  wrote:
>>>
>>> On Fri, Aug 24, 2012 at 4:46 PM, Jennifer Sabatier
>>>  wrote:

 Hi Michael,

 No, I never use attach(), exactly for the reasons you state.  To do
 due diligence I did a search of code for the function and it didn't
 come up (I would have been shocked because I never us it!).

 Now that real data is up, does your suggestion still apply?  I am
 reading it now.

>>>
>>> If you mean the data you sent to Peter, it got scrubbed by the list
>>> servers as well (they are somewhat draconian, but appropriately so in
>>> the long run). The absolute best way to send R data via email (esp on
>>> this list) is to use the dput() function which will create a plain
>>> text representation of your data _exactly_ as R sees it. It's a little
>>> hard for the untrained eye to parse (I can usually get about 90% of
>>> what it all means but there's some stuff with rownames = NA I've never
>>> looked into) but it's perfectly reproducible to a different R session.
>>> Then us having the same data is a simple copy+paste away.
>>>
>>> For more on dput() and reproducibility generally, see
>>>
>>> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>>>
>>> It could be the floating point thing (it's hard to say without knowing
>>> how your data was calculated), but Rui seems to think not.
>>>
>>> M
>>>
 Thanks,

 Jen

 On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
  wrote:
>
> Off the wall / wild guess, do you use attach() frequently? Not
> entirely sure how it would come up, but it tends to make weird errors
> like this occur.
>
> M
>
> On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
>  wrote:
>>
>> Hi Rui,
>>
>> Thanks so much for responding but I think with my HTML problem the vn
>> data you made must not be the same.  I tried running your code on the
>> data (I uploaded a copy) and I got the same thing I had before.
>>
>> Jen
>>
>> On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas 
>> wrote:
>>>
>>>
>>> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1
>>> 1  23
>>
>>
>> __
>> 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

Re: [R] ifelse problem - bug or operator error

2012-08-24 Thread Peter Ehlers

I see that you got other responses while I was composing an answer.
Your 'example.csv' did come through for me, but I still can't
replicate your PM.DIST_flag variable. Specifically, observations
30, 33, 36 and 40 are wrong.

I agree with Rui, that there's something else going on. The data
you've sent can't be the data that yielded the 'flag' variable
or you didn't use the ifelse() function in the way that you've
shown.

I would start with a clean R session and I would use the 'convert
logical to numeric' idea (or keep a logical rather than numeric
flag):

  vn <- transform(vn,
  my_flag = ( (PM.EXP > 0) & (PM.DIST.TOT != 1) ) * 1 )

It looks as though your PM.DIST.TOT variable is meant to be
integer. If so, you might want to ensure that it is that type.
Otherwise, you might want to use Michael's suggestion of using
abs(... - 1) < 1e-05.

Peter Ehlers

On 2012-08-24 14:56, Jennifer Sabatier wrote:

Hi Michael,

Thanks for letting me know how to post data.  I will try to upload it
that way in a second.

I can usually use code to make a reproducible dataset but this time
with the ifelse behaving strangely (perhaps, it's probably me) I
didn't think I could do it easily so I figured I would just put my
data up.

I will check out the R FAQ you mentioned.

Thanks, again,

Jen



On Fri, Aug 24, 2012 at 5:50 PM, R. Michael Weylandt
 wrote:

On Fri, Aug 24, 2012 at 4:46 PM, Jennifer Sabatier
 wrote:

Hi Michael,

No, I never use attach(), exactly for the reasons you state.  To do
due diligence I did a search of code for the function and it didn't
come up (I would have been shocked because I never us it!).

Now that real data is up, does your suggestion still apply?  I am
reading it now.



If you mean the data you sent to Peter, it got scrubbed by the list
servers as well (they are somewhat draconian, but appropriately so in
the long run). The absolute best way to send R data via email (esp on
this list) is to use the dput() function which will create a plain
text representation of your data _exactly_ as R sees it. It's a little
hard for the untrained eye to parse (I can usually get about 90% of
what it all means but there's some stuff with rownames = NA I've never
looked into) but it's perfectly reproducible to a different R session.
Then us having the same data is a simple copy+paste away.

For more on dput() and reproducibility generally, see
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

It could be the floating point thing (it's hard to say without knowing
how your data was calculated), but Rui seems to think not.

M


Thanks,

Jen

On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
 wrote:

Off the wall / wild guess, do you use attach() frequently? Not
entirely sure how it would come up, but it tends to make weird errors
like this occur.

M

On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
 wrote:

Hi Rui,

Thanks so much for responding but I think with my HTML problem the vn
data you made must not be the same.  I tried running your code on the
data (I uploaded a copy) and I got the same thing I had before.

Jen

On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:


165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23


__
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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Hi Michael,

Thanks for letting me know how to post data.  I will try to upload it
that way in a second.

I can usually use code to make a reproducible dataset but this time
with the ifelse behaving strangely (perhaps, it's probably me) I
didn't think I could do it easily so I figured I would just put my
data up.

I will check out the R FAQ you mentioned.

Thanks, again,

Jen



On Fri, Aug 24, 2012 at 5:50 PM, R. Michael Weylandt
 wrote:
> On Fri, Aug 24, 2012 at 4:46 PM, Jennifer Sabatier
>  wrote:
>> Hi Michael,
>>
>> No, I never use attach(), exactly for the reasons you state.  To do
>> due diligence I did a search of code for the function and it didn't
>> come up (I would have been shocked because I never us it!).
>>
>> Now that real data is up, does your suggestion still apply?  I am
>> reading it now.
>>
>
> If you mean the data you sent to Peter, it got scrubbed by the list
> servers as well (they are somewhat draconian, but appropriately so in
> the long run). The absolute best way to send R data via email (esp on
> this list) is to use the dput() function which will create a plain
> text representation of your data _exactly_ as R sees it. It's a little
> hard for the untrained eye to parse (I can usually get about 90% of
> what it all means but there's some stuff with rownames = NA I've never
> looked into) but it's perfectly reproducible to a different R session.
> Then us having the same data is a simple copy+paste away.
>
> For more on dput() and reproducibility generally, see
> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>
> It could be the floating point thing (it's hard to say without knowing
> how your data was calculated), but Rui seems to think not.
>
> M
>
>> Thanks,
>>
>> Jen
>>
>> On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
>>  wrote:
>>> Off the wall / wild guess, do you use attach() frequently? Not
>>> entirely sure how it would come up, but it tends to make weird errors
>>> like this occur.
>>>
>>> M
>>>
>>> On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
>>>  wrote:
 Hi Rui,

 Thanks so much for responding but I think with my HTML problem the vn
 data you made must not be the same.  I tried running your code on the
 data (I uploaded a copy) and I got the same thing I had before.

 Jen

 On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:
>
> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  
> 23

__
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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Oh, sorry, I first though you couldn't post data to the list, but then
I thought I remembered other people doing so, so I tried to post it.

Here is a copy.

Thanks,

Jen

On Fri, Aug 24, 2012 at 5:49 PM, Rui Barradas  wrote:
> No data arrived to me.
>
> Rui Barradas
> Em 24-08-2012 22:46, Jennifer Sabatier escreveu:
>
>> Hi Michael,
>>
>> No, I never use attach(), exactly for the reasons you state.  To do
>> due diligence I did a search of code for the function and it didn't
>> come up (I would have been shocked because I never us it!).
>>
>> Now that real data is up, does your suggestion still apply?  I am
>> reading it now.
>>
>> Thanks,
>>
>> Jen
>>
>> On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
>>  wrote:
>>>
>>> Off the wall / wild guess, do you use attach() frequently? Not
>>> entirely sure how it would come up, but it tends to make weird errors
>>> like this occur.
>>>
>>> M
>>>
>>> On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
>>>  wrote:

 Hi Rui,

 Thanks so much for responding but I think with my HTML problem the vn
 data you made must not be the same.  I tried running your code on the
 data (I uploaded a copy) and I got the same thing I had before.

 Jen

 On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas 
 wrote:
>
> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1
> 23
>
>
__
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] ifelse problem - bug or operator error

2012-08-24 Thread R. Michael Weylandt
On Fri, Aug 24, 2012 at 4:50 PM, R. Michael Weylandt
 wrote:
> On Fri, Aug 24, 2012 at 4:46 PM, Jennifer Sabatier
>  wrote:
>> Hi Michael,
>>
>> No, I never use attach(), exactly for the reasons you state.  To do
>> due diligence I did a search of code for the function and it didn't
>> come up (I would have been shocked because I never us it!).
>>
>> Now that real data is up, does your suggestion still apply?  I am
>> reading it now.
>>
>
> If you mean the data you sent to Peter, it got scrubbed by the list
> servers as well (they are somewhat draconian, but appropriately so in
> the long run). The absolute best way to send R data via email (esp on
> this list) is to use the dput() function which will create a plain
> text representation of your data _exactly_ as R sees it. It's a little
> hard for the untrained eye to parse (I can usually get about 90% of
> what it all means but there's some stuff with rownames = NA I've never
> looked into) but it's perfectly reproducible to a different R session.
> Then us having the same data is a simple copy+paste away.

Note that the dput() output is to be put into the body of the email,
not in an attachment or we're back where we started ;-)

M

>
> For more on dput() and reproducibility generally, see
> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>
> It could be the floating point thing (it's hard to say without knowing
> how your data was calculated), but Rui seems to think not.
>
> M
>
>> Thanks,
>>
>> Jen
>>
>> On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
>>  wrote:
>>> Off the wall / wild guess, do you use attach() frequently? Not
>>> entirely sure how it would come up, but it tends to make weird errors
>>> like this occur.
>>>
>>> M
>>>
>>> On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
>>>  wrote:
 Hi Rui,

 Thanks so much for responding but I think with my HTML problem the vn
 data you made must not be the same.  I tried running your code on the
 data (I uploaded a copy) and I got the same thing I had before.

 Jen

 On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:
>
> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  
> 23

__
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] ifelse problem - bug or operator error

2012-08-24 Thread R. Michael Weylandt
On Fri, Aug 24, 2012 at 4:46 PM, Jennifer Sabatier
 wrote:
> Hi Michael,
>
> No, I never use attach(), exactly for the reasons you state.  To do
> due diligence I did a search of code for the function and it didn't
> come up (I would have been shocked because I never us it!).
>
> Now that real data is up, does your suggestion still apply?  I am
> reading it now.
>

If you mean the data you sent to Peter, it got scrubbed by the list
servers as well (they are somewhat draconian, but appropriately so in
the long run). The absolute best way to send R data via email (esp on
this list) is to use the dput() function which will create a plain
text representation of your data _exactly_ as R sees it. It's a little
hard for the untrained eye to parse (I can usually get about 90% of
what it all means but there's some stuff with rownames = NA I've never
looked into) but it's perfectly reproducible to a different R session.
Then us having the same data is a simple copy+paste away.

For more on dput() and reproducibility generally, see
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

It could be the floating point thing (it's hard to say without knowing
how your data was calculated), but Rui seems to think not.

M

> Thanks,
>
> Jen
>
> On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
>  wrote:
>> Off the wall / wild guess, do you use attach() frequently? Not
>> entirely sure how it would come up, but it tends to make weird errors
>> like this occur.
>>
>> M
>>
>> On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
>>  wrote:
>>> Hi Rui,
>>>
>>> Thanks so much for responding but I think with my HTML problem the vn
>>> data you made must not be the same.  I tried running your code on the
>>> data (I uploaded a copy) and I got the same thing I had before.
>>>
>>> Jen
>>>
>>> On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:

 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23

__
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] ifelse problem - bug or operator error

2012-08-24 Thread Rui Barradas

No data arrived to me.

Rui Barradas
Em 24-08-2012 22:46, Jennifer Sabatier escreveu:

Hi Michael,

No, I never use attach(), exactly for the reasons you state.  To do
due diligence I did a search of code for the function and it didn't
come up (I would have been shocked because I never us it!).

Now that real data is up, does your suggestion still apply?  I am
reading it now.

Thanks,

Jen

On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
 wrote:

Off the wall / wild guess, do you use attach() frequently? Not
entirely sure how it would come up, but it tends to make weird errors
like this occur.

M

On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
 wrote:

Hi Rui,

Thanks so much for responding but I think with my HTML problem the vn
data you made must not be the same.  I tried running your code on the
data (I uploaded a copy) and I got the same thing I had before.

Jen

On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:

165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23


__
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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Hi Michael,

No, I never use attach(), exactly for the reasons you state.  To do
due diligence I did a search of code for the function and it didn't
come up (I would have been shocked because I never us it!).

Now that real data is up, does your suggestion still apply?  I am
reading it now.

Thanks,

Jen

On Fri, Aug 24, 2012 at 5:38 PM, R. Michael Weylandt
 wrote:
> Off the wall / wild guess, do you use attach() frequently? Not
> entirely sure how it would come up, but it tends to make weird errors
> like this occur.
>
> M
>
> On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
>  wrote:
>> Hi Rui,
>>
>> Thanks so much for responding but I think with my HTML problem the vn
>> data you made must not be the same.  I tried running your code on the
>> data (I uploaded a copy) and I got the same thing I had before.
>>
>> Jen
>>
>> On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:
>>>
>>> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23

__
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] ifelse problem - bug or operator error

2012-08-24 Thread R. Michael Weylandt
Off the wall / wild guess, do you use attach() frequently? Not
entirely sure how it would come up, but it tends to make weird errors
like this occur.

M

On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier
 wrote:
> Hi Rui,
>
> Thanks so much for responding but I think with my HTML problem the vn
> data you made must not be the same.  I tried running your code on the
> data (I uploaded a copy) and I got the same thing I had before.
>
> Jen
>
> On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:
>>
>> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23

__
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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Hi Rui,

Thanks so much for responding but I think with my HTML problem the vn
data you made must not be the same.  I tried running your code on the
data (I uploaded a copy) and I got the same thing I had before.

Jen

On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas  wrote:
>
> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23

__
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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
BTW - no one else who has replied to this topic was snobby or
unfriendly and I thank you very much for trying to help me.

It's just Bert is not the first to respond to my request for help as
such.  As someone looking forward to becoming an advanced R programmer
in my statistical work it is discouraging to be castigated FOR ASKING
FOR HELP.

Thank you, all.

Best,

Jen

__
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] ifelse problem - bug or operator error

2012-08-24 Thread Rui Barradas

Hello,

Michael's standard guess, FAQ 7.31, was also mine, but is wrong. The 
error is in Jennifer's flag column,  not the result of her ifelse. (!)



x <- scan(what="character", text="
 PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0 31403 1
0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0 0 0
83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23
1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0 1478 1 0
2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1
")

vn <- matrix(as.numeric(x[-(1:3)]), ncol=3, byrow=TRUE)
vn <- data.frame(vn)
names(vn) <- x[1:3]
str(vn)
vn$flag2 <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1, 0 )

str(vn)
identical(vn$PM.DIST_flag, vn$flag2)  # FALSE
inx <- vn$PM.DIST_flag != vn$flag2
vn[inx, ]

As you can see, there's nothing wrong with ifelse. The second standard 
guess is that Jennifer had something, a variable, in her session messing 
up with the variables envolved in the ifelse.


Also, when the result of a if/else or ifelse is, by this order, 1 or 0, 
the following can be used, with performance benefits.


vn$flag3 <- 1 * (vn$PM.EXP > 0.0 & vn$PM.DIST.TOT != 1.0) # make T/F 
numeric 1/0


Hope this helps,

Rui Barradas
Em 24-08-2012 21:43, Jennifer Sabatier escreveu:

Oops, sorry, I thought I was in plain text.  I can't tell the
difference because I use so little formatting in my emails.

Try this (a truncated version since I have to hand space everything):

PM.EXP  PM.DIST.TOT PM.DIST_flag
0  00
6417   11
23 10
97691   2   1
0  00
33993   2   1


On Fri, Aug 24, 2012 at 4:36 PM, R. Michael Weylandt
 wrote:

On Fri, Aug 24, 2012 at 3:22 PM, Jennifer Sabatier
 wrote:

Hi R-Helpers,

I don't think I need to post a dataset for this question but if I do, I
can.  Anyway, I am having a lot of trouble with the ifelse command.


You probably should have: dput() makes it super easy as well.


Here is my code:


vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0),
1,
0 )


And here is my output that doesn't make ANY sense:

   PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0
31403 1
0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0
0 0
83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1
23
1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1
0
2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1

Indeed it makes no sense to me either because you sent HTML email
which got mangled by the server.


As you can see, there are many instances where PM.EXP > 0 and
PM.DIST.TOT =
1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
such as the last line of data.

WWHHH Why why why why why why why? Why?

(Sorry, I've been trying to figure this out for hours and I've devolved
to
mumbling in corners and banging my head against the table)

What in the world am I doing wrong?  Or is ifelse not the right
function?

First guess standard problems with equality of floating point
numbers. (See R FAQ 7.31 for the details)

You probably want to change

x == 1

to

abs(x - 1) < 1e-05

or something similar.

Cheers,
Michael


Best,

Jen

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


__
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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Hi Peter,

I'm really sorry, I thought I was in plain text.  I don't use any
formatting in my emails and in Gmail the HTML looks the same as plain
text.

Anyway, I've attached the data (I didn't think we could do that but I
am frequently wrong).

I say many cases because this is just a subset of >300 observations.

The error seems to happen without a pattern I can discern.  I am
assuming I am doing something wrong.

Thanks,

Jen


On Fri, Aug 24, 2012 at 5:17 PM, Peter Ehlers  wrote:
> On 2012-08-24 13:22, Jennifer Sabatier wrote:
>>
>> Hi R-Helpers,
>>
>> I don't think I need to post a dataset for this question but if I do, I
>> can.  Anyway, I am having a lot of trouble with the ifelse command.
>>
>> Here is my code:
>>
>>
>> vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1,
>> 0 )
>>
>>
>> And here is my output that doesn't make ANY sense:
>>
>>PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0  31403
>> 1
>> 0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0 0
>> 0
>> 83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
>> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1
>> 23
>> 1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1 0
>> 2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1
>>
>> As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT
>> =
>> 1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
>> such as the last line of data.
>
>
> _Many_ instances?? I see only 4 such cases. Still not good, though.
> Here's what you should do:
>
> 1. Don't send html mail.
> 2. use simple variable names (x,y,z would do fine here).
> 3. either provide the data or at least a part of it with dput() or
>at least provide str(vn).
> 4. when you're trying to decide between operator error and bug, go
>with the operator error theory. You'll be correct at least 99% of
>the time.
>
> I ran your command on the above (suitably deciphered) data and had no
> problem getting what I think you expect (i.e. the four suspect cases
> came out just as they should). But what my mailer provides as your
> data may not be what you really have.
>
> Oh, and get a bandage for that head bruise.
>
> Peter Ehlers
>
>>
>> WWHHH Why why why why why why why? Why?
>>
>> (Sorry, I've been trying to figure this out for hours and I've devolved to
>> mumbling in corners and banging my head against the table)
>>
>> What in the world am I doing wrong?  Or is ifelse not the right function?
>>
>> Best,
>>
>> Jen
>>
>> [[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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Bert,

I will thank you not to condescend to me, as I am too damn old (40) to
be treated that way.  You didn't even offer a solution to my problem.
You only came to chastise me with regards to your assumptions about
me, which is very annoying.

While I am at the beginner level of R, I am not an idiot.  Or an
infant.  I've been a statistician for more than 12 years, mostly
programming in SAS.  I've been moving into R for the past 3 years.

I am EXTENSIVELY familiar with the documentation.  The documentation
is how I've learned to code in R.

I NEVER post to R-help until I've exhausted my own ability to solve
the problem, INCLUDING combing through the documentation.  And looking
at other requests for help to see if the problem has arisen for
someone else.

One of the reason I wait to the last minute to post to R-help is
because it is a very snobby and often unfriendly list, as your comment
illustrates.  You just assumed I hadn't already looked though the
docs.  Nice.

The reality of it is, what Michael pointed out to me wasn't something
I'd discovered in my own search of the docs.  And that, Bert, someone
like me comes to R-help.

Please, Bert, don't we want the whole world to use R?  They won't if
the community is so unwelcoming.

Now, if you have a solution, please post it.  If not, leave off while
I explore Michael's suggestion.

Best,

Jen



On Fri, Aug 24, 2012 at 4:49 PM, Bert Gunter  wrote:
> ... and if Michael is correct, there is a lesson here: Think of how
> much time and aggravation you would have saved yourself if you had
> FIRST made an effort to read the docs. The FAQ's are there for a
> reason. As is An Introduction to R, which also should be read before
> posting on this list.
>
> If Michael is wrong, then the above homily should be amended to:
> If you have not already done so, read the FAQ's and An Introduction to
> R. You will save yourself -- and us -- much time and effort by doing
> so.
>
> Thus endeth the lesson.
>
> -- Bert
>
> On Fri, Aug 24, 2012 at 1:36 PM, R. Michael Weylandt
>  wrote:
>> On Fri, Aug 24, 2012 at 3:22 PM, Jennifer Sabatier
>>  wrote:
>>> Hi R-Helpers,
>>>
>>> I don't think I need to post a dataset for this question but if I do, I
>>> can.  Anyway, I am having a lot of trouble with the ifelse command.
>>>
>> You probably should have: dput() makes it super easy as well.
>>
>>> Here is my code:
>>>
>>>
>>> vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1,
>>> 0 )
>>>
>>>
>>> And here is my output that doesn't make ANY sense:
>>>
>>>   PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0  31403 1
>>> 0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0 0 0
>>> 83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
>>> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23
>>> 1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1 0
>>> 2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1
>>
>> Indeed it makes no sense to me either because you sent HTML email
>> which got mangled by the server.
>>
>>>
>>> As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT =
>>> 1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
>>> such as the last line of data.
>>>
>>> WWHHH Why why why why why why why? Why?
>>>
>>> (Sorry, I've been trying to figure this out for hours and I've devolved to
>>> mumbling in corners and banging my head against the table)
>>>
>>> What in the world am I doing wrong?  Or is ifelse not the right function?
>>
>> First guess standard problems with equality of floating point
>> numbers. (See R FAQ 7.31 for the details)
>>
>> You probably want to change
>>
>> x == 1
>>
>> to
>>
>> abs(x - 1) < 1e-05
>>
>> or something similar.
>>
>> Cheers,
>> Michael
>>
>>>
>>> Best,
>>>
>>> Jen
>>>
>>> [[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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] ifelse problem - bug or operator error

2012-08-24 Thread Peter Ehlers

On 2012-08-24 13:22, Jennifer Sabatier wrote:

Hi R-Helpers,

I don't think I need to post a dataset for this question but if I do, I
can.  Anyway, I am having a lot of trouble with the ifelse command.

Here is my code:


vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1,
0 )


And here is my output that doesn't make ANY sense:

   PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0  31403 1
0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0 0 0
83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23
1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1 0
2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1

As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT =
1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
such as the last line of data.


_Many_ instances?? I see only 4 such cases. Still not good, though.
Here's what you should do:

1. Don't send html mail.
2. use simple variable names (x,y,z would do fine here).
3. either provide the data or at least a part of it with dput() or
   at least provide str(vn).
4. when you're trying to decide between operator error and bug, go
   with the operator error theory. You'll be correct at least 99% of
   the time.

I ran your command on the above (suitably deciphered) data and had no
problem getting what I think you expect (i.e. the four suspect cases
came out just as they should). But what my mailer provides as your
data may not be what you really have.

Oh, and get a bandage for that head bruise.

Peter Ehlers



WWHHH Why why why why why why why? Why?

(Sorry, I've been trying to figure this out for hours and I've devolved to
mumbling in corners and banging my head against the table)

What in the world am I doing wrong?  Or is ifelse not the right function?

Best,

Jen

[[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] ifelse problem - bug or operator error

2012-08-24 Thread Bert Gunter
... and if Michael is correct, there is a lesson here: Think of how
much time and aggravation you would have saved yourself if you had
FIRST made an effort to read the docs. The FAQ's are there for a
reason. As is An Introduction to R, which also should be read before
posting on this list.

If Michael is wrong, then the above homily should be amended to:
If you have not already done so, read the FAQ's and An Introduction to
R. You will save yourself -- and us -- much time and effort by doing
so.

Thus endeth the lesson.

-- Bert

On Fri, Aug 24, 2012 at 1:36 PM, R. Michael Weylandt
 wrote:
> On Fri, Aug 24, 2012 at 3:22 PM, Jennifer Sabatier
>  wrote:
>> Hi R-Helpers,
>>
>> I don't think I need to post a dataset for this question but if I do, I
>> can.  Anyway, I am having a lot of trouble with the ifelse command.
>>
> You probably should have: dput() makes it super easy as well.
>
>> Here is my code:
>>
>>
>> vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1,
>> 0 )
>>
>>
>> And here is my output that doesn't make ANY sense:
>>
>>   PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0  31403 1
>> 0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0 0 0
>> 83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
>> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23
>> 1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1 0
>> 2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1
>
> Indeed it makes no sense to me either because you sent HTML email
> which got mangled by the server.
>
>>
>> As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT =
>> 1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
>> such as the last line of data.
>>
>> WWHHH Why why why why why why why? Why?
>>
>> (Sorry, I've been trying to figure this out for hours and I've devolved to
>> mumbling in corners and banging my head against the table)
>>
>> What in the world am I doing wrong?  Or is ifelse not the right function?
>
> First guess standard problems with equality of floating point
> numbers. (See R FAQ 7.31 for the details)
>
> You probably want to change
>
> x == 1
>
> to
>
> abs(x - 1) < 1e-05
>
> or something similar.
>
> Cheers,
> Michael
>
>>
>> Best,
>>
>> Jen
>>
>> [[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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Oops, sorry, I thought I was in plain text.  I can't tell the
difference because I use so little formatting in my emails.

Try this (a truncated version since I have to hand space everything):

PM.EXP  PM.DIST.TOT PM.DIST_flag
0  00
6417   11
23 10
97691   2   1
0  00
33993   2   1


On Fri, Aug 24, 2012 at 4:36 PM, R. Michael Weylandt
 wrote:
>
> On Fri, Aug 24, 2012 at 3:22 PM, Jennifer Sabatier
>  wrote:
> > Hi R-Helpers,
> >
> > I don't think I need to post a dataset for this question but if I do, I
> > can.  Anyway, I am having a lot of trouble with the ifelse command.
> >
> You probably should have: dput() makes it super easy as well.
>
> > Here is my code:
> >
> >
> > vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0),
> > 1,
> > 0 )
> >
> >
> > And here is my output that doesn't make ANY sense:
> >
> >   PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0
> > 31403 1
> > 0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0
> > 0 0
> > 83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
> > 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1
> > 23
> > 1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1
> > 0
> > 2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1
>
> Indeed it makes no sense to me either because you sent HTML email
> which got mangled by the server.
>
> >
> > As you can see, there are many instances where PM.EXP > 0 and
> > PM.DIST.TOT =
> > 1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
> > such as the last line of data.
> >
> > WWHHH Why why why why why why why? Why?
> >
> > (Sorry, I've been trying to figure this out for hours and I've devolved
> > to
> > mumbling in corners and banging my head against the table)
> >
> > What in the world am I doing wrong?  Or is ifelse not the right
> > function?
>
> First guess standard problems with equality of floating point
> numbers. (See R FAQ 7.31 for the details)
>
> You probably want to change
>
> x == 1
>
> to
>
> abs(x - 1) < 1e-05
>
> or something similar.
>
> Cheers,
> Michael
>
> >
> > Best,
> >
> > Jen
> >
> > [[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] ifelse problem - bug or operator error

2012-08-24 Thread R. Michael Weylandt
On Fri, Aug 24, 2012 at 3:22 PM, Jennifer Sabatier
 wrote:
> Hi R-Helpers,
>
> I don't think I need to post a dataset for this question but if I do, I
> can.  Anyway, I am having a lot of trouble with the ifelse command.
>
You probably should have: dput() makes it super easy as well.

> Here is my code:
>
>
> vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1,
> 0 )
>
>
> And here is my output that doesn't make ANY sense:
>
>   PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0  31403 1
> 0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0 0 0
> 83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
> 165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23
> 1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1 0
> 2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1

Indeed it makes no sense to me either because you sent HTML email
which got mangled by the server.

>
> As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT =
> 1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
> such as the last line of data.
>
> WWHHH Why why why why why why why? Why?
>
> (Sorry, I've been trying to figure this out for hours and I've devolved to
> mumbling in corners and banging my head against the table)
>
> What in the world am I doing wrong?  Or is ifelse not the right function?

First guess standard problems with equality of floating point
numbers. (See R FAQ 7.31 for the details)

You probably want to change

x == 1

to

abs(x - 1) < 1e-05

or something similar.

Cheers,
Michael

>
> Best,
>
> Jen
>
> [[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.


[R] ifelse problem - bug or operator error

2012-08-24 Thread Jennifer Sabatier
Hi R-Helpers,

I don't think I need to post a dataset for this question but if I do, I
can.  Anyway, I am having a lot of trouble with the ifelse command.

Here is my code:


vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1,
0 )


And here is my output that doesn't make ANY sense:

  PM.EXP PM.DIST.TOT PM.DIST_flag  0 0 0  0 0 0  0 0 0  177502 1 0  31403 1
0  0 0 0  1100549 1 0  38762 1 0  0 0 0  20025 1 0  0 0 0  13742 1 0  0 0 0
83078 1 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
165114 1 0  0 0 0  417313 1 0  3546 1 0  4613 1 0  225460 1 0  6417 1 1  23
1 0  3402 1 0  8504 1 1  8552 1 0  9723 1 0  37273 1 1  396 1 0  1478 1 0
2074 1 0  12220 1 1  97691 2 1  0 0 0  33993 2 1

As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT =
1 yet PM.DIST_flag = 1 and it should be 0.  It should only flag in cases
such as the last line of data.

WWHHH Why why why why why why why? Why?

(Sorry, I've been trying to figure this out for hours and I've devolved to
mumbling in corners and banging my head against the table)

What in the world am I doing wrong?  Or is ifelse not the right function?

Best,

Jen

[[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] ifelse help

2012-07-10 Thread Jeff Newmiller
You failed to tell us any of: the actual code you used, the data you were 
working with, or the actual error you got. Your "pseudo-code" looks okay to me, 
so I suggest you read the Posting Guide and try again. (Anything other than a 
complete reproducible example of your problem is unlikely to elicit useful 
responses. Pseudo-code is definitely "other".)
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Jeff  wrote:

>
>I'm sure this is easy, but I'm new to R and can't find any example of 
>the following.
>
>Here's what I'm trying to do in pseudo-code.
>
>data$newvar <- ifelse(data$oldvar1 == 8, 1,data$oldvar2)
>
>In other words, if the existing variable equals 8, then the new 
>variable should equal 1, otherwise the new variable should equal the 
>value of another existing variable.
>
>I've tried to follow the examples given on the web and in the R 
>manuals, and each time I get errors or unexpected values.
>
>Thanks
>
>Jeff
>
>__
>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] ifelse help

2012-07-10 Thread Jeff

At 07:41 PM 7/10/2012, you wrote:


Seems to work for me:

> x <- data.frame(old1 = sample(c(1,2,8), 10, TRUE), old2 = 1:10)
> x$new <- ifelse(x$old1 == 8, 1, x$old2)
> x



Thanks Jim and Dan. The problem ended up with a missing values issue 
that threw me off. When your post confirmed that my code was right 
all along, I finally figured out the real problem.


Jeff

__
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] ifelse help

2012-07-10 Thread Nordlund, Dan (DSHS/RDA)
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Jeff
> Sent: Tuesday, July 10, 2012 5:26 PM
> To: r-help
> Subject: [R] ifelse help
> 
> 
> I'm sure this is easy, but I'm new to R and can't find any example of
> the following.
> 
> Here's what I'm trying to do in pseudo-code.
> 
> data$newvar <- ifelse(data$oldvar1 == 8, 1,data$oldvar2)
> 
> In other words, if the existing variable equals 8, then the new
> variable should equal 1, otherwise the new variable should equal the
> value of another existing variable.
> 
> I've tried to follow the examples given on the web and in the R
> manuals, and each time I get errors or unexpected values.
> 
> Thanks
> 
> Jeff
> 

Rather than tell us that you tried some examples and they didn't work, show us 
what you tried (i.e. a reproducible example as requested in the posting guide) 
so that we can play along at home. :-)  

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


__
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] ifelse help

2012-07-10 Thread jim holtman
Seems to work for me:

> x <- data.frame(old1 = sample(c(1,2,8), 10, TRUE), old2 = 1:10)
> x$new <- ifelse(x$old1 == 8, 1, x$old2)
> x
   old1 old2 new
1 11   1
2 22   2
3 23   3
4 84   1
5 15   5
6 86   1
7 87   1
8 28   8
9 29   9
101   10  10


On Tue, Jul 10, 2012 at 8:26 PM, Jeff  wrote:
>
> I'm sure this is easy, but I'm new to R and can't find any example of the
> following.
>
> Here's what I'm trying to do in pseudo-code.
>
> data$newvar <- ifelse(data$oldvar1 == 8, 1,data$oldvar2)
>
> In other words, if the existing variable equals 8, then the new variable
> should equal 1, otherwise the new variable should equal the value of another
> existing variable.
>
> I've tried to follow the examples given on the web and in the R manuals, and
> each time I get errors or unexpected values.
>
> Thanks
>
> Jeff
>
> __
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

__
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] ifelse help

2012-07-10 Thread Jeff


I'm sure this is easy, but I'm new to R and can't find any example of 
the following.


Here's what I'm trying to do in pseudo-code.

data$newvar <- ifelse(data$oldvar1 == 8, 1,data$oldvar2)

In other words, if the existing variable equals 8, then the new 
variable should equal 1, otherwise the new variable should equal the 
value of another existing variable.


I've tried to follow the examples given on the web and in the R 
manuals, and each time I get errors or unexpected values.


Thanks

Jeff

__
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] Ifelse on matrix using a vector argument

2012-06-05 Thread Özgür Asar
A generalized procedure for real life datasets might be

out<-rep(matrix(arg),ncol(ma))-ma
out[which(arg==0),]<--out[which(arg==0),]
out

Ozgur


--
View this message in context: 
http://r.789695.n4.nabble.com/Ifelse-on-matrix-using-a-vector-argument-tp4632485p4632490.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.


  1   2   3   >