Re: [R] if-else that returns vector

2023-10-13 Thread Christofer Bogaso
This is very interesting. Thanks for sharing.

On Fri, Oct 13, 2023 at 10:25 AM Richard O'Keefe  wrote:
>
> ?ifelse
> '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'.
>
> This is actually rather startling, because elsewhere in the
> S (R) language, operands are normally replicated to the length
> of the longer.  Thus
> c(1,2,3)*10 + c(5,6)
> first (notionally) replicates 10 to c(10,10,10)
> and then c(5,6) to c(5,6,5), yielding c(15,26,35).
> And this *does* happen, sort of.
> > ifelse(c(F,T,F), c(1,2,3), c(5,6))
> => 5 2 5.
> But it *doesn't* apply to the test.
>
> There's another surprise.  Years ago I expected that
> all three arguments would be evaluated, then length
> adjusted, and then processing would be done.
> But the 2nd argument is evaluated (in full) if and only
> if some element of the test is true,
> and the 3rd argument is evaluated (in full) if and oly
> if some element of the test is false.
> ifelse(c(NA,NA), stop("true"), stop("false"))
> => c(NA,NA).
>
> At any rate, what you want is if ()  else 
>
>
>
> On Fri, 13 Oct 2023 at 09:22, Christofer Bogaso  
> wrote:
>>
>> Hi,
>>
>> Following expression returns only the first element
>>
>> ifelse(T, c(1,2,3), c(5,6))
>>
>> However I am looking for some one-liner expression like above which
>> will return the entire vector.
>>
>> Is there any way to achieve this?
>>
>> __
>> 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] if-else that returns vector

2023-10-12 Thread Richard O'Keefe
?ifelse
'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'.

This is actually rather startling, because elsewhere in the
S (R) language, operands are normally replicated to the length
of the longer.  Thus
c(1,2,3)*10 + c(5,6)
first (notionally) replicates 10 to c(10,10,10)
and then c(5,6) to c(5,6,5), yielding c(15,26,35).
And this *does* happen, sort of.
> ifelse(c(F,T,F), c(1,2,3), c(5,6))
=> 5 2 5.
But it *doesn't* apply to the test.

There's another surprise.  Years ago I expected that
all three arguments would be evaluated, then length
adjusted, and then processing would be done.
But the 2nd argument is evaluated (in full) if and only
if some element of the test is true,
and the 3rd argument is evaluated (in full) if and oly
if some element of the test is false.
ifelse(c(NA,NA), stop("true"), stop("false"))
=> c(NA,NA).

At any rate, what you want is if ()  else 



On Fri, 13 Oct 2023 at 09:22, Christofer Bogaso 
wrote:

> Hi,
>
> Following expression returns only the first element
>
> ifelse(T, c(1,2,3), c(5,6))
>
> However I am looking for some one-liner expression like above which
> will return the entire vector.
>
> Is there any way to achieve this?
>
> __
> 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] if-else that returns vector

2023-10-12 Thread Rui Barradas

Às 21:22 de 12/10/2023, Christofer Bogaso escreveu:

Hi,

Following expression returns only the first element

ifelse(T, c(1,2,3), c(5,6))

However I am looking for some one-liner expression like above which
will return the entire vector.

Is there any way to achieve this?

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

Hello,

I don't like it but


ifelse(rep(T, length(c(1,2,3))), c(1,2,3), c(5,6))


maybe you should use


max(length(c(1, 2, 3)), length(5, 6)))


instead, but it's still ugly.

Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
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] if-else that returns vector

2023-10-12 Thread Jeff Newmiller via R-help
What a strange question... ifelse returns a vector (all data in R is vectors... 
some have length 1, but length zero is also possible, as are longer vectors) 
that is exactly as long as the logical vector that you give it, filled with 
elements from the respective positions in the vectors supplied in the second 
and third arguments. Because your logical vector is length 1, you only get a 
vector with the first element of the second argument.

If you want to choose between one of two vectors considered wholly, then "if" 
is what you need:

result <- if (TRUE) c(1,2,3) else c(5,6)


On October 12, 2023 1:22:03 PM PDT, Christofer Bogaso 
 wrote:
>Hi,
>
>Following expression returns only the first element
>
>ifelse(T, c(1,2,3), c(5,6))
>
>However I am looking for some one-liner expression like above which
>will return the entire vector.
>
>Is there any way to achieve this?
>
>__
>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.

-- 
Sent from my phone. Please excuse my brevity.

__
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] if-else that returns vector

2023-10-12 Thread Ben Bolker

 how about

if(T) c(1,2,3) else c(5,6)

?

On 2023-10-12 4:22 p.m., Christofer Bogaso wrote:

Hi,

Following expression returns only the first element

ifelse(T, c(1,2,3), c(5,6))

However I am looking for some one-liner expression like above which
will return the entire vector.

Is there any way to achieve this?

__
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] if-else that returns vector

2023-10-12 Thread Christofer Bogaso
Hi,

Following expression returns only the first element

ifelse(T, c(1,2,3), c(5,6))

However I am looking for some one-liner expression like above which
will return the entire vector.

Is there any way to achieve this?

__
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] unexpected 'else' in " else"

2022-10-28 Thread Ebert,Timothy Aaron
I appreciate this thread on coding. My preference for reading is to have 
complete sentences.
I can read this:
{ if (x On Behalf Of Jorgen Harmse via 
R-help
Sent: Friday, October 28, 2022 10:39 AM
To: r-help@r-project.org
Subject: Re: [R] unexpected 'else' in " else"

[External Email]

Richard O'Keefe's remarks on the workings of the interpreter are correct, but 
the code examples are ugly and hard to read. (On the other hand, anyone who has 
used the debugger may be de-sensitised to horrible code formatting.) The use of 
whitespace should if possible reflect the structure of the code, and I would 
usually rather throw in a few extra delimiters than obscure the structure.

Regards,
Jorgen Harmse.


Examples (best viewed in a real text editor so things line up):


{ if (x
To: Jinsong Zhao 
Cc: "r-help@r-project.org" 
Subject: Re: [R] unexpected 'else' in " else"
Message-ID:

Content-Type: text/plain; charset="utf-8"

...

The basic issue is that the top level wants to get started on your command AS 
SOON AS IT HAS A COMPLETE COMMAND, and if (...) stmt is complete.  It's not 
going to hang around "Waiting for Godot"
for an 'else' that might never ever ever turn up.  So
   if (x < y) z <-
   x else z <- y
is absolutely fine, no braces needed, while
   if (x < y) z <- x
   else z <- y
will see the eager top level rush off to do your bidding at the end of the 
first line and then be completely baffled by an 'else' where it does not expect 
one.

It's the same reason that you break AFTER infix operators instead of BEFORE.
   x <- y +
   z
works fine, while
   x <- y
   + z
doesn't.





[[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] unexpected 'else' in " else"

2022-10-28 Thread Jorgen Harmse via R-help
Richard O'Keefe's remarks on the workings of the interpreter are correct, but 
the code examples are ugly and hard to read. (On the other hand, anyone who has 
used the debugger may be de-sensitised to horrible code formatting.) The use of 
whitespace should if possible reflect the structure of the code, and I would 
usually rather throw in a few extra delimiters than obscure the structure.

Regards,
Jorgen Harmse.


Examples (best viewed in a real text editor so things line up):


{ if (x
To: Jinsong Zhao 
Cc: "r-help@r-project.org" 
Subject: Re: [R] unexpected 'else' in " else"
Message-ID:

Content-Type: text/plain; charset="utf-8"

...

The basic issue is that the top level wants to get started
on your command AS SOON AS IT HAS A COMPLETE COMMAND,
and if (...) stmt
is complete.  It's not going to hang around "Waiting for Godot"
for an 'else' that might never ever ever turn up.  So
   if (x < y) z <-
   x else z <- y
is absolutely fine, no braces needed, while
   if (x < y) z <- x
   else z <- y
will see the eager top level rush off to do your bidding
at the end of the first line and then be completely
baffled by an 'else' where it does not expect one.

It's the same reason that you break AFTER infix operators
instead of BEFORE.
   x <- y +
   z
works fine, while
   x <- y
   + z
doesn't.





[[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] unexpected 'else' in " else"

2022-10-26 Thread Richard O'Keefe
This is explained in books about S and R.
The first place to look is of course
> ?"if"
which says

 Note that it is a common mistake to forget to put braces ('{ ..
 }') around your statements, e.g., after 'if(..)' or 'for()'.
 In particular, you should not have a newline between '}' and
 'else' to avoid a syntax error in entering a 'if ... else'
 construct at the keyboard or via 'source'.  For that reason, one
 (somewhat extreme) attitude of defensive programming is to always
 use braces, e.g., for 'if' clauses.


The basic issue is that the top level wants to get started
on your command AS SOON AS IT HAS A COMPLETE COMMAND,
and if (...) stmt
is complete.  It's not going to hang around "Waiting for Godot"
for an 'else' that might never ever ever turn up.  So
   if (x < y) z <-
   x else z <- y
is absolutely fine, no braces needed, while
   if (x < y) z <- x
   else z <- y
will see the eager top level rush off to do your bidding
at the end of the first line and then be completely
baffled by an 'else' where it does not expect one.

It's the same reason that you break AFTER infix operators
instead of BEFORE.
   x <- y +
   z
works fine, while
   x <- y
   + z
doesn't.



On Fri, 21 Oct 2022 at 22:29, Jinsong Zhao  wrote:

> Hi there,
>
> The following code would cause R error:
>
>  > w <- 1:5
>  > r <- 1:5
>  >     if (is.matrix(r))
> + r[w != 0, , drop = FALSE]
>  > else r[w != 0]
> Error: unexpected 'else' in "else"
>
> However, the code:
>  if (is.matrix(r))
>  r[w != 0, , drop = FALSE]
>  else r[w != 0]
> is extracted from stats::weighted.residuals.
>
> My question is why the code in the function does not cause error?
>
> Best,
> Jinsong
>
> __
> 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] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Bert Gunter
I wanted to follow up.
A more careful reading of the following:
"A vector of the same length and attributes (including dimensions and
"class") as test..."

So the above **refers only to a "class" attribute that appears among
the attributes of test and result**. Using my previous example, note
that:

 z <- c(TRUE,TRUE,FALSE)
> attributes(z)
NULL ## so no 'class' among attributes(z)
## However
> class(z)  ## S3 class
[1] "logical"
## Similarly
> w <- ifelse(z,5,'a')
> attributes(w)
NULL ## so no 'class' among attributes(w)
> class(w)   ##S3 class
[1] "character"

So my (anyway) confusion stems from conflating the S3 'class' of the
object with a "class" attribute, of which there is none.

Nevertheless, I believe that the phrase I suggested (or something
along those lines) might clarify how the S3 class is determined and
perhaps better distinguish it from a "class" attribute among the
attributes, if there there is such. Or maybe that part of the doc
should just be removed.

My guess is that this documentation has been around for a long time
and no one has gotten around to revising it once S3 classes came into
wider use. ... or saw the need to revise it, anyway.

-- Bert





"

On Mon, Oct 24, 2022 at 9:30 AM Bert Gunter  wrote:
>
> "...but 'same length and attributes (including dimensions and
> ‘"class"’) as ‘test’' looks wrong. The output seems to be `logical` or
> something related to the classes of `yes` & `no`."
>
> The documentation in fact says:
> "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.**
>
> So the values are taken from 'yes' and 'no' (with coercion if they are
> of different classes), and the class of the result will presumably be
> inferred from the mode of those values. e.g.
>
> > z <- c(TRUE,TRUE,FALSE)
> > class(z)
> [1] "logical"
> > w <- ifelse(z,5,'a')
> > class(w)
> [1] "character"  ## note coercion
>
> So it would appear that the ifelse() documentation needs to be
> clarified. For example, if the above asterisked phrase were "The S3
> *class* of the answer will be inferred from the mode, where the mode
> of the answer will be coerced ..." that might resolve at least that
> bit of confusion However, that might also be incorrect -- what about
> S4 vs S3 vs Reference classes, for example (are such cases even
> possible?)? I leave resolution of these matters -- or at least their
> accurate and complete documentation -- to wiser heads.
>
> Cheers,
> Bert
>
>
>
>
> On Mon, Oct 24, 2022 at 8:45 AM Jorgen Harmse via R-help
>  wrote:
> >
> > There were several interesting points about `ifelse`. The usual behaviour 
> > seems to be that all three inputs are evaluated, and the entries of `yes` 
> > corresponding to `TRUE` in `test` are combined with the entries of `no` 
> > corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be 
> > recycled as necessary in case `test` is longer. On top of that, there seems 
> > to be some sugar that suppresses evaluations in case `all(test)` and/or 
> > `all(!test)`, and the return type can be `logical` even if `yes` & `no` are 
> > not. I agreed with the other responses already, but my experiments further 
> > confirmed that `ifelse` is not interchangeable with `if()  else 
> > `.
> >
> >
> >
> > The documentation confirms most of this, but 'same length and attributes 
> > (including dimensions and ‘"class"’) as ‘test’' looks wrong. The output 
> > seems to be `logical` or something related to the classes of `yes` & `no`.
> >
> >
> >
> > Regards,
> >
> > Jorgen Harmse.
> >
> >
> >
> > > ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > > {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 0
> >
> > > ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > > {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 0 1 2 3 4
> >
> > > ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > > {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'if'.
> >
> > [1] 1 2 3
> >
> > > ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 
> > > 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'if'.
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 1 2 2
> >
> > > ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 
> > > 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'if'.
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 1 2 2 1
> >
> > > args(ifelse)
> >
> > function (test, yes, no)
> >
> > NULL
> >
> > > ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the 
> > > vector for 'if'.\n"); 

Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Bert Gunter
"...but 'same length and attributes (including dimensions and
‘"class"’) as ‘test’' looks wrong. The output seems to be `logical` or
something related to the classes of `yes` & `no`."

The documentation in fact says:
"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.**

So the values are taken from 'yes' and 'no' (with coercion if they are
of different classes), and the class of the result will presumably be
inferred from the mode of those values. e.g.

> z <- c(TRUE,TRUE,FALSE)
> class(z)
[1] "logical"
> w <- ifelse(z,5,'a')
> class(w)
[1] "character"  ## note coercion

So it would appear that the ifelse() documentation needs to be
clarified. For example, if the above asterisked phrase were "The S3
*class* of the answer will be inferred from the mode, where the mode
of the answer will be coerced ..." that might resolve at least that
bit of confusion However, that might also be incorrect -- what about
S4 vs S3 vs Reference classes, for example (are such cases even
possible?)? I leave resolution of these matters -- or at least their
accurate and complete documentation -- to wiser heads.

Cheers,
Bert




On Mon, Oct 24, 2022 at 8:45 AM Jorgen Harmse via R-help
 wrote:
>
> There were several interesting points about `ifelse`. The usual behaviour 
> seems to be that all three inputs are evaluated, and the entries of `yes` 
> corresponding to `TRUE` in `test` are combined with the entries of `no` 
> corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be 
> recycled as necessary in case `test` is longer. On top of that, there seems 
> to be some sugar that suppresses evaluations in case `all(test)` and/or 
> `all(!test)`, and the return type can be `logical` even if `yes` & `no` are 
> not. I agreed with the other responses already, but my experiments further 
> confirmed that `ifelse` is not interchangeable with `if()  else `.
>
>
>
> The documentation confirms most of this, but 'same length and attributes 
> (including dimensions and ‘"class"’) as ‘test’' looks wrong. The output seems 
> to be `logical` or something related to the classes of `yes` & `no`.
>
>
>
> Regards,
>
> Jorgen Harmse.
>
>
>
> > ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'else'.
>
> [1] 0
>
> > ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'else'.
>
> [1] 0 1 2 3 4
>
> > ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> [1] 1 2 3
>
> > ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> Evaluating the vector for 'else'.
>
> [1] 1 2 2
>
> > ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 'if'.\n"); 
> > 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> Evaluating the vector for 'else'.
>
> [1] 1 2 2 1
>
> > args(ifelse)
>
> function (test, yes, no)
>
> NULL
>
> > ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector 
> > for 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> Evaluating the vector for 'else'.
>
> [1] 1 2 2 1 2 0 1
>
> > ifelse(logical(0L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> logical(0)
>
> > ifelse(TRUE, integer(0L), numeric(0L))
>
> [1] NA
>
> > class(ifelse(TRUE, integer(0L), numeric(0L)))
>
> [1] "integer"
>
> > ifelse(integer(0L)) # test is an empty vector of integers and yes & no are 
> > missing.
>
> logical(0)
>
>
>
>
>
>
>
>
> [[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] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Jorgen Harmse via R-help
There were several interesting points about `ifelse`. The usual behaviour seems 
to be that all three inputs are evaluated, and the entries of `yes` 
corresponding to `TRUE` in `test` are combined with the entries of `no` 
corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be recycled 
as necessary in case `test` is longer. On top of that, there seems to be some 
sugar that suppresses evaluations in case `all(test)` and/or `all(!test)`, and 
the return type can be `logical` even if `yes` & `no` are not. I agreed with 
the other responses already, but my experiments further confirmed that `ifelse` 
is not interchangeable with `if()  else `.



The documentation confirms most of this, but 'same length and attributes 
(including dimensions and �"class"�) as �test�' looks wrong. The output seems 
to be `logical` or something related to the classes of `yes` & `no`.



Regards,

Jorgen Harmse.



> ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'else'.

[1] 0

> ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'else'.

[1] 0 1 2 3 4

> ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

[1] 1 2 3

> ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2

> ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 'if'.\n"); 
> 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2 1

> args(ifelse)

function (test, yes, no)

NULL

> ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector 
> for 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2 1 2 0 1

> ifelse(logical(0L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

logical(0)

> ifelse(TRUE, integer(0L), numeric(0L))

[1] NA

> class(ifelse(TRUE, integer(0L), numeric(0L)))

[1] "integer"

> ifelse(integer(0L)) # test is an empty vector of integers and yes & no are 
> missing.

logical(0)








[[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] unexpected 'else' in " else"

2022-10-21 Thread Jeff Newmiller
Your selection priorities are inverted: ifelse is vectorised... use it to make 
many individual choices at once with a single atomic vector result. If-else is 
a control-flow construct... you can return one object of any type from it based 
on a single comparison or alter variables and ignore the return value (or both, 
though that is probably not going to make for clear readability). Their use 
cases might appear to overlap if you choose to work with a length-one vector, 
but that is a somewhat obscure mis-use of ifelse.

On October 21, 2022 10:38:48 AM PDT, "Ebert,Timothy Aaron"  
wrote:
>Is there a place where you would use ifelse()? I used it here because it was 
>short and there was no indication of lots of data. In the example I cannot 
>tell the difference of a few hundred milliseconds execution time. Benchmarking 
>code is important for larger problems.
>
>Tim
>
>-Original Message-
>From: Martin Maechler  
>Sent: Friday, October 21, 2022 8:43 AM
>To: Ebert,Timothy Aaron 
>Cc: Andrew Simmons ; Jinsong Zhao ; 
>R-help Mailing List 
>Subject: Re: [R] unexpected 'else' in " else"
>
>[External Email]
>
>>>>>> Ebert,Timothy Aaron
>>>>>> on Fri, 21 Oct 2022 12:05:58 + writes:
>
>> I can get it to work with
>> ifelse(is.matrix(r), r[w!=0, , drop=FALSE], r[w!=0])
>
>Note that this is *not* good advice:
>
>  if(Cnd) A else Bis very much more efficient  than
>  ifelse(Cnd, A, B)
>
>whenever it is appropriate, i.e.,
>the condition Cnd is a simple TRUE or FALSE.
>
>ifelse() is very much over-used!
>
>{For the more sophisticated reader:
> In R, these both are function calls:
> `if` is a function of 3 argument with a "peculiar" syntax and  the third 
> argument with default NULL.
>}
>
>Martin Maechler
>ETH Zurich  and  R Core team
>
>
>
>> With w and r as defined r is not a  matrix, so the first part will never 
> execute. The test is for w not equal to zero so it is always true for these 
> vectors. It is usually good to have test code such that all possible cases 
> are activated.
>
>> r<--1:8
>> w<- -1:5
>> if(is.matrix(r)){
>> r[w!=0, , drop=FALSE]
>> } else r[w != 0]
>
>> I think this also works. The "else" must follow the } and you put in a 
> carriage return after the }
>
>> I think that is the answer, but not 100% sure.
>
>> Tim
>
>> -Original Message-
>> From: R-help  On Behalf Of Andrew Simmons
>> Sent: Friday, October 21, 2022 5:37 AM
>> To: Jinsong Zhao 
>> Cc: R-help Mailing List 
>> Subject: Re: [R] unexpected 'else' in " else"
>
>> [External Email]
>
>> The error comes from the expression not being wrapped with braces. You 
> could change it to
>
>> if (is.matrix(r)) {
>> r[w != 0, , drop = FALSE]
>> } else r[w != 0]
>
>> or
>
>> {
>> if (is.matrix(r))
>> r[w != 0, , drop = FALSE]
>> else r[w != 0]
>> }
>
>> or
>
>> if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]
>
>
>    > On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:
>
>>> Hi there,
>>>
>>> The following code would cause R error:
>>>
>>> > w <- 1:5
>>> > r <- 1:5
>>> > if (is.matrix(r))
>>> + r[w != 0, , drop = FALSE]
>>> > else r[w != 0]
>>> Error: unexpected 'else' in "else"
>>>
>>> However, the code:
>>> if (is.matrix(r))
>>> r[w != 0, , drop = FALSE]
>>> else r[w != 0]
>>> is extracted from stats::weighted.residuals.
>>>
>>> My question is why the code in the function does not cause error?
>>>
>>> Best,
>>> Jinsong
>>>
>>> __
>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
>>> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
>>> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e
>>> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
>>> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
>>> sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved
>>> =0

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Ebert,Timothy Aaron
Is there a place where you would use ifelse()? I used it here because it was 
short and there was no indication of lots of data. In the example I cannot tell 
the difference of a few hundred milliseconds execution time. Benchmarking code 
is important for larger problems.

Tim

-Original Message-
From: Martin Maechler  
Sent: Friday, October 21, 2022 8:43 AM
To: Ebert,Timothy Aaron 
Cc: Andrew Simmons ; Jinsong Zhao ; R-help 
Mailing List 
Subject: Re: [R] unexpected 'else' in " else"

[External Email]

>>>>> Ebert,Timothy Aaron
>>>>> on Fri, 21 Oct 2022 12:05:58 + writes:

> I can get it to work with
> ifelse(is.matrix(r), r[w!=0, , drop=FALSE], r[w!=0])

Note that this is *not* good advice:

  if(Cnd) A else Bis very much more efficient  than
  ifelse(Cnd, A, B)

whenever it is appropriate, i.e.,
the condition Cnd is a simple TRUE or FALSE.

ifelse() is very much over-used!

{For the more sophisticated reader:
 In R, these both are function calls:
 `if` is a function of 3 argument with a "peculiar" syntax and  the third 
argument with default NULL.
}

Martin Maechler
ETH Zurich  and  R Core team



> With w and r as defined r is not a  matrix, so the first part will never 
execute. The test is for w not equal to zero so it is always true for these 
vectors. It is usually good to have test code such that all possible cases are 
activated.

> r<--1:8
> w<- -1:5
> if(is.matrix(r)){
> r[w!=0, , drop=FALSE]
> } else r[w != 0]

> I think this also works. The "else" must follow the } and you put in a 
carriage return after the }

> I think that is the answer, but not 100% sure.

> Tim

> -Original Message-
> From: R-help  On Behalf Of Andrew Simmons
> Sent: Friday, October 21, 2022 5:37 AM
> To: Jinsong Zhao 
> Cc: R-help Mailing List 
> Subject: Re: [R] unexpected 'else' in " else"

> [External Email]

> The error comes from the expression not being wrapped with braces. You 
could change it to

> if (is.matrix(r)) {
> r[w != 0, , drop = FALSE]
> } else r[w != 0]

> or

> {
    > if (is.matrix(r))
> r[w != 0, , drop = FALSE]
> else r[w != 0]
> }

> or

> if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]


> On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

>> Hi there,
>>
>> The following code would cause R error:
>>
>> > w <- 1:5
>> > r <- 1:5
>> > if (is.matrix(r))
    >> + r[w != 0, , drop = FALSE]
>> > else r[w != 0]
>> Error: unexpected 'else' in "else"
>>
>> However, the code:
>> if (is.matrix(r))
>> r[w != 0, , drop = FALSE]
>> else r[w != 0]
>> is extracted from stats::weighted.residuals.
>>
>> My question is why the code in the function does not cause error?
>>
>> Best,
>> Jinsong
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
>> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
>> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e
>> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
>> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
>> sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved
>> =0
>> PLEASE do read the posting guide
>> 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r%2Fdata=05%7C01%7Ctebert%40ufl.edu%7Cd3824be0a76a4e488dda08dab361d1e6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019529977132811%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=MTmSNg1tfjToCYtWtMf01Hhy93K0TpT3DPwuvYqVv0s%3Dreserved=0
>> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
>> 7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%
>> 7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
>> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
>> sdata=WsqoNk2NQ6NOjKoOKwf%2FPU57XkAwKtRhw6xb68COT1o%3Dreserved=0
>> and provide commented, minimal, self-contained, reproducible code.
>>

> [[alternative HTML version deleted]]

> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and m

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The code working inside stats::weighted.residuals has nothing to do
with being evaluated in a different environment than globalenv() and
has nothing to do with being inside a package.
The reason the code works inside stats::weighted.residuals is because
the function body is wrapped with braces. You can try it yourself:

local({
FILE <- tempfile(fileext = ".R")
on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE,
after = FALSE)
writeLines("if (TRUE) \n'evaluating cons.expr'\nelse
'evaluating alt.expr'", FILE)
writeLines(readLines(FILE))
try(source(FILE, local = TRUE, echo = TRUE, verbose = FALSE))
})

If you try entering it as a function, it still fails:

local({
FILE <- tempfile(fileext = ".R")
on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE,
after = FALSE)
writeLines("function () \nif (TRUE) \n'evaluating
cons.expr'\nelse 'evaluating alt.expr'", FILE)
writeLines(readLines(FILE))
try(source(FILE, local = TRUE, echo = TRUE, verbose = FALSE))
})

But R packages use sys.source() instead of source() to run R code, but
it still fails if you run it:

local({
FILE <- tempfile(fileext = ".R")
on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE,
after = FALSE)
writeLines("if (TRUE) \n'evaluating cons.expr'\nelse
'evaluating alt.expr'", FILE)
writeLines(readLines(FILE))
try(sys.source(FILE, envir = environment()))
})

The part that matters is that the function body is wrapped with
braces. `if` statements inside braces or parenthesis (or possibly
brackets) will continue looking for `else` even after `cons.expr` and
a newline has been fully parsed, but will not otherwise.

On Fri, Oct 21, 2022 at 10:39 AM Jorgen Harmse via R-help
 wrote:
>
> Andrew Simmons is correct but doesn't explain why the code works in the 
> package. This is one of only two differences I have found between running 
> code at the command line and running it from a file. (The other difference is 
> that code in a file is often executed in an environment other than 
> .GlobalEnv. There is some related sugar around packages that usually makes 
> things work the way a user would want.) At the command line, R executes code 
> whenever RETURN could be interpreted as the end of a statement. "If(….) …. 
> RETURN" is ambiguous: will it be followed by "else", or is it a complete 
> statement? If it's in a file or wrapped in a block or other structure that 
> obviously hasn't ended yet then R will wait to see the next line of input, 
> but if it could be a complete statement then not executing it would cause a 
> lot of frustration for users. Once the statement is executed, R expects 
> another statement, and no statement begins with "else". (Maybe the 
> interpreter could be enhanced to keep the "if" open under some conditions, 
> but I haven't thought it through. In particular, "if" without "else" is NULL 
> if the condition is FALSE, so it might be necessary to undo an assignment, 
> and that seems very difficult.)
>
> Regards,
> Jorgen Harmse.
>
>
> On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:
>
> > Hi there,
> >
> > The following code would cause R error:
> >
> >  > w <- 1:5
> >  > r <- 1:5
> >  >     if (is.matrix(r))
> > + r[w != 0, , drop = FALSE]
> >  > else r[w != 0]
> > Error: unexpected 'else' in "else"
> >
> > However, the code:
> >  if (is.matrix(r))
> >  r[w != 0, , drop = FALSE]
> >  else r[w != 0]
> > is extracted from stats::weighted.residuals.
> >
> > My question is why the code in the function does not cause error?
> >
> > Best,
> > Jinsong
> >
> > __
> > 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]]
>
>
>
>
> --
>
> Subject: Digest Footer
>
> ___
> 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.
>
>
> --
>
> End of R-help Digest, Vol 236, Issue 19
> ***
>
>

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jorgen Harmse via R-help
Andrew Simmons is correct but doesn't explain why the code works in the 
package. This is one of only two differences I have found between running code 
at the command line and running it from a file. (The other difference is that 
code in a file is often executed in an environment other than .GlobalEnv. There 
is some related sugar around packages that usually makes things work the way a 
user would want.) At the command line, R executes code whenever RETURN could be 
interpreted as the end of a statement. "If(�.) �. RETURN" is ambiguous: will it 
be followed by "else", or is it a complete statement? If it's in a file or 
wrapped in a block or other structure that obviously hasn't ended yet then R 
will wait to see the next line of input, but if it could be a complete 
statement then not executing it would cause a lot of frustration for users. 
Once the statement is executed, R expects another statement, and no statement 
begins with "else". (Maybe the interpreter could be enhanced to keep the "if" 
open under some conditions, but I haven't thought it through. In particular, 
"if" without "else" is NULL if the condition is FALSE, so it might be necessary 
to undo an assignment, and that seems very difficult.)

Regards,
Jorgen Harmse.


On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

> Hi there,
>
> The following code would cause R error:
>
>  > w <- 1:5
>  > r <- 1:5
>  > if (is.matrix(r))
> + r[w != 0, , drop = FALSE]
>  > else r[w != 0]
> Error: unexpected 'else' in "    else"
>
> However, the code:
>  if (is.matrix(r))
>  r[w != 0, , drop = FALSE]
>  else r[w != 0]
> is extracted from stats::weighted.residuals.
>
> My question is why the code in the function does not cause error?
>
> Best,
> Jinsong
>
> __
> 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]]




--

Subject: Digest Footer

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


--

End of R-help Digest, Vol 236, Issue 19
***

[[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] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao

Dear John,

Thank you very much for the explanation. It cleared up my confusion 
about the syntax of "if ... else...", which in the help page of "if" said:

```
In particular, you should not have a newline between ‘}’ and
 ‘else’ to avoid a syntax error in entering a ‘if ... else’
 construct at the keyboard or via ‘source’.
```

Best,
Jinsong


On 2022/10/21 21:49, John Fox wrote:

Dear Jinsong,

When you enter these code lines at the R command prompt, the 
interpreter evaluates an expression when it's syntactically complete, 
which occurs before it sees the else clause. The interpreter can't 
read your mind and know that an else clause will be entered on the 
next line. When the code lines are in a function, the function body is 
enclosed in braces and so the interpreter sees the else clause.


As I believe was already pointed out, you can similarly use braces at 
the command prompt to signal incompleteness of an expression, as in


> {if (FALSE) print(1)
+ else print(2)}
[1] 2

I hope this helps,
 John



__
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] unexpected 'else' in " else"

2022-10-21 Thread John Fox

Dear Jinsong,

When you enter these code lines at the R command prompt, the interpreter 
evaluates an expression when it's syntactically complete, which occurs 
before it sees the else clause. The interpreter can't read your mind and 
know that an else clause will be entered on the next line. When the code 
lines are in a function, the function body is enclosed in braces and so 
the interpreter sees the else clause.


As I believe was already pointed out, you can similarly use braces at 
the command prompt to signal incompleteness of an expression, as in


> {if (FALSE) print(1)
+ else print(2)}
[1] 2

I hope this helps,
 John

--
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/
On 2022-10-21 8:06 a.m., Jinsong Zhao wrote:

Thanks a lot!

I know the first and third way to correct the error. The second way 
seems make me know why the code is correct in the function 
stats::weighted.residuals.


On 2022/10/21 17:36, Andrew Simmons wrote:
The error comes from the expression not being wrapped with braces. You 
could change it to


if (is.matrix(r)) {
    r[w != 0, , drop = FALSE]
} else r[w != 0]

or

{
    if (is.matrix(r))
        r[w != 0, , drop = FALSE]
    else r[w != 0]
}

or

if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]


On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

    Hi there,

    The following code would cause R error:

     > w <- 1:5
     > r <- 1:5
     >         if (is.matrix(r))
    +             r[w != 0, , drop = FALSE]
     >         else r[w != 0]
    Error: unexpected 'else' in "        else"

    However, the code:
             if (is.matrix(r))
                 r[w != 0, , drop = FALSE]
             else r[w != 0]
    is extracted from stats::weighted.residuals.

    My question is why the code in the function does not cause error?

    Best,
    Jinsong

    __
    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
    <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-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] unexpected 'else' in " else"

2022-10-21 Thread Martin Maechler
>>>>> Ebert,Timothy Aaron 
>>>>> on Fri, 21 Oct 2022 12:05:58 + writes:

> I can get it to work with
> ifelse(is.matrix(r), r[w!=0, , drop=FALSE], r[w!=0])

Note that this is *not* good advice:

  if(Cnd) A else Bis very much more efficient  than
  ifelse(Cnd, A, B)

whenever it is appropriate, i.e.,
the condition Cnd is a simple TRUE or FALSE.

ifelse() is very much over-used!

{For the more sophisticated reader:
 In R, these both are function calls:
 `if` is a function of 3 argument with a "peculiar" syntax and
 the third argument with default NULL.
}

Martin Maechler
ETH Zurich  and  R Core team



> With w and r as defined r is not a  matrix, so the first part will never 
execute. The test is for w not equal to zero so it is always true for these 
vectors. It is usually good to have test code such that all possible cases are 
activated.

> r<--1:8
> w<- -1:5
    > if(is.matrix(r)){
> r[w!=0, , drop=FALSE]
> } else r[w != 0]

> I think this also works. The "else" must follow the } and you put in a 
carriage return after the }

> I think that is the answer, but not 100% sure.

> Tim

> -Original Message-
> From: R-help  On Behalf Of Andrew Simmons
> Sent: Friday, October 21, 2022 5:37 AM
> To: Jinsong Zhao 
> Cc: R-help Mailing List 
> Subject: Re: [R] unexpected 'else' in " else"

> [External Email]

> The error comes from the expression not being wrapped with braces. You 
could change it to

> if (is.matrix(r)) {
> r[w != 0, , drop = FALSE]
> } else r[w != 0]

> or

> {
> if (is.matrix(r))
> r[w != 0, , drop = FALSE]
> else r[w != 0]
> }

> or

> if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]


> On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

>> Hi there,
>> 
>> The following code would cause R error:
>> 
>> > w <- 1:5
>> > r <- 1:5
>> > if (is.matrix(r))
>> +     r[w != 0, , drop = FALSE]
>> > else r[w != 0]
>> Error: unexpected 'else' in "else"
>> 
>> However, the code:
>> if (is.matrix(r))
>> r[w != 0, , drop = FALSE]
>> else r[w != 0]
>> is extracted from stats::weighted.residuals.
>> 
>> My question is why the code in the function does not cause error?
>> 
>> Best,
>> Jinsong
>> 
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
>> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
>> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e
>> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
>> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
>> sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved
>> =0
>> PLEASE do read the posting guide
>> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
>> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
>> 7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%
>> 7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
>> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
>> sdata=WsqoNk2NQ6NOjKoOKwf%2FPU57XkAwKtRhw6xb68COT1o%3Dreserved=0
>> 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=2mYpa25vpXNvPNINi9XxCtc7Vbj4Sp4IQUsA9fL%2BA60%3Dreserved=0
> PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=21L

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao

Thanks a lot!

I know the first and third way to correct the error. The second way 
seems make me know why the code is correct in the function 
stats::weighted.residuals.


On 2022/10/21 17:36, Andrew Simmons wrote:
The error comes from the expression not being wrapped with braces. You 
could change it to


if (is.matrix(r)) {
    r[w != 0, , drop = FALSE]
} else r[w != 0]

or

{
    if (is.matrix(r))
        r[w != 0, , drop = FALSE]
    else r[w != 0]
}

or

if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]


On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

Hi there,

The following code would cause R error:

 > w <- 1:5
 > r <- 1:5
 >         if (is.matrix(r))
+             r[w != 0, , drop = FALSE]
 >         else r[w != 0]
Error: unexpected 'else' in "        else"

However, the code:
         if (is.matrix(r))
             r[w != 0, , drop = FALSE]
         else r[w != 0]
is extracted from stats::weighted.residuals.

My question is why the code in the function does not cause error?

Best,
Jinsong

__
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
<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] unexpected 'else' in " else"

2022-10-21 Thread Ebert,Timothy Aaron
I can get it to work with
ifelse(is.matrix(r), r[w!=0, , drop=FALSE], r[w!=0])

With w and r as defined r is not a  matrix, so the first part will never 
execute. The test is for w not equal to zero so it is always true for these 
vectors. It is usually good to have test code such that all possible cases are 
activated.

r<--1:8
w<- -1:5
if(is.matrix(r)){
  r[w!=0, , drop=FALSE]
} else r[w != 0]

I think this also works. The "else" must follow the } and you put in a carriage 
return after the }

I think that is the answer, but not 100% sure.

Tim

-Original Message-
From: R-help  On Behalf Of Andrew Simmons
Sent: Friday, October 21, 2022 5:37 AM
To: Jinsong Zhao 
Cc: R-help Mailing List 
Subject: Re: [R] unexpected 'else' in " else"

[External Email]

The error comes from the expression not being wrapped with braces. You could 
change it to

if (is.matrix(r)) {
r[w != 0, , drop = FALSE]
} else r[w != 0]

or

{
if (is.matrix(r))
r[w != 0, , drop = FALSE]
else r[w != 0]
}

or

if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]


On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

> Hi there,
>
> The following code would cause R error:
>
>  > w <- 1:5
>  > r <- 1:5
>  > if (is.matrix(r))
> + r[w != 0, , drop = FALSE]
>  > else r[w != 0]
> Error: unexpected 'else' in "else"
>
> However, the code:
>  if (is.matrix(r))
>  r[w != 0, , drop = FALSE]
>  else r[w != 0]
> is extracted from stats::weighted.residuals.
>
> My question is why the code in the function does not cause error?
>
> Best,
> Jinsong
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved
> =0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=WsqoNk2NQ6NOjKoOKwf%2FPU57XkAwKtRhw6xb68COT1o%3Dreserved=0
> 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=2mYpa25vpXNvPNINi9XxCtc7Vbj4Sp4IQUsA9fL%2BA60%3Dreserved=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=21LO4e%2BNnGmlDjm3XfExMX63Z2viUePxMaHbcgm7JuA%3Dreserved=0
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] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The error comes from the expression not being wrapped with braces. You
could change it to

if (is.matrix(r)) {
r[w != 0, , drop = FALSE]
} else r[w != 0]

or

{
if (is.matrix(r))
r[w != 0, , drop = FALSE]
else r[w != 0]
}

or

if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0]


On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao,  wrote:

> Hi there,
>
> The following code would cause R error:
>
>  > w <- 1:5
>  > r <- 1:5
>  > if (is.matrix(r))
> + r[w != 0, , drop = FALSE]
>  > else r[w != 0]
> Error: unexpected 'else' in "else"
>
> However, the code:
>  if (is.matrix(r))
>  r[w != 0, , drop = FALSE]
>  else r[w != 0]
> is extracted from stats::weighted.residuals.
>
> My question is why the code in the function does not cause error?
>
> Best,
> Jinsong
>
> __
> 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.


[R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao

Hi there,

The following code would cause R error:

> w <- 1:5
> r <- 1:5
> if (is.matrix(r))
+ r[w != 0, , drop = FALSE]
> else r[w != 0]
Error: unexpected 'else' in "else"

However, the code:
if (is.matrix(r))
r[w != 0, , drop = FALSE]
else r[w != 0]
is extracted from stats::weighted.residuals.

My question is why the code in the function does not cause error?

Best,
Jinsong

__
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] if else statement adjustemtn

2020-06-15 Thread Ana Marija
HI Jim

thank you so much! This is amazing answer!!!

Ana

On Sat, Jun 13, 2020 at 4:09 AM Jim Lemon  wrote:
>
> Right, back from shopping. Since you have fourteen rows containing NAs
> and you only want seven, we can infer that half of them must go. As
> they are neatly divided into seven rows in which only one NA appears
> and seven in which two stare meaninglessly out at us. I will assume
> that the latter are the ones to be discarded. As your condition for
> calculating "pheno" stated that a 2 in either FLASER or PLASER should
> result in a 2 in pheno, the following statement closely conforms to
> that:
>
> b<-read.table(text="FID   IID FLASER PLASER
>   fam1837 G1837  1 NA
>   fam2410 G2410 NA NA
>   fam2838 G2838 NA  2
>   fam3367 G3367  1 NA
>   fam3410 G3410  1 NA
>   fam3492 G3492  1 NA
>   fam0911  G911 NA NA
>   fam3834 G3834  2 NA
>   fam4708 G4708 NA  2
>   fam5162 G5162 NA NA
>   fam5274 G5274 NA NA
>   fam0637  G637 NA NA
>   fam0640  G640 NA NA
>   fam0743  G743 NA NA
>   fam0911  G911 NA NA",
>   header=TRUE,stringsAsFactors=FALSE)
>
> b$pheno<-ifelse(b$FLASER == 2 | b$PLASER == 2,2,1)
> # use the valid FLASER values when PLASER is NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$FLASER),
>  b[is.na(b$pheno),]$FLASER,NA)
> # use the valid PLASER values when FLASER if NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$PLASER),
>  b[is.na(b$pheno),]$PLASER,NA)
> b
>
> I could write that mess in one straitjacket of conditional statements
> but my brain hurts enough.
>
> Jim
>
>
> On Sat, Jun 13, 2020 at 1:59 PM Ana Marija  
> wrote:
> >
> > Great idea!
> > Here it is:
> > > b[is.na(b$FLASER) | is.na(b$PLASER),]
> > FID   IID FLASER PLASER pheno
> >  1: fam1837 G1837  1 NA 2
> >  2: fam2410 G2410 NA NA 2
> >  3: fam2838 G2838 NA  2 2
> >  4: fam3367 G3367  1 NA 2
> >  5: fam3410 G3410  1 NA 2
> >  6: fam3492 G3492  1 NA 2
> >  7: fam3834 G3834  2 NA 2
> >  8: fam4708 G4708 NA  2 2
> >  9: fam5162 G5162 NA NA 2
> > 10: fam5274 G5274 NA NA 2
> > 11: fam0637  G637 NA NA 2
> > 12: fam0640  G640 NA NA 2
> > 13: fam0743  G743 NA NA 2
> > 14: fam0911  G911 NA NA 2
> >
> > On Fri, Jun 12, 2020 at 10:29 PM Jim Lemon  wrote:
> > >
> > > Since you have only a few troublesome NA values, if you look at them,
> > > or even better, post them:
> > >
> > > b[is.na(b$FLASER) | is.na(b$PLASER),]
> > >
> > > perhaps we can work out the appropriate logic to get rid of only the
> > > ones you don't want.
> > >
> > > Jim
> > >
> > > On Sat, Jun 13, 2020 at 12:50 PM Ana Marija  
> > > wrote:
> > > >
> > > > Hi Rasmus,
> > > >
> > > > thank you for getting back to be, the command your provided seems to
> > > > add all 11 NAs to 2s
> > > > > b$pheno <-
> > > > +   ifelse(b$PLASER==2 |
> > > > +  b$FLASER==2 |
> > > > +  is.na(b$PLASER) |
> > > > +  is.na(b$PLASER) & b$FLASER %in% 1:2 |
> > > > +  is.na(b$FLASER) & b$PLASER == 2,
> > > > +  2, 1)
> > > > > table(b$pheno, exclude = NULL)
> > > >
> > > >   1   2
> > > > 859 839
> > > >
> > > > Once again my desired results is to keep these 7 NAs as NAs
> > > > > table(b$PLASER,b$FLASER, exclude = NULL)
> > > >
> > > >  1   2   3 
> > > >   1836  14   00
> > > >   2691  70  432
> > > >   3  2   7  210
> > > >  4   1   07
> > > >
> > > > and have
> > > > 825 2s (825=691+14+70+7+43)
> > > > and the rest would be 1s (866=1698-7-825)
> > > >
> > > > On Fri, Jun 12, 2020 at 9:29 PM Rasmus Liland  wrote:
> > > > >
> > > > > On 2020-06-13 11:30 +1000, Jim Lemon wrote:
> > > > > > On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon wrote:
> > > > > > > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija wrote:
> > > > > > > >
> > > > > > > > I am trying to make a new column
> > > > > > > > "pheno" so that I reduce the number
> > > > > > > > of NAs
> > > > > > >
> > > > > > > it looks like those two NA values in
> > > > > > > PLASER are the ones you want to drop.
> > > > > >
> > > > > > From just your summary table, it's hard to
> > > > > > guess the distribution of NA values.
> > > > >
> > > > > Dear Ana,
> > > > >
> > > > > This small sample
> > > > >
> > > > > b <- read.table(text="FLASER;PLASER
> > > > > 1;2
> > > > > ;2
> > > > > ;
> > > > > 1;
> > > > > 2;
> > > > > 2;2
> > > > > 3;2
> > > > > 3;3
> > > > > 1;1", sep=";", header=TRUE)
> > > > >
> > > > > table(b$PLASER,b$FLASER, exclude = NULL)
> > > > >
> > > > > yields the same combinations you showed
> > > > > earlier:
> > > > >
> > > > >1 2 3 
> > > > >   11 0 00
> > > 

Re: [R] if else statement adjustemtn

2020-06-15 Thread Rasmus Liland
On 2020-06-13 19:09 +1000, Jim Lemon wrote:
> Right, back from shopping. Since you have fourteen rows containing NAs
> and you only want seven, we can infer that half of them must go. As
> they are neatly divided into seven rows in which only one NA appears
> and seven in which two stare meaninglessly out at us. I will assume
> that the latter are the ones to be discarded. As your condition for
> calculating "pheno" stated that a 2 in either FLASER or PLASER should
> result in a 2 in pheno, the following statement closely conforms to
> that:
> 
> b<-read.table(text="FID   IID FLASER PLASER
>   fam1837 G1837  1 NA
>   fam2410 G2410 NA NA
>   fam2838 G2838 NA  2
>   fam3367 G3367  1 NA
>   fam3410 G3410  1 NA
>   fam3492 G3492  1 NA
>   fam0911  G911 NA NA
>   fam3834 G3834  2 NA
>   fam4708 G4708 NA  2
>   fam5162 G5162 NA NA
>   fam5274 G5274 NA NA
>   fam0637  G637 NA NA
>   fam0640  G640 NA NA
>   fam0743  G743 NA NA
>   fam0911  G911 NA NA",
>   header=TRUE,stringsAsFactors=FALSE)
> 
> b$pheno<-ifelse(b$FLASER == 2 | b$PLASER == 2,2,1)
> # use the valid FLASER values when PLASER is NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$FLASER),
>  b[is.na(b$pheno),]$FLASER,NA)
> # use the valid PLASER values when FLASER if NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$PLASER),
>  b[is.na(b$pheno),]$PLASER,NA)
> b
> 
> I could write that mess in one straitjacket of conditional statements
> but my brain hurts enough.

I think this answer is billiant!

Here's a picture of my laptop screen out in my local park 
yesterday as I was trying to figure this out

https://mega.nz/#!OERQFSgL!McUkMYwkrcQXN148Wr11K1xRHSTOWVFfz4gRwaZYLzM

I have nothing more to add at the moment :)

/Rasmus


signature.asc
Description: PGP signature
__
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] if else statement adjustemtn

2020-06-13 Thread Márk Szalai
Dear Ana,

pmax could also fit here.
pmax(b$FLASER, b$PLASER, na.rm = TRUE)

Bests,
Mark



> --
>
> Message: 21
> Date: Sat, 13 Jun 2020 19:09:11 +1000
> From: Jim Lemon 
> To: sokovic.anamar...@gmail.com
> Cc: Rasmus Liland , r-help 
> Subject: Re: [R] if else statement adjustemtn
> Message-ID:
> 
> Content-Type: text/plain; charset="utf-8"
>
> Right, back from shopping. Since you have fourteen rows containing NAs
> and you only want seven, we can infer that half of them must go. As
> they are neatly divided into seven rows in which only one NA appears
> and seven in which two stare meaninglessly out at us. I will assume
> that the latter are the ones to be discarded. As your condition for
> calculating "pheno" stated that a 2 in either FLASER or PLASER should
> result in a 2 in pheno, the following statement closely conforms to
> that:
>
> b<-read.table(text="FID   IID FLASER PLASER
>   fam1837 G1837  1 NA
>   fam2410 G2410 NA NA
>   fam2838 G2838 NA  2
>   fam3367 G3367  1 NA
>   fam3410 G3410  1 NA
>   fam3492 G3492  1 NA
>   fam0911  G911 NA NA
>   fam3834 G3834  2 NA
>   fam4708 G4708 NA  2
>   fam5162 G5162 NA NA
>   fam5274 G5274 NA NA
>   fam0637  G637 NA NA
>   fam0640  G640 NA NA
>   fam0743  G743 NA NA
>   fam0911  G911 NA NA",
>   header=TRUE,stringsAsFactors=FALSE)
>
> b$pheno<-ifelse(b$FLASER == 2 | b$PLASER == 2,2,1)
> # use the valid FLASER values when PLASER is NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$FLASER),
>  b[is.na(b$pheno),]$FLASER,NA)
> # use the valid PLASER values when FLASER if NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$PLASER),
>  b[is.na(b$pheno),]$PLASER,NA)
> b
>
> I could write that mess in one straitjacket of conditional statements
> but my brain hurts enough.
>
> Jim
>
>
> On Sat, Jun 13, 2020 at 1:59 PM Ana Marija  
> wrote:
> >
> > Great idea!
> > Here it is:
> > > b[is.na(b$FLASER) | is.na(b$PLASER),]
> > FID   IID FLASER PLASER pheno
> >  1: fam1837 G1837  1 NA 2
> >  2: fam2410 G2410 NA NA 2
> >  3: fam2838 G2838 NA  2 2
> >  4: fam3367 G3367  1 NA 2
> >  5: fam3410 G3410  1 NA 2
> >  6: fam3492 G3492  1 NA 2
> >  7: fam3834 G3834  2 NA 2
> >  8: fam4708 G4708 NA  2 2
> >  9: fam5162 G5162 NA NA 2
> > 10: fam5274 G5274 NA NA 2
> > 11: fam0637  G637 NA NA 2
> > 12: fam0640  G640 NA NA 2
> > 13: fam0743  G743 NA NA 2
> > 14: fam0911  G911 NA NA 2
> >
> > On Fri, Jun 12, 2020 at 10:29 PM Jim Lemon  wrote:
> > >
> > > Since you have only a few troublesome NA values, if you look at them,
> > > or even better, post them:
> > >
> > > b[is.na(b$FLASER) | is.na(b$PLASER),]
> > >
> > > perhaps we can work out the appropriate logic to get rid of only the
> > > ones you don't want.
> > >
> > > Jim
> > >
> > > On Sat, Jun 13, 2020 at 12:50 PM Ana Marija  
> > > wrote:
> > > >
> > > > Hi Rasmus,
> > > >
> > > > thank you for getting back to be, the command your provided seems to
> > > > add all 11 NAs to 2s
> > > > > b$pheno <-
> > > > +   ifelse(b$PLASER==2 |
> > > > +  b$FLASER==2 |
> > > > +  is.na(b$PLASER) |
> > > > +  is.na(b$PLASER) & b$FLASER %in% 1:2 |
> > > > +  is.na(b$FLASER) & b$PLASER == 2,
> > > > +  2, 1)
> > > > > table(b$pheno, exclude = NULL)
> > > >
> > > >   1   2
> > > > 859 839
> > > >
> > > > Once again my desired results is to keep these 7 NAs as NAs
> > > > > table(b$PLASER,b$FLASER, exclude = NULL)
> > > >
> > > >  1   2   3 
> > > >   1836  14   00
> > > >   2691  70  432
> > > >   3  2   7  210
> > > >  4   1   07
> > > >
> > > > and have
> > > > 825 2s (825=691+14+70+7+43)
> > > > and the rest would be 1s (866=1698-7-825)
> > > >
> > > > On Fri, Jun 12, 2020 at 9:29 PM Rasmus Liland  wrote:
> > > &g

Re: [R] if else statement adjustemtn

2020-06-13 Thread Jim Lemon
Right, back from shopping. Since you have fourteen rows containing NAs
and you only want seven, we can infer that half of them must go. As
they are neatly divided into seven rows in which only one NA appears
and seven in which two stare meaninglessly out at us. I will assume
that the latter are the ones to be discarded. As your condition for
calculating "pheno" stated that a 2 in either FLASER or PLASER should
result in a 2 in pheno, the following statement closely conforms to
that:

b<-read.table(text="FID   IID FLASER PLASER
  fam1837 G1837  1 NA
  fam2410 G2410 NA NA
  fam2838 G2838 NA  2
  fam3367 G3367  1 NA
  fam3410 G3410  1 NA
  fam3492 G3492  1 NA
  fam0911  G911 NA NA
  fam3834 G3834  2 NA
  fam4708 G4708 NA  2
  fam5162 G5162 NA NA
  fam5274 G5274 NA NA
  fam0637  G637 NA NA
  fam0640  G640 NA NA
  fam0743  G743 NA NA
  fam0911  G911 NA NA",
  header=TRUE,stringsAsFactors=FALSE)

b$pheno<-ifelse(b$FLASER == 2 | b$PLASER == 2,2,1)
# use the valid FLASER values when PLASER is NA
b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$FLASER),
 b[is.na(b$pheno),]$FLASER,NA)
# use the valid PLASER values when FLASER if NA
b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$PLASER),
 b[is.na(b$pheno),]$PLASER,NA)
b

I could write that mess in one straitjacket of conditional statements
but my brain hurts enough.

Jim


On Sat, Jun 13, 2020 at 1:59 PM Ana Marija  wrote:
>
> Great idea!
> Here it is:
> > b[is.na(b$FLASER) | is.na(b$PLASER),]
> FID   IID FLASER PLASER pheno
>  1: fam1837 G1837  1 NA 2
>  2: fam2410 G2410 NA NA 2
>  3: fam2838 G2838 NA  2 2
>  4: fam3367 G3367  1 NA 2
>  5: fam3410 G3410  1 NA 2
>  6: fam3492 G3492  1 NA 2
>  7: fam3834 G3834  2 NA 2
>  8: fam4708 G4708 NA  2 2
>  9: fam5162 G5162 NA NA 2
> 10: fam5274 G5274 NA NA 2
> 11: fam0637  G637 NA NA 2
> 12: fam0640  G640 NA NA 2
> 13: fam0743  G743 NA NA 2
> 14: fam0911  G911 NA NA 2
>
> On Fri, Jun 12, 2020 at 10:29 PM Jim Lemon  wrote:
> >
> > Since you have only a few troublesome NA values, if you look at them,
> > or even better, post them:
> >
> > b[is.na(b$FLASER) | is.na(b$PLASER),]
> >
> > perhaps we can work out the appropriate logic to get rid of only the
> > ones you don't want.
> >
> > Jim
> >
> > On Sat, Jun 13, 2020 at 12:50 PM Ana Marija  
> > wrote:
> > >
> > > Hi Rasmus,
> > >
> > > thank you for getting back to be, the command your provided seems to
> > > add all 11 NAs to 2s
> > > > b$pheno <-
> > > +   ifelse(b$PLASER==2 |
> > > +  b$FLASER==2 |
> > > +  is.na(b$PLASER) |
> > > +  is.na(b$PLASER) & b$FLASER %in% 1:2 |
> > > +  is.na(b$FLASER) & b$PLASER == 2,
> > > +  2, 1)
> > > > table(b$pheno, exclude = NULL)
> > >
> > >   1   2
> > > 859 839
> > >
> > > Once again my desired results is to keep these 7 NAs as NAs
> > > > table(b$PLASER,b$FLASER, exclude = NULL)
> > >
> > >  1   2   3 
> > >   1836  14   00
> > >   2691  70  432
> > >   3  2   7  210
> > >  4   1   07
> > >
> > > and have
> > > 825 2s (825=691+14+70+7+43)
> > > and the rest would be 1s (866=1698-7-825)
> > >
> > > On Fri, Jun 12, 2020 at 9:29 PM Rasmus Liland  wrote:
> > > >
> > > > On 2020-06-13 11:30 +1000, Jim Lemon wrote:
> > > > > On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon wrote:
> > > > > > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija wrote:
> > > > > > >
> > > > > > > I am trying to make a new column
> > > > > > > "pheno" so that I reduce the number
> > > > > > > of NAs
> > > > > >
> > > > > > it looks like those two NA values in
> > > > > > PLASER are the ones you want to drop.
> > > > >
> > > > > From just your summary table, it's hard to
> > > > > guess the distribution of NA values.
> > > >
> > > > Dear Ana,
> > > >
> > > > This small sample
> > > >
> > > > b <- read.table(text="FLASER;PLASER
> > > > 1;2
> > > > ;2
> > > > ;
> > > > 1;
> > > > 2;
> > > > 2;2
> > > > 3;2
> > > > 3;3
> > > > 1;1", sep=";", header=TRUE)
> > > >
> > > > table(b$PLASER,b$FLASER, exclude = NULL)
> > > >
> > > > yields the same combinations you showed
> > > > earlier:
> > > >
> > > >1 2 3 
> > > >   11 0 00
> > > >   21 1 11
> > > >   30 0 10
> > > >1 1 01
> > > >
> > > > If you want to eliminate the four -based
> > > > combinations completely, this line
> > > >
> > > > b$pheno <-
> > > >   ifelse(b$PLASER==2 |
> > > >  b$FLASER==2 |
> > > >  is.na(b$PLASER) |
> > > >  is.na(b$PLASER) & 

Re: [R] if else statement adjustemtn

2020-06-12 Thread Ana Marija
Great idea!
Here it is:
> b[is.na(b$FLASER) | is.na(b$PLASER),]
FID   IID FLASER PLASER pheno
 1: fam1837 G1837  1 NA 2
 2: fam2410 G2410 NA NA 2
 3: fam2838 G2838 NA  2 2
 4: fam3367 G3367  1 NA 2
 5: fam3410 G3410  1 NA 2
 6: fam3492 G3492  1 NA 2
 7: fam3834 G3834  2 NA 2
 8: fam4708 G4708 NA  2 2
 9: fam5162 G5162 NA NA 2
10: fam5274 G5274 NA NA 2
11: fam0637  G637 NA NA 2
12: fam0640  G640 NA NA 2
13: fam0743  G743 NA NA 2
14: fam0911  G911 NA NA 2

On Fri, Jun 12, 2020 at 10:29 PM Jim Lemon  wrote:
>
> Since you have only a few troublesome NA values, if you look at them,
> or even better, post them:
>
> b[is.na(b$FLASER) | is.na(b$PLASER),]
>
> perhaps we can work out the appropriate logic to get rid of only the
> ones you don't want.
>
> Jim
>
> On Sat, Jun 13, 2020 at 12:50 PM Ana Marija  
> wrote:
> >
> > Hi Rasmus,
> >
> > thank you for getting back to be, the command your provided seems to
> > add all 11 NAs to 2s
> > > b$pheno <-
> > +   ifelse(b$PLASER==2 |
> > +  b$FLASER==2 |
> > +  is.na(b$PLASER) |
> > +  is.na(b$PLASER) & b$FLASER %in% 1:2 |
> > +  is.na(b$FLASER) & b$PLASER == 2,
> > +  2, 1)
> > > table(b$pheno, exclude = NULL)
> >
> >   1   2
> > 859 839
> >
> > Once again my desired results is to keep these 7 NAs as NAs
> > > table(b$PLASER,b$FLASER, exclude = NULL)
> >
> >  1   2   3 
> >   1836  14   00
> >   2691  70  432
> >   3  2   7  210
> >  4   1   07
> >
> > and have
> > 825 2s (825=691+14+70+7+43)
> > and the rest would be 1s (866=1698-7-825)
> >
> > On Fri, Jun 12, 2020 at 9:29 PM Rasmus Liland  wrote:
> > >
> > > On 2020-06-13 11:30 +1000, Jim Lemon wrote:
> > > > On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon wrote:
> > > > > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija wrote:
> > > > > >
> > > > > > I am trying to make a new column
> > > > > > "pheno" so that I reduce the number
> > > > > > of NAs
> > > > >
> > > > > it looks like those two NA values in
> > > > > PLASER are the ones you want to drop.
> > > >
> > > > From just your summary table, it's hard to
> > > > guess the distribution of NA values.
> > >
> > > Dear Ana,
> > >
> > > This small sample
> > >
> > > b <- read.table(text="FLASER;PLASER
> > > 1;2
> > > ;2
> > > ;
> > > 1;
> > > 2;
> > > 2;2
> > > 3;2
> > > 3;3
> > > 1;1", sep=";", header=TRUE)
> > >
> > > table(b$PLASER,b$FLASER, exclude = NULL)
> > >
> > > yields the same combinations you showed
> > > earlier:
> > >
> > >1 2 3 
> > >   11 0 00
> > >   21 1 11
> > >   30 0 10
> > >1 1 01
> > >
> > > If you want to eliminate the four -based
> > > combinations completely, this line
> > >
> > > b$pheno <-
> > >   ifelse(b$PLASER==2 |
> > >  b$FLASER==2 |
> > >  is.na(b$PLASER) |
> > >  is.na(b$PLASER) & b$FLASER %in% 1:2 |
> > >  is.na(b$FLASER) & b$PLASER == 2,
> > >  2, 1)
> > > table(b$pheno, exclude = NULL)
> > >
> > > will do it:
> > >
> > > 1 2
> > > 2 7
> > >
> > > Best,
> > > Rasmus
> > > __
> > > 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-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] if else statement adjustemtn

2020-06-12 Thread Jim Lemon
Since you have only a few troublesome NA values, if you look at them,
or even better, post them:

b[is.na(b$FLASER) | is.na(b$PLASER),]

perhaps we can work out the appropriate logic to get rid of only the
ones you don't want.

Jim

On Sat, Jun 13, 2020 at 12:50 PM Ana Marija  wrote:
>
> Hi Rasmus,
>
> thank you for getting back to be, the command your provided seems to
> add all 11 NAs to 2s
> > b$pheno <-
> +   ifelse(b$PLASER==2 |
> +  b$FLASER==2 |
> +  is.na(b$PLASER) |
> +  is.na(b$PLASER) & b$FLASER %in% 1:2 |
> +  is.na(b$FLASER) & b$PLASER == 2,
> +  2, 1)
> > table(b$pheno, exclude = NULL)
>
>   1   2
> 859 839
>
> Once again my desired results is to keep these 7 NAs as NAs
> > table(b$PLASER,b$FLASER, exclude = NULL)
>
>  1   2   3 
>   1836  14   00
>   2691  70  432
>   3  2   7  210
>  4   1   07
>
> and have
> 825 2s (825=691+14+70+7+43)
> and the rest would be 1s (866=1698-7-825)
>
> On Fri, Jun 12, 2020 at 9:29 PM Rasmus Liland  wrote:
> >
> > On 2020-06-13 11:30 +1000, Jim Lemon wrote:
> > > On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon wrote:
> > > > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija wrote:
> > > > >
> > > > > I am trying to make a new column
> > > > > "pheno" so that I reduce the number
> > > > > of NAs
> > > >
> > > > it looks like those two NA values in
> > > > PLASER are the ones you want to drop.
> > >
> > > From just your summary table, it's hard to
> > > guess the distribution of NA values.
> >
> > Dear Ana,
> >
> > This small sample
> >
> > b <- read.table(text="FLASER;PLASER
> > 1;2
> > ;2
> > ;
> > 1;
> > 2;
> > 2;2
> > 3;2
> > 3;3
> > 1;1", sep=";", header=TRUE)
> >
> > table(b$PLASER,b$FLASER, exclude = NULL)
> >
> > yields the same combinations you showed
> > earlier:
> >
> >1 2 3 
> >   11 0 00
> >   21 1 11
> >   30 0 10
> >1 1 01
> >
> > If you want to eliminate the four -based
> > combinations completely, this line
> >
> > b$pheno <-
> >   ifelse(b$PLASER==2 |
> >  b$FLASER==2 |
> >  is.na(b$PLASER) |
> >  is.na(b$PLASER) & b$FLASER %in% 1:2 |
> >  is.na(b$FLASER) & b$PLASER == 2,
> >  2, 1)
> > table(b$pheno, exclude = NULL)
> >
> > will do it:
> >
> > 1 2
> > 2 7
> >
> > Best,
> > Rasmus
> > __
> > 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-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] if else statement adjustemtn

2020-06-12 Thread Ana Marija
Hi Rasmus,

thank you for getting back to be, the command your provided seems to
add all 11 NAs to 2s
> b$pheno <-
+   ifelse(b$PLASER==2 |
+  b$FLASER==2 |
+  is.na(b$PLASER) |
+  is.na(b$PLASER) & b$FLASER %in% 1:2 |
+  is.na(b$FLASER) & b$PLASER == 2,
+  2, 1)
> table(b$pheno, exclude = NULL)

  1   2
859 839

Once again my desired results is to keep these 7 NAs as NAs
> table(b$PLASER,b$FLASER, exclude = NULL)

 1   2   3 
  1836  14   00
  2691  70  432
  3  2   7  210
 4   1   07

and have
825 2s (825=691+14+70+7+43)
and the rest would be 1s (866=1698-7-825)

On Fri, Jun 12, 2020 at 9:29 PM Rasmus Liland  wrote:
>
> On 2020-06-13 11:30 +1000, Jim Lemon wrote:
> > On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon wrote:
> > > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija wrote:
> > > >
> > > > I am trying to make a new column
> > > > "pheno" so that I reduce the number
> > > > of NAs
> > >
> > > it looks like those two NA values in
> > > PLASER are the ones you want to drop.
> >
> > From just your summary table, it's hard to
> > guess the distribution of NA values.
>
> Dear Ana,
>
> This small sample
>
> b <- read.table(text="FLASER;PLASER
> 1;2
> ;2
> ;
> 1;
> 2;
> 2;2
> 3;2
> 3;3
> 1;1", sep=";", header=TRUE)
>
> table(b$PLASER,b$FLASER, exclude = NULL)
>
> yields the same combinations you showed
> earlier:
>
>1 2 3 
>   11 0 00
>   21 1 11
>   30 0 10
>1 1 01
>
> If you want to eliminate the four -based
> combinations completely, this line
>
> b$pheno <-
>   ifelse(b$PLASER==2 |
>  b$FLASER==2 |
>  is.na(b$PLASER) |
>  is.na(b$PLASER) & b$FLASER %in% 1:2 |
>  is.na(b$FLASER) & b$PLASER == 2,
>  2, 1)
> table(b$pheno, exclude = NULL)
>
> will do it:
>
> 1 2
> 2 7
>
> Best,
> Rasmus
> __
> 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] if else statement adjustemtn

2020-06-12 Thread Rasmus Liland
On 2020-06-13 11:30 +1000, Jim Lemon wrote:
> On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon wrote:
> > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija wrote:
> > >
> > > I am trying to make a new column 
> > > "pheno" so that I reduce the number 
> > > of NAs
> >
> > it looks like those two NA values in 
> > PLASER are the ones you want to drop.
> 
> From just your summary table, it's hard to 
> guess the distribution of NA values.

Dear Ana,

This small sample

b <- read.table(text="FLASER;PLASER
1;2
;2
;
1;
2;
2;2
3;2
3;3
1;1", sep=";", header=TRUE)

table(b$PLASER,b$FLASER, exclude = NULL)

yields the same combinations you showed 
earlier:

   1 2 3 
  11 0 00
  21 1 11
  30 0 10
   1 1 01

If you want to eliminate the four -based 
combinations completely, this line

b$pheno <-
  ifelse(b$PLASER==2 |
 b$FLASER==2 |
 is.na(b$PLASER) |
 is.na(b$PLASER) & b$FLASER %in% 1:2 |
 is.na(b$FLASER) & b$PLASER == 2,
 2, 1)
table(b$pheno, exclude = NULL)

will do it:

1 2
2 7

Best,
Rasmus


signature.asc
Description: PGP signature
__
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] if else statement adjustemtn

2020-06-12 Thread Jim Lemon
Obviously my guess was wrong. I thought you wanted to impute the value
of "pheno" from FLASER if PLASER was missing. From just your summary
table, it's hard to guess the distribution of NA values. My guess that
the two undesirable NAs were cases where PLASER was missing and FLASER
was 2. My tactic at this point would be to look at the cases where
either FLASER or PLASER was missing and work out the logic to impute
the two that are giving you trouble.

Jim

On Sat, Jun 13, 2020 at 11:16 AM Ana Marija  wrote:
>
> Hi Jim,
>
> I tried it:
> > b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |is.na(b$PLASER) & b$FLASER == 
> > 2,2,1)
> > table(b$pheno,exclude = NULL)
>
>12 
>  859  828   11
> > b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |is.na(b$FLASER) & b$PLASER == 
> > 2,2,1)
> > table(b$pheno,exclude = NULL)
>
>12 
>  859  828   11
>
> Am I am doing something wrong?
>
> Thanks
> Ana
>
> On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon  wrote:
> >
> > Hi Ana,
> > From your desired result, it looks like those two NA values in PLASER
> > are the ones you want to drop.
> > If so, try this:
> >
> > b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |
> >  is.na(b$PLASER) & b$FLASER == 2,2,1)
> >
> > and if I have it the wrong way round, swap FLASER and PLASER in the
> > bit I have added.
> >
> > Jim
> >
> > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija  
> > wrote:
> > >
> > > Hello
> > >
> > > I have a data frame like this:
> > >
> > > > head(b)
> > >FID   IID FLASER PLASER
> > > 1: fam1000 G1000  1  1
> > > 2: fam1001 G1001  1  1
> > > 3: fam1003 G1003  1  2
> > > 4: fam1005 G1005  1  1
> > > 5: fam1009 G1009  1  1
> > > 6: fam1052 G1052  1  1
> > > ...
> > >
> > > > table(b$PLASER,b$FLASER, exclude = NULL)
> > >
> > >  1   2   3 
> > >   1836  14   00
> > >   2691  70  432
> > >   3  2   7  210
> > >  4   1   07
> > >
> > > I am trying to make a new column "pheno" so that I reduce the number of 
> > > NAs
> > >
> > > right now I am doing:
> > >
> > > > b$pheno=ifelse(b$PLASER==2 | b$FLASER==2,2,1)
> > > > table(b$pheno, exclude = NULL)
> > >
> > >12 
> > >  859  828   11
> > >
> > > I would like to reduce this number of NAs to be 7
> > > so I would like to have in "pheno column"
> > > 7 NAs
> > > 825 2s (825=691+14+70+7+43)
> > > and the rest would be 1s (866=1698-7-825)
> > >
> > > How can I change the above command to get these numbers?
> > >
> > > Thanks
> > > Ana
> > >
> > > __
> > > 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] if else statement adjustemtn

2020-06-12 Thread Ana Marija
Hi Jim,

I tried it:
> b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |is.na(b$PLASER) & b$FLASER == 
> 2,2,1)
> table(b$pheno,exclude = NULL)

   12 
 859  828   11
> b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |is.na(b$FLASER) & b$PLASER == 
> 2,2,1)
> table(b$pheno,exclude = NULL)

   12 
 859  828   11

Am I am doing something wrong?

Thanks
Ana

On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon  wrote:
>
> Hi Ana,
> From your desired result, it looks like those two NA values in PLASER
> are the ones you want to drop.
> If so, try this:
>
> b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |
>  is.na(b$PLASER) & b$FLASER == 2,2,1)
>
> and if I have it the wrong way round, swap FLASER and PLASER in the
> bit I have added.
>
> Jim
>
> On Sat, Jun 13, 2020 at 10:46 AM Ana Marija  
> wrote:
> >
> > Hello
> >
> > I have a data frame like this:
> >
> > > head(b)
> >FID   IID FLASER PLASER
> > 1: fam1000 G1000  1  1
> > 2: fam1001 G1001  1  1
> > 3: fam1003 G1003  1  2
> > 4: fam1005 G1005  1  1
> > 5: fam1009 G1009  1  1
> > 6: fam1052 G1052  1  1
> > ...
> >
> > > table(b$PLASER,b$FLASER, exclude = NULL)
> >
> >  1   2   3 
> >   1836  14   00
> >   2691  70  432
> >   3  2   7  210
> >  4   1   07
> >
> > I am trying to make a new column "pheno" so that I reduce the number of NAs
> >
> > right now I am doing:
> >
> > > b$pheno=ifelse(b$PLASER==2 | b$FLASER==2,2,1)
> > > table(b$pheno, exclude = NULL)
> >
> >12 
> >  859  828   11
> >
> > I would like to reduce this number of NAs to be 7
> > so I would like to have in "pheno column"
> > 7 NAs
> > 825 2s (825=691+14+70+7+43)
> > and the rest would be 1s (866=1698-7-825)
> >
> > How can I change the above command to get these numbers?
> >
> > Thanks
> > Ana
> >
> > __
> > 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] if else statement adjustemtn

2020-06-12 Thread Jim Lemon
Hi Ana,
>From your desired result, it looks like those two NA values in PLASER
are the ones you want to drop.
If so, try this:

b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |
 is.na(b$PLASER) & b$FLASER == 2,2,1)

and if I have it the wrong way round, swap FLASER and PLASER in the
bit I have added.

Jim

On Sat, Jun 13, 2020 at 10:46 AM Ana Marija  wrote:
>
> Hello
>
> I have a data frame like this:
>
> > head(b)
>FID   IID FLASER PLASER
> 1: fam1000 G1000  1  1
> 2: fam1001 G1001  1  1
> 3: fam1003 G1003  1  2
> 4: fam1005 G1005  1  1
> 5: fam1009 G1009  1  1
> 6: fam1052 G1052  1  1
> ...
>
> > table(b$PLASER,b$FLASER, exclude = NULL)
>
>  1   2   3 
>   1836  14   00
>   2691  70  432
>   3  2   7  210
>  4   1   07
>
> I am trying to make a new column "pheno" so that I reduce the number of NAs
>
> right now I am doing:
>
> > b$pheno=ifelse(b$PLASER==2 | b$FLASER==2,2,1)
> > table(b$pheno, exclude = NULL)
>
>12 
>  859  828   11
>
> I would like to reduce this number of NAs to be 7
> so I would like to have in "pheno column"
> 7 NAs
> 825 2s (825=691+14+70+7+43)
> and the rest would be 1s (866=1698-7-825)
>
> How can I change the above command to get these numbers?
>
> Thanks
> Ana
>
> __
> 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] if else statement adjustemtn

2020-06-12 Thread Ana Marija
Hello

I have a data frame like this:

> head(b)
   FID   IID FLASER PLASER
1: fam1000 G1000  1  1
2: fam1001 G1001  1  1
3: fam1003 G1003  1  2
4: fam1005 G1005  1  1
5: fam1009 G1009  1  1
6: fam1052 G1052  1  1
...

> table(b$PLASER,b$FLASER, exclude = NULL)

 1   2   3 
  1836  14   00
  2691  70  432
  3  2   7  210
 4   1   07

I am trying to make a new column "pheno" so that I reduce the number of NAs

right now I am doing:

> b$pheno=ifelse(b$PLASER==2 | b$FLASER==2,2,1)
> table(b$pheno, exclude = NULL)

   12 
 859  828   11

I would like to reduce this number of NAs to be 7
so I would like to have in "pheno column"
7 NAs
825 2s (825=691+14+70+7+43)
and the rest would be 1s (866=1698-7-825)

How can I change the above command to get these numbers?

Thanks
Ana

__
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] if else statement

2020-05-05 Thread PIKAL Petr
Hi

another possible version 

b$pheno <- ((b$FLASER==2) | (b$PLASER==2))+1

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Rui Barradas
> Sent: Monday, May 4, 2020 8:32 PM
> To: sokovic.anamar...@gmail.com; r-help 
> Subject: Re: [R] if else statement
> 
> Hello,
> 
> Here is a way, using logical indices.
> 
> b$pheno <- NA
> b$pheno[b$FLASER == 1 & b$PLASER == 1] <- 1 b$pheno[b$FLASER == 2 |
> b$PLASER == 2] <- 2
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Às 18:15 de 04/05/20, Ana Marija escreveu:
> > Hello,
> >
> > I have a data frame like this:
> >
> >> head(b)
> > FID   IID FLASER PLASER
> > 1: fam1000 G1000  1  1
> > 2: fam1001 G1001  1  1
> > 3: fam1003 G1003  1  2
> > 4: fam1005 G1005  1  1
> > 5: fam1009 G1009  NA  2
> > 6: fam1052 G1052  1  1
> > ...
> >> unique(b$PLASER)
> > [1]  1  2 NA
> >> unique(b$FLASER)
> > [1]  1  2 NA
> >
> > how can I do if else statement so that I am creating a PHENO =2 if
> > b$FLASER=2 or b$PLASER=2
> > PHENO=1 if b$FLASER=1 and b$PLASER=1
> > otherwise PHENO=NA
> >
> > I tried this but I am not sure if this is correct:
> > b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 |
> > b$FLASER==2,2,NA))
> >
> > Thanks
> > Ana
> >
> > [[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-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] if else statement

2020-05-04 Thread Richard O'Keefe
Your ifelse expression looks fine.  What goes wrong with it?

On Tue, 5 May 2020 at 05:16, Ana Marija  wrote:
>
> Hello,
>
> I have a data frame like this:
>
> > head(b)
>FID   IID FLASER PLASER
> 1: fam1000 G1000  1  1
> 2: fam1001 G1001  1  1
> 3: fam1003 G1003  1  2
> 4: fam1005 G1005  1  1
> 5: fam1009 G1009  NA  2
> 6: fam1052 G1052  1  1
> ...
> > unique(b$PLASER)
> [1]  1  2 NA
> > unique(b$FLASER)
> [1]  1  2 NA
>
> how can I do if else statement so that I am creating a
> PHENO =2 if b$FLASER=2 or b$PLASER=2
> PHENO=1 if b$FLASER=1 and b$PLASER=1
> otherwise PHENO=NA
>
> I tried this but I am not sure if this is correct:
> b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 |
> b$FLASER==2,2,NA))
>
> Thanks
> Ana
>
> [[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] if else statement

2020-05-04 Thread Rui Barradas

Hello,

Here is a way, using logical indices.

b$pheno <- NA
b$pheno[b$FLASER == 1 & b$PLASER == 1] <- 1
b$pheno[b$FLASER == 2 | b$PLASER == 2] <- 2


Hope this helps,

Rui Barradas

Às 18:15 de 04/05/20, Ana Marija escreveu:

Hello,

I have a data frame like this:


head(b)

FID   IID FLASER PLASER
1: fam1000 G1000  1  1
2: fam1001 G1001  1  1
3: fam1003 G1003  1  2
4: fam1005 G1005  1  1
5: fam1009 G1009  NA  2
6: fam1052 G1052  1  1
...

unique(b$PLASER)

[1]  1  2 NA

unique(b$FLASER)

[1]  1  2 NA

how can I do if else statement so that I am creating a
PHENO =2 if b$FLASER=2 or b$PLASER=2
PHENO=1 if b$FLASER=1 and b$PLASER=1
otherwise PHENO=NA

I tried this but I am not sure if this is correct:
b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 |
b$FLASER==2,2,NA))

Thanks
Ana

[[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] if else statement

2020-05-04 Thread Ana Marija
Thank you for the tip about table function, it seems correct:

> table(b$FLASER, b$PLASER, exclude = NULL)

 1   2 
  1836 6916
  2 14  708
 0  45   28
> table(b$pheno,exclude = NULL)

   12 
 836  828   34

On Mon, May 4, 2020 at 12:45 PM Jeff Newmiller 
wrote:

> To expand on Patrick's response...
>
> You can use the expand.grid function to generate a test table containing
> all combinations. However, we would not be in a position to verify that the
> results you get when you apply your logic to the test table are what you
> want... you know the requirements much better than we do. Nor is that kind
> of service what this mailing list is for, so please focus on showing what
> you cannot figure out how to accomplish rather than asking us to do or
> check your work for you.
>
> On May 4, 2020 10:33:12 AM PDT, "Patrick (Malone Quantitative)" <
> mal...@malonequantitative.com> wrote:
> >"I tried this but I am not sure if this is correct:"
> >
> >Does it provide the expected result for all possible combinations of
> >1/2/NA
> >for both variables?
> >
> >On Mon, May 4, 2020 at 1:16 PM Ana Marija 
> >wrote:
> >
> >> Hello,
> >>
> >> I have a data frame like this:
> >>
> >> > head(b)
> >>FID   IID FLASER PLASER
> >> 1: fam1000 G1000  1  1
> >> 2: fam1001 G1001  1  1
> >> 3: fam1003 G1003  1  2
> >> 4: fam1005 G1005  1  1
> >> 5: fam1009 G1009  NA  2
> >> 6: fam1052 G1052  1  1
> >> ...
> >> > unique(b$PLASER)
> >> [1]  1  2 NA
> >> > unique(b$FLASER)
> >> [1]  1  2 NA
> >>
> >> how can I do if else statement so that I am creating a
> >> PHENO =2 if b$FLASER=2 or b$PLASER=2
> >> PHENO=1 if b$FLASER=1 and b$PLASER=1
> >> otherwise PHENO=NA
> >>
> >> I tried this but I am not sure if this is correct:
> >> b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 |
> >> b$FLASER==2,2,NA))
> >>
> >> Thanks
> >> Ana
> >>
> >> [[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.
> >>
>
> --
> Sent from my phone. Please excuse my brevity.
>

[[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] if else statement

2020-05-04 Thread Jeff Newmiller
To expand on Patrick's response...

You can use the expand.grid function to generate a test table containing all 
combinations. However, we would not be in a position to verify that the results 
you get when you apply your logic to the test table are what you want... you 
know the requirements much better than we do. Nor is that kind of service what 
this mailing list is for, so please focus on showing what you cannot figure out 
how to accomplish rather than asking us to do or check your work for you.

On May 4, 2020 10:33:12 AM PDT, "Patrick (Malone Quantitative)" 
 wrote:
>"I tried this but I am not sure if this is correct:"
>
>Does it provide the expected result for all possible combinations of
>1/2/NA
>for both variables?
>
>On Mon, May 4, 2020 at 1:16 PM Ana Marija 
>wrote:
>
>> Hello,
>>
>> I have a data frame like this:
>>
>> > head(b)
>>FID   IID FLASER PLASER
>> 1: fam1000 G1000  1  1
>> 2: fam1001 G1001  1  1
>> 3: fam1003 G1003  1  2
>> 4: fam1005 G1005  1  1
>> 5: fam1009 G1009  NA  2
>> 6: fam1052 G1052  1  1
>> ...
>> > unique(b$PLASER)
>> [1]  1  2 NA
>> > unique(b$FLASER)
>> [1]  1  2 NA
>>
>> how can I do if else statement so that I am creating a
>> PHENO =2 if b$FLASER=2 or b$PLASER=2
>> PHENO=1 if b$FLASER=1 and b$PLASER=1
>> otherwise PHENO=NA
>>
>> I tried this but I am not sure if this is correct:
>> b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 |
>> b$FLASER==2,2,NA))
>>
>> Thanks
>> Ana
>>
>> [[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.
>>

-- 
Sent from my phone. Please excuse my brevity.

__
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] if else statement

2020-05-04 Thread Patrick (Malone Quantitative)
"I tried this but I am not sure if this is correct:"

Does it provide the expected result for all possible combinations of 1/2/NA
for both variables?

On Mon, May 4, 2020 at 1:16 PM Ana Marija 
wrote:

> Hello,
>
> I have a data frame like this:
>
> > head(b)
>FID   IID FLASER PLASER
> 1: fam1000 G1000  1  1
> 2: fam1001 G1001  1  1
> 3: fam1003 G1003  1  2
> 4: fam1005 G1005  1  1
> 5: fam1009 G1009  NA  2
> 6: fam1052 G1052  1  1
> ...
> > unique(b$PLASER)
> [1]  1  2 NA
> > unique(b$FLASER)
> [1]  1  2 NA
>
> how can I do if else statement so that I am creating a
> PHENO =2 if b$FLASER=2 or b$PLASER=2
> PHENO=1 if b$FLASER=1 and b$PLASER=1
> otherwise PHENO=NA
>
> I tried this but I am not sure if this is correct:
> b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 |
> b$FLASER==2,2,NA))
>
> Thanks
> Ana
>
> [[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.
>


-- 
Patrick S. Malone, Ph.D., Malone Quantitative
NEW Service Models: http://malonequantitative.com

He/Him/His

[[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] if else statement

2020-05-04 Thread Ana Marija
Hello,

I have a data frame like this:

> head(b)
   FID   IID FLASER PLASER
1: fam1000 G1000  1  1
2: fam1001 G1001  1  1
3: fam1003 G1003  1  2
4: fam1005 G1005  1  1
5: fam1009 G1009  NA  2
6: fam1052 G1052  1  1
...
> unique(b$PLASER)
[1]  1  2 NA
> unique(b$FLASER)
[1]  1  2 NA

how can I do if else statement so that I am creating a
PHENO =2 if b$FLASER=2 or b$PLASER=2
PHENO=1 if b$FLASER=1 and b$PLASER=1
otherwise PHENO=NA

I tried this but I am not sure if this is correct:
b$pheno=ifelse(b$PLASER==1 & b$FLASER==1,1,ifelse(b$PLASER==2 |
b$FLASER==2,2,NA))

Thanks
Ana

[[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] if else with 4 conditions problem

2018-05-27 Thread Rui Barradas

Hello,

It's just a sequence of ifelse instructions.

dat <- read.table(text = "
   A   B
   1   1
   1   0
   0   1
   0   0
", header = TRUE)

dat$A1 <- ifelse(dat$A == 1 & dat$B == 1, 1, 0)
dat$A2 <- ifelse(dat$A == 1 & dat$B == 0, 1, 0)
dat$A3 <- ifelse(dat$A == 0 & dat$B == 1, 1, 0)
dat$A4 <- ifelse(dat$A == 0 & dat$B == 0, 1, 0)
dat


Hope this helps,

Rui Barradas


On 5/27/2018 3:13 PM, smart hendsome via R-help wrote:

Hi everyone,
I have two columns:
    A               B
    1               1   1               0
    0               1
    0               0

I have 4 categories which are:
1) if A = 1 and B =1 then A1 = 1, else A2 = 0, A3 = 0, A4 = 0
2) if A = 1 and B =0 then A1 = 0, else A2 =1, A3 = 0, A4 = 0

3) if A = 0 and B = 1 then A1 = 0, else A2 = 0, A3 = 1, A4 = 0

4) if A = 0 and B =0 then A1 = 0, else A2 = 0, A3 = 0, A4 = 1

I want the data become like below:
    A        B      A1      A2    A3     A4
    1        1       1    0    0      0   1        0       0        1   
     0  0
    0        1       0    0        1  0
    0        0       0    0        0  1
Anyone can help me? Many Thanks.
Regards,
Zuhri


|  | Virus-free. www.avast.com  |


[[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] if else with 4 conditions problem

2018-05-27 Thread smart hendsome via R-help
Hi everyone,
I have two columns:
   A               B
   1               1   1               0
   0               1
   0               0

I have 4 categories which are:
1) if A = 1 and B =1 then A1 = 1, else A2 = 0, A3 = 0, A4 = 0
2) if A = 1 and B =0 then A1 = 0, else A2 =1, A3 = 0, A4 = 0

3) if A = 0 and B = 1 then A1 = 0, else A2 = 0, A3 = 1, A4 = 0

4) if A = 0 and B =0 then A1 = 0, else A2 = 0, A3 = 0, A4 = 1

I want the data become like below:
   A        B      A1      A2    A3     A4
   1        1       1    0    0      0   1        0       0        1
    0  0
   0        1       0    0        1  0
   0        0       0    0        0  1
Anyone can help me? Many Thanks.
Regards,
Zuhri


|  | Virus-free. www.avast.com  |


[[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] if/else help

2016-09-22 Thread Crombie, Burnette N
Thanks very much for your detailed reply to my post.  Very helpful/useful 
tool(s) you’ve provide me.  Best wishes, B.

From: William Dunlap [mailto:wdun...@tibco.com]
Sent: Wednesday, September 21, 2016 10:48 AM
To: Crombie, Burnette N <bcrom...@utk.edu>
Cc: r-help@r-project.org
Subject: Re: [R] if/else help

If you write your code as functions you can avoid the nasty 
'if(exists("x"))x<-...' business this by writing default values for
arguments to your function.   They will be computed only when
they are used.  E.g.,
analyzeData <- function(a=0, b=0, c=0, d="x", r4 = data.frame(a, b, c, d)) {
summary(r4)
}
> analyzeData(c=101:102)
   a   b   c d
 Min.   :0   Min.   :0   Min.   :101.0   x:2
 1st Qu.:0   1st Qu.:0   1st Qu.:101.2
 Median :0   Median :0   Median :101.5
 Mean   :0   Mean   :0   Mean   :101.5
 3rd Qu.:0   3rd Qu.:0   3rd Qu.:101.8
 Max.   :0   Max.   :0   Max.   :102.0
> analyzeData(r4=data.frame(a=10:11,b=20:21,c=30:31,d=c("x","y")))
   a   b   c d
 Min.   :10.00   Min.   :20.00   Min.   :30.00   x:1
 1st Qu.:10.25   1st Qu.:20.25   1st Qu.:30.25   y:1
 Median :10.50   Median :20.50   Median :30.50
 Mean   :10.50   Mean   :20.50   Mean   :30.50
 3rd Qu.:10.75   3rd Qu.:20.75   3rd Qu.:30.75
 Max.   :11.00   Max.   :21.00   Max.   :31.00


Bill Dunlap
TIBCO Software
wdunlap tibco.com<http://tibco.com>

On Tue, Sep 20, 2016 at 12:31 PM, Crombie, Burnette N 
<bcrom...@utk.edu<mailto:bcrom...@utk.edu>> wrote:
If a data.frame (r4) does not exist in my R environment, I would like to create 
it before I move on to the next step in my script. How do I make that happen?  
Here is what I want to do from a code perspective:

if (exists(r4))
{
is.data.frame(get(r4))
}
else
{
a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
}

Thanks for your help,
B

[[alternative HTML version deleted]]

__
R-help@r-project.org<mailto: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] if/else help

2016-09-22 Thread Crombie, Burnette N
Thank you for your time, Don.  Exactly what I was looking for - a one-liner.  
Feedback from others on this post has been good to expand my knowledge, though. 
 I'm too old for homework but have just started using R if/else, loops, and 
functions and trying to get the hang of them.  Best wishes - B

-Original Message-
From: MacQueen, Don [mailto:macque...@llnl.gov] 
Sent: Wednesday, September 21, 2016 11:26 AM
To: Crombie, Burnette N <bcrom...@utk.edu>; r-help@r-project.org
Subject: Re: [R] if/else help

Hopefully this is not a homework question.

The other responses are fine, but I would suggest the simplest way to do 
exactly what you ask is


if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x')


The exists() function requires a character string for its first argument, i.e., 
the name of the object, not the object itself (check the help page for exists).

Using "get" to get it doesn't make sense if it already exists.

-Don

--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N"
<r-help-boun...@r-project.org on behalf of bcrom...@utk.edu> wrote:

>If a data.frame (r4) does not exist in my R environment, I would like 
>to create it before I move on to the next step in my script. How do I 
>make that happen?  Here is what I want to do from a code perspective:
>
>if (exists(r4))
>{
>is.data.frame(get(r4))
>}
>else
>{
>a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) }
>
>Thanks for your help,
>B
>
>   [[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] if/else help

2016-09-21 Thread David Winsemius

> On Sep 21, 2016, at 8:26 AM, MacQueen, Don  wrote:
> 
> Hopefully this is not a homework question.
> 
> The other responses are fine, but I would suggest the simplest way to do
> exactly what you ask is
> 
> 
> if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x')
> 
> 
> The exists() function requires a character string for its first argument,
> i.e., the name of the object, not the object itself (check the help page
> for exists).

Since the implicit goal is to determine whether a data.frame by that name is in 
the search path one could add the requirement that the 'list'-mode be added as 
a requirement in the search:

 if ( !exists('r4', mode='list') ){ r4 <- data.frame(a=0, b=0, c=0, d='x')}

That would avoid a 'false'-detection (or at least an unintended detection) for 
a function by that name if one `exist`-ed. It would also mask any `r4` that 
might have been a matrix or other atomic vector outside the local evaluation 
frame.

-- 
David.


> 
> Using "get" to get it doesn't make sense if it already exists.
> 
> -Don
> 
> -- 
> Don MacQueen
> 
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
> 
> 
> 
> 
> 
> On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N"
>  wrote:
> 
>> If a data.frame (r4) does not exist in my R environment, I would like to
>> create it before I move on to the next step in my script. How do I make
>> that happen?  Here is what I want to do from a code perspective:
>> 
>> if (exists(r4))
>> {
>> is.data.frame(get(r4))
>> }
>> else
>> {
>> a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
>> }
>> 
>> Thanks for your help,
>> B
>> 
>>  [[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.

David Winsemius
Alameda, CA, USA

__
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] if/else help

2016-09-21 Thread MacQueen, Don
Hopefully this is not a homework question.

The other responses are fine, but I would suggest the simplest way to do
exactly what you ask is


if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x')


The exists() function requires a character string for its first argument,
i.e., the name of the object, not the object itself (check the help page
for exists).

Using "get" to get it doesn't make sense if it already exists.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N"
 wrote:

>If a data.frame (r4) does not exist in my R environment, I would like to
>create it before I move on to the next step in my script. How do I make
>that happen?  Here is what I want to do from a code perspective:
>
>if (exists(r4))
>{
>is.data.frame(get(r4))
>}
>else
>{
>a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
>}
>
>Thanks for your help,
>B
>
>   [[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] if/else help

2016-09-21 Thread William Dunlap via R-help
If you write your code as functions you can avoid the nasty
'if(exists("x"))x<-...' business this by writing default values for
arguments to your function.   They will be computed only when
they are used.  E.g.,

analyzeData <- function(a=0, b=0, c=0, d="x", r4 = data.frame(a, b, c, d)) {
summary(r4)
}

> analyzeData(c=101:102)
   a   b   c d
 Min.   :0   Min.   :0   Min.   :101.0   x:2
 1st Qu.:0   1st Qu.:0   1st Qu.:101.2
 Median :0   Median :0   Median :101.5
 Mean   :0   Mean   :0   Mean   :101.5
 3rd Qu.:0   3rd Qu.:0   3rd Qu.:101.8
 Max.   :0   Max.   :0   Max.   :102.0
> analyzeData(r4=data.frame(a=10:11,b=20:21,c=30:31,d=c("x","y")))
   a   b   c d
 Min.   :10.00   Min.   :20.00   Min.   :30.00   x:1
 1st Qu.:10.25   1st Qu.:20.25   1st Qu.:30.25   y:1
 Median :10.50   Median :20.50   Median :30.50
 Mean   :10.50   Mean   :20.50   Mean   :30.50
 3rd Qu.:10.75   3rd Qu.:20.75   3rd Qu.:30.75
 Max.   :11.00   Max.   :21.00   Max.   :31.00



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Sep 20, 2016 at 12:31 PM, Crombie, Burnette N 
wrote:

> If a data.frame (r4) does not exist in my R environment, I would like to
> create it before I move on to the next step in my script. How do I make
> that happen?  Here is what I want to do from a code perspective:
>
> if (exists(r4))
> {
> is.data.frame(get(r4))
> }
> else
> {
> a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
> }
>
> Thanks for your help,
> B
>
> [[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] if/else help

2016-09-21 Thread David Winsemius

> On Sep 20, 2016, at 12:31 PM, Crombie, Burnette N  wrote:
> 
> If a data.frame (r4) does not exist in my R environment, I would like to 
> create it before I move on to the next step in my script. How do I make that 
> happen?  Here is what I want to do from a code perspective:
> 
> if (exists(r4))
> {
> is.data.frame(get(r4))
> }
> else
> {
> a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
> }
> 
> Thanks for your help,
> B
> 
>   [[alternative HTML version deleted]]

Please, please, please, do not encourage people to use the construction:

data.frame(cbind(

...and learn to distrust whatever misguided example you learned it from. (Yes, 
I know there is one in the help pages but that is an exception to the rule.)

There is no reason to use it. It would be much clearer to do this:

r4 <- data.frame(a = 0, b = 0, c = 0, d = "x")# only one column is 
factor-class.

If you did it your way you would have gotten four columns of factors, (first 
cbind() creates a character matrix, and then data.frame() creates factors)  ... 
which does not appear to be what you expected.

And ... as always has been the case on Rhelp, ... learn to post in plain text.

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

David Winsemius
Alameda, CA, USA

__
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] if/else help

2016-09-21 Thread Jeff Newmiller
Get rid of the commas? Get rid of the get() function call? Get rid of the 
cbind() function call?  Post using plain text format so the HTML doesn't screw 
up code? Read the Posting Guide? All of these ideas have merit IMHO...
-- 
Sent from my phone. Please excuse my brevity.

On September 20, 2016 12:31:47 PM PDT, "Crombie, Burnette N"  
wrote:
>If a data.frame (r4) does not exist in my R environment, I would like
>to create it before I move on to the next step in my script. How do I
>make that happen?  Here is what I want to do from a code perspective:
>
>if (exists(r4))
>{
>is.data.frame(get(r4))
>}
>else
>{
>a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
>}
>
>Thanks for your help,
>B
>
>   [[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] if/else help

2016-09-21 Thread Crombie, Burnette N
If a data.frame (r4) does not exist in my R environment, I would like to create 
it before I move on to the next step in my script. How do I make that happen?  
Here is what I want to do from a code perspective:

if (exists(r4))
{
is.data.frame(get(r4))
}
else
{
a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d))
}

Thanks for your help,
B

[[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] if else condition - help

2016-05-23 Thread Martin Maechler
> jim holtman 
> on Sun, 22 May 2016 16:47:06 -0400 writes:

> if you want to use 'ifelse', here is a way:

hmm, why should he want that ?
The OP did mention that it's about somewhat large objects, so
efficiency is one of the considerations :

ifelse() is often convenient and nicely self-explaining, but it 
is (because of its generality, but also by its definition)
much *less efficient* than the (sometimes slightly less
convenient) ways you were shown previously in this thread :

- For the generalized case findInterval() is order of magnitudes
  better, and
- for the simple case you were shown to use logical indexing,
  i.e., calls à lax[x > k] <- ..


In summary:
   Use  ifelse()  much less -- notably if writing
   functions/code which should scale !


Martin Maechler
ETH Zurich

__
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] if else condition - help

2016-05-22 Thread jim holtman
if you want to use 'ifelse', here is a way:


> k =
+ structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
+ -2.91485614212114, -0.108388742328104, -1.41670341534772,
-1.70609114096417,
+ 2.92018951284015, 0.201868946570178, 0.907637296638577,
-0.403004972105994,
+ -2.47718015803221, -0.354616729237253, -0.316113789733413,
1.01532974064126,
+ -2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
+ "B", "C", "D"), c("C1", "C2", "C3", "C4")))
>
> # if you want to use 'ifelse', here is a way
>
> x <- ifelse(k > 1.5
+ , 1
+ , ifelse(k < -1.5
+ , -1
+ , 0
+ )
+ )
>
> str(x)
 num [1:4, 1:4] 0 1 0 -1 0 0 -1 1 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:4] "A" "B" "C" "D"
  ..$ : chr [1:4] "C1" "C2" "C3" "C4"
> x
  C1 C2 C3 C4
A  0  0  0  0
B  1  0  0  0
C  0 -1  0  0
D -1  1 -1 -1
>




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.

On Sun, May 22, 2016 at 1:58 PM, Adrian Johnson 
wrote:

> Hi group:
> I am having difficulty with if else condition. I kindly request some help.
>
> I have a matrix k
>
> > k
>C1 C2 C3 C4
> A  0.09902175 -0.1083887  0.2018689 -0.3546167
> B  1.60623838 -1.4167034  0.9076373 -0.3161138
> C -0.10433133 -1.7060911 -0.4030050  1.0153297
> D -2.91485614  2.9201895 -2.4771802 -2.6991517
>
> I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
>
> > k1 - desired output
>C1 C2 C3 C4
> A  0   00   0
> B   1  00   0
> C   0-1 0   0
> D   -11-1   -1
>
>
> I am trying with if else but cannot do it. I could only define one
> condition.  Could someone help how I can do this. I dont mean only if
> else, but any other way.
>
> k =
> structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
> -2.91485614212114, -0.108388742328104, -1.41670341534772,
> -1.70609114096417,
> 2.92018951284015, 0.201868946570178, 0.907637296638577, -0.403004972105994,
> -2.47718015803221, -0.354616729237253, -0.316113789733413,
> 1.01532974064126,
> -2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
> "B", "C", "D"), c("C1", "C2", "C3", "C4")))
>
>
>
> k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
>
> > k1
>   C1 C2 C3 C4
> A -1 -1 -1 -1
> B  1 -1 -1 -1
> C -1 -1 -1 -1
> D -1  1 -1 -1
>
>
>
> Thanks
> Adrian
>
> __
> 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] if else condition - help

2016-05-22 Thread Dylan Keenan
Try this:

> sign(ifelse(abs(k)<=1.5, 0, k))


  C1 C2 C3 C4
A  0  0  0  0
B  1  0  0  0
C  0 -1  0  0
D -1  1 -1 -1

On Sun, May 22, 2016 at 2:00 PM Adrian Johnson 
wrote:

> Hi group:
> I am having difficulty with if else condition. I kindly request some help.
>
> I have a matrix k
>
> > k
>C1 C2 C3 C4
> A  0.09902175 -0.1083887  0.2018689 -0.3546167
> B  1.60623838 -1.4167034  0.9076373 -0.3161138
> C -0.10433133 -1.7060911 -0.4030050  1.0153297
> D -2.91485614  2.9201895 -2.4771802 -2.6991517
>
> I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
>
> > k1 - desired output
>C1 C2 C3 C4
> A  0   00   0
> B   1  00   0
> C   0-1 0   0
> D   -11-1   -1
>
>
> I am trying with if else but cannot do it. I could only define one
> condition.  Could someone help how I can do this. I dont mean only if
> else, but any other way.
>
> k =
> structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
> -2.91485614212114, -0.108388742328104, -1.41670341534772,
> -1.70609114096417,
> 2.92018951284015, 0.201868946570178, 0.907637296638577, -0.403004972105994,
> -2.47718015803221, -0.354616729237253, -0.316113789733413,
> 1.01532974064126,
> -2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
> "B", "C", "D"), c("C1", "C2", "C3", "C4")))
>
>
>
> k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
>
> > k1
>   C1 C2 C3 C4
> A -1 -1 -1 -1
> B  1 -1 -1 -1
> C -1 -1 -1 -1
> D -1  1 -1 -1
>
>
>
> Thanks
> Adrian
>
> __
> 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] if else condition - help

2016-05-22 Thread David Winsemius

> On May 22, 2016, at 11:23 AM, Adrian Johnson  
> wrote:
> 
> Thank you both Dylan and Wray.
> 
> since my matrix is quite large and for simplicity in downstream
> operation, i will use sign function. thanks a lot.
> 
> On Sun, May 22, 2016 at 2:12 PM, Dylan Keenan  wrote:
>> Try this:
>> 
>>> sign(ifelse(abs(k)<=1.5, 0, k))
>> 
>> 
>>  C1 C2 C3 C4
>> A  0  0  0  0
>> B  1  0  0  0
>> C  0 -1  0  0
>> D -1  1 -1 -1
>> 

If the problems were somewhat less symmetric or  more complex this would be a 
method that could be easily generalized to a larger number of less "absolutely" 
symmetric intervals:

 k2 <- k
 k2[] <- findInterval(k2, c(-Inf, -1.5, 1.5, Inf) ) -2  
 # shifts the 1-3 values to -1 to 1
 k2
  C1 C2 C3 C4
A  0  0  0  0
B  1  0  0  0
C  0 -1  0  0
D -1  1 -1 -1

Using k2[] <- ... preserves the matrix structure



>> On Sun, May 22, 2016 at 2:00 PM Adrian Johnson 
>> wrote:
>>> 
>>> Hi group:
>>> I am having difficulty with if else condition. I kindly request some help.
>>> 
>>> I have a matrix k
>>> 
 k
>>>   C1 C2 C3 C4
>>> A  0.09902175 -0.1083887  0.2018689 -0.3546167
>>> B  1.60623838 -1.4167034  0.9076373 -0.3161138
>>> C -0.10433133 -1.7060911 -0.4030050  1.0153297
>>> D -2.91485614  2.9201895 -2.4771802 -2.6991517
>>> 
>>> I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
>>> 
 k1 - desired output
>>>   C1 C2 C3 C4
>>> A  0   00   0
>>> B   1  00   0
>>> C   0-1 0   0
>>> D   -11-1   -1
>>> 
>>> 
>>> I am trying with if else but cannot do it. I could only define one
>>> condition.  Could someone help how I can do this. I dont mean only if
>>> else, but any other way.
>>> 
>>> k =
>>> structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
>>> -2.91485614212114, -0.108388742328104, -1.41670341534772,
>>> -1.70609114096417,
>>> 2.92018951284015, 0.201868946570178, 0.907637296638577,
>>> -0.403004972105994,
>>> -2.47718015803221, -0.354616729237253, -0.316113789733413,
>>> 1.01532974064126,
>>> -2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
>>> "B", "C", "D"), c("C1", "C2", "C3", "C4")))
>>> 
>>> 
>>> 
>>> k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
>>> 
 k1
>>>  C1 C2 C3 C4
>>> A -1 -1 -1 -1
>>> B  1 -1 -1 -1
>>> C -1 -1 -1 -1
>>> D -1  1 -1 -1
>>> 
>>> 
>>> 
>>> Thanks
>>> Adrian
>>> 
>>> __
>>> 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.

David Winsemius
Alameda, CA, USA

__
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] if else condition - help

2016-05-22 Thread Adrian Johnson
Thank you both Dylan and Wray.

since my matrix is quite large and for simplicity in downstream
operation, i will use sign function. thanks a lot.

On Sun, May 22, 2016 at 2:12 PM, Dylan Keenan  wrote:
> Try this:
>
>> sign(ifelse(abs(k)<=1.5, 0, k))
>
>
>   C1 C2 C3 C4
> A  0  0  0  0
> B  1  0  0  0
> C  0 -1  0  0
> D -1  1 -1 -1
>
> On Sun, May 22, 2016 at 2:00 PM Adrian Johnson 
> wrote:
>>
>> Hi group:
>> I am having difficulty with if else condition. I kindly request some help.
>>
>> I have a matrix k
>>
>> > k
>>C1 C2 C3 C4
>> A  0.09902175 -0.1083887  0.2018689 -0.3546167
>> B  1.60623838 -1.4167034  0.9076373 -0.3161138
>> C -0.10433133 -1.7060911 -0.4030050  1.0153297
>> D -2.91485614  2.9201895 -2.4771802 -2.6991517
>>
>> I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
>>
>> > k1 - desired output
>>C1 C2 C3 C4
>> A  0   00   0
>> B   1  00   0
>> C   0-1 0   0
>> D   -11-1   -1
>>
>>
>> I am trying with if else but cannot do it. I could only define one
>> condition.  Could someone help how I can do this. I dont mean only if
>> else, but any other way.
>>
>> k =
>> structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
>> -2.91485614212114, -0.108388742328104, -1.41670341534772,
>> -1.70609114096417,
>> 2.92018951284015, 0.201868946570178, 0.907637296638577,
>> -0.403004972105994,
>> -2.47718015803221, -0.354616729237253, -0.316113789733413,
>> 1.01532974064126,
>> -2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
>> "B", "C", "D"), c("C1", "C2", "C3", "C4")))
>>
>>
>>
>> k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
>>
>> > k1
>>   C1 C2 C3 C4
>> A -1 -1 -1 -1
>> B  1 -1 -1 -1
>> C -1 -1 -1 -1
>> D -1  1 -1 -1
>>
>>
>>
>> Thanks
>> Adrian
>>
>> __
>> 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] if else condition - help

2016-05-22 Thread WRAY NICHOLAS
Hi Adrian I'm not sure that you need to use the ifelse here.  You can simply
assign values ina vector or matrix using a simple condition -- here is a simple
example:

v<-c(4,5,6,7)
v1<-v
v1[]<-0
v1[v<5]<--1
v1[v>6]<-1
v1

Nick

> 
> On 22 May 2016 at 18:58 Adrian Johnson  wrote:
> 
> 
> Hi group:
> I am having difficulty with if else condition. I kindly request some help.
> 
> I have a matrix k
> 
> > k
> C1 C2 C3 C4
> A 0.09902175 -0.1083887 0.2018689 -0.3546167
> B 1.60623838 -1.4167034 0.9076373 -0.3161138
> C -0.10433133 -1.7060911 -0.4030050 1.0153297
> D -2.91485614 2.9201895 -2.4771802 -2.6991517
> 
> I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
> 
> > k1 - desired output
> C1 C2 C3 C4
> A 0 0 0 0
> B 1 0 0 0
> C 0 -1 0 0
> D -1 1 -1 -1
> 
> 
> I am trying with if else but cannot do it. I could only define one
> condition. Could someone help how I can do this. I dont mean only if
> else, but any other way.
> 
> k =
> structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
> -2.91485614212114, -0.108388742328104, -1.41670341534772,
> -1.70609114096417,
> 2.92018951284015, 0.201868946570178, 0.907637296638577,
> -0.403004972105994,
> -2.47718015803221, -0.354616729237253, -0.316113789733413,
> 1.01532974064126,
> -2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
> "B", "C", "D"), c("C1", "C2", "C3", "C4")))
> 
> 
> 
> k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
> 
> > k1
> C1 C2 C3 C4
> A -1 -1 -1 -1
> B 1 -1 -1 -1
> C -1 -1 -1 -1
> D -1 1 -1 -1
> 
> 
> 
> Thanks
> Adrian
> 
> __
> 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.


[R] if else condition - help

2016-05-22 Thread Adrian Johnson
Hi group:
I am having difficulty with if else condition. I kindly request some help.

I have a matrix k

> k
   C1 C2 C3 C4
A  0.09902175 -0.1083887  0.2018689 -0.3546167
B  1.60623838 -1.4167034  0.9076373 -0.3161138
C -0.10433133 -1.7060911 -0.4030050  1.0153297
D -2.91485614  2.9201895 -2.4771802 -2.6991517

I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;

> k1 - desired output
   C1 C2 C3 C4
A  0   00   0
B   1  00   0
C   0-1 0   0
D   -11-1   -1


I am trying with if else but cannot do it. I could only define one
condition.  Could someone help how I can do this. I dont mean only if
else, but any other way.

k =
structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
-2.91485614212114, -0.108388742328104, -1.41670341534772, -1.70609114096417,
2.92018951284015, 0.201868946570178, 0.907637296638577, -0.403004972105994,
-2.47718015803221, -0.354616729237253, -0.316113789733413, 1.01532974064126,
-2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
"B", "C", "D"), c("C1", "C2", "C3", "C4")))



k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))

> k1
  C1 C2 C3 C4
A -1 -1 -1 -1
B  1 -1 -1 -1
C -1 -1 -1 -1
D -1  1 -1 -1



Thanks
Adrian

__
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] If else

2015-10-31 Thread John Kane
In line.

John Kane
Kingston ON Canada


> -Original Message-
> From: valkr...@gmail.com
> Sent: Fri, 30 Oct 2015 20:40:03 -0500
> To: istaz...@gmail.com
> Subject: Re: [R] If else
> 
> I am trying to change the mydata$sex  from character to numeric

Why? 
 As Ista (mydata$confusingWillCauseProblemsLater) has pointed out this is a 
very unusual thing to do in R. 

Is there a very specific reason for doing this in your analysis.  Otherwise it 
may better to leave the coding as NA. Some of the data mungers here may be able 
to suggest which is the best strategy in R. 

R is 'weird' compared to more mundane stats packages such as SAS or SPSS and 
common techniques that one would use with them often are not appropriate in R.




> I want teh out put like
>id  sex
>   1  NA   0
>   2  NA   0
>   3  M 1
>   4  F 2
>   5  M1
>   6  F 2
>   7  F2
> 
> mydata$sex1 <- 0
> if(mydata$sex =="M " ){
>   mydata$sex1<-1
> } else {
>   mydata$sex1<-2
> }
> 
> mydata$sex1
> 
> Warning message:In if (mydata$sex == "M ") { :
>   the condition has length > 1 and only the first element will be
> used> mydata$sex1[1] 2 2 2 2 2 2 2 2
> 
>> 
> 
> 
> On Fri, Oct 30, 2015 at 8:28 PM, Ista Zahn <istaz...@gmail.com> wrote:
> 
>> Using numeric for missing sounds like asking for trouble. But if you
>> must, something like
>> 
>> mydata$confusingWillCauseProblemsLater <-
>>   ifelse(
>> is.na(mydata$sex),
>> 0,
>> as.numeric(factor(mydata$sex,
>>   levels = c("M", "F"
>> 
>> should do it.
>> 
>> Best,
>> Ista
>> 
>> On Fri, Oct 30, 2015 at 9:15 PM, Val <valkr...@gmail.com> wrote:
>>> Hi all,
>>> Iam trying to change character  to numeric but have probelm
>>> 
>>> mydata <- read.table(header=TRUE, text=', sep=" "
>>>  id  sex
>>>   1  NA
>>>   2  NA
>>>   3  M
>>>   4  F
>>>   5  M
>>>   6  F
>>>   7  F
>>>')
>>> 
>>> if sex is missing then sex=0;
>>> if sex is"M" then sex=1;
>>> if sex is"F" then sex=2;
>>> 
>>> Any help please ?
>>> 
>>> [[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.


FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your 
desktop!

__
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] If else

2015-10-31 Thread Val
Hi Jeff,

I thought I answered. Yes I was not clear about it.  The further analysis
will no  be done by R.  It is another software that will not accept a
character response variable.

Why R is so complicated to do that.  If it is SAS then I can do it on one
statement. .


On Sat, Oct 31, 2015 at 11:39 AM, Jeff Newmiller <jdnew...@dcn.davis.ca.us>
wrote:

> You haven't actually answered John's question as to the type of analysis
> you plan to do. It still looks from here like you should be using factor
> data rather than numeric, but since you are not being clear we cannot give
> specifics as to how to proceed.
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:<jdnew...@dcn.davis.ca.us>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 October 31, 2015 8:23:05 AM PDT, Val <valkr...@gmail.com> wrote:
> >Hi All,
> >
> >
> >Yes I need  to change to numeric  because I am preparing a data set
> >for
> >further  analysis. The variable to be changed  from character to
> >numeric
> >(in this case, sex) will be a response variable.  Some records have
> >missing
> >observation on sex and it is blank.
> > id  sex
> >  1
> >  2
> >  3  M
> >  4  F
> >  5  M
> >  6  F
> >  7  F
> >
> >I am reading the data like this
> >
> >mydata <- read.csv(header=TRUE, text=', sep=", ")
> > id  sex
> >  1   NA
> >  2  NA
> >  3  M
> >  4  F
> >  5  M
> >  6  F
> >  7  F
> >
> >The  data set is huge   (>250,000)
> >
> >
> >I want the output like this
> >
> > id  sexsex1
> >  1   NA0
> >  2  NA 0
> >  3  M   1
> >  4  F   2
> >  5  M  1
> >  6  F   2
> >  7  F   2
> >
> >Thank you in advance
> >
> >
> >On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com> wrote:
> >
> >> In line.
> >>
> >> John Kane
> >> Kingston ON Canada
> >>
> >>
> >> > -Original Message-
> >> > From: valkr...@gmail.com
> >> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
> >> > To: istaz...@gmail.com
> >> > Subject: Re: [R] If else
> >> >
> >> > I am trying to change the mydata$sex  from character to numeric
> >>
> >> Why?
> >>  As Ista (mydata$confusingWillCauseProblemsLater) has pointed out
> >this is
> >> a very unusual thing to do in R.
> >>
> >> Is there a very specific reason for doing this in your analysis.
> >> Otherwise it may better to leave the coding as NA. Some of the data
> >mungers
> >> here may be able to suggest which is the best strategy in R.
> >>
> >> R is 'weird' compared to more mundane stats packages such as SAS or
> >SPSS
> >> and common techniques that one would use with them often are not
> >> appropriate in R.
> >>
> >>
> >>
> >>
> >> > I want teh out put like
> >> >id  sex
> >> >   1  NA   0
> >> >   2  NA   0
> >> >   3  M 1
> >> >   4  F 2
> >> >   5  M1
> >> >   6  F 2
> >> >   7  F2
> >> >
> >> > mydata$sex1 <- 0
> >> > if(mydata$sex =="M " ){
> >> >   mydata$sex1<-1
> >> > } else {
> >> >   mydata$sex1<-2
> >> > }
> >> >
> >> > mydata$sex1
> >> >
> >> > Warning message:In if (mydata$sex == "M ") { :
> >> >   the condition has length > 1 and only the first element will be
> >> > used> mydata$sex1[1] 2 2 2 2 2 2 2 2
> >> >
> >> >>
> >> >
> >> >
> >> > On Fri, Oct 30, 2015 at 8:28 PM, Ista Zahn <istaz...@gmail.com>
> >wrote:
> >> >
> >> >> Using numeric for missing sounds like asking for trouble. But if
> >you
> &g

Re: [R] If else

2015-10-31 Thread Jeff Newmiller
Rolf gave you two ways. There are others. They all misrepresent the data (there 
are only two sexes but you are effectively acting as if there are three); hence 
the inquisition in hopes of diverting you to a more correct method of analysis. 
However, this is not the support forum for whatever other software you plan to 
proceed with so never mind.
---
Jeff NewmillerThe .   .  Go Live...
DCN:<jdnew...@dcn.davis.ca.us>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 October 31, 2015 10:15:33 AM PDT, Val <valkr...@gmail.com> wrote:
>Hi Jeff,
>
>I thought I answered. Yes I was not clear about it.  The further
>analysis
>will no  be done by R.  It is another software that will not accept a
>character response variable.
>
>Why R is so complicated to do that.  If it is SAS then I can do it on
>one
>statement. .
>
>
>On Sat, Oct 31, 2015 at 11:39 AM, Jeff Newmiller
><jdnew...@dcn.davis.ca.us>
>wrote:
>
>> You haven't actually answered John's question as to the type of
>analysis
>> you plan to do. It still looks from here like you should be using
>factor
>> data rather than numeric, but since you are not being clear we cannot
>give
>> specifics as to how to proceed.
>>
>---
>> Jeff NewmillerThe .   .  Go
>Live...
>> DCN:<jdnew...@dcn.davis.ca.us>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 October 31, 2015 8:23:05 AM PDT, Val <valkr...@gmail.com> wrote:
>> >Hi All,
>> >
>> >
>> >Yes I need  to change to numeric  because I am preparing a data set
>> >for
>> >further  analysis. The variable to be changed  from character to
>> >numeric
>> >(in this case, sex) will be a response variable.  Some records have
>> >missing
>> >observation on sex and it is blank.
>> > id  sex
>> >  1
>> >  2
>> >  3  M
>> >  4  F
>> >  5  M
>> >  6  F
>> >  7  F
>> >
>> >I am reading the data like this
>> >
>> >mydata <- read.csv(header=TRUE, text=', sep=", ")
>> > id  sex
>> >  1   NA
>> >  2  NA
>> >  3  M
>> >  4  F
>> >  5  M
>> >  6  F
>> >  7  F
>> >
>> >The  data set is huge   (>250,000)
>> >
>> >
>> >I want the output like this
>> >
>> >     id  sexsex1
>> >  1   NA0
>> >  2  NA 0
>> >  3  M   1
>> >  4  F   2
>> >  5  M  1
>> >  6  F   2
>> >  7  F   2
>> >
>> >Thank you in advance
>> >
>> >
>> >On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com>
>wrote:
>> >
>> >> In line.
>> >>
>> >> John Kane
>> >> Kingston ON Canada
>> >>
>> >>
>> >> > -Original Message-
>> >> > From: valkr...@gmail.com
>> >> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
>> >> > To: istaz...@gmail.com
>> >> > Subject: Re: [R] If else
>> >> >
>> >> > I am trying to change the mydata$sex  from character to numeric
>> >>
>> >> Why?
>> >>  As Ista (mydata$confusingWillCauseProblemsLater) has pointed out
>> >this is
>> >> a very unusual thing to do in R.
>> >>
>> >> Is there a very specific reason for doing this in your analysis.
>> >> Otherwise it may better to leave the coding as NA. Some of the
>data
>> >mungers
>> >> here may be able to suggest which is the b

Re: [R] If else

2015-10-31 Thread Ted Harding
paring a data set
>>> >for
>>> >further  analysis. The variable to be changed  from character to
>>> >numeric
>>> >(in this case, sex) will be a response variable.  Some records have
>>> >missing
>>> >observation on sex and it is blank.
>>> > id  sex
>>> >  1
>>> >  2
>>> >  3  M
>>> >  4  F
>>> >  5  M
>>> >  6  F
>>> >  7  F
>>> >
>>> >I am reading the data like this
>>> >
>>> >mydata <- read.csv(header=TRUE, text=', sep=", ")
>>> > id  sex
>>> >  1   NA
>>> >  2  NA
>>> >  3  M
>>> >  4  F
>>> >  5  M
>>> >  6  F
>>> >  7  F
>>> >
>>> >The  data set is huge   (>250,000)
>>> >
>>> >
>>> >I want the output like this
>>> >
>>> > id  sexsex1
>>> >  1   NA0
>>> >  2  NA 0
>>> >  3  M   1
>>> >  4  F   2
>>> >  5  M  1
>>> >  6  F   2
>>> >  7  F   2
>>> >
>>> >Thank you in advance
>>> >
>>> >
>>> >On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com>
>>wrote:
>>> >
>>> >> In line.
>>> >>
>>> >> John Kane
>>> >> Kingston ON Canada
>>> >>
>>> >>
>>> >> > -Original Message-
>>> >> > From: valkr...@gmail.com
>>> >> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
>>> >> > To: istaz...@gmail.com
>>> >> > Subject: Re: [R] If else
>>> >> >
>>> >> > I am trying to change the mydata$sex  from character to numeric
>>> >>
>>> >> Why?
>>> >>  As Ista (mydata$confusingWillCauseProblemsLater) has pointed out
>>> >this is
>>> >> a very unusual thing to do in R.
>>> >>
>>> >> Is there a very specific reason for doing this in your analysis.
>>> >> Otherwise it may better to leave the coding as NA. Some of the
>>data
>>> >mungers
>>> >> here may be able to suggest which is the best strategy in R.
>>> >>
>>> >> R is 'weird' compared to more mundane stats packages such as SAS
>>or
>>> >SPSS
>>> >> and common techniques that one would use with them often are not
>>> >> appropriate in R.
>>> >>
>>> >>
>>> >>
>>> >>
>>> >> > I want teh out put like
>>> >> >id  sex
>>> >> >   1  NA   0
>>> >> >   2  NA   0
>>> >> >   3  M 1
>>> >> >   4  F 2
>>> >> >   5  M1
>>> >> >   6  F 2
>>> >> >   7  F2
>>> >> >
>>> >> > mydata$sex1 <- 0
>>> >> > if(mydata$sex =="M " ){
>>> >> >   mydata$sex1<-1
>>> >> > } else {
>>> >> >   mydata$sex1<-2
>>> >> > }
>>> >> >
>>> >> > mydata$sex1
>>> >> >
>>> >> > Warning message:In if (mydata$sex == "M ") { :
>>> >> >   the condition has length > 1 and only the first element will
>>be
>>> >> > used> mydata$sex1[1] 2 2 2 2 2 2 2 2
>>> >> >
>>> >> >>
>>> >> >
>>> >> >
>>> >> > On Fri, Oct 30, 2015 at 8:28 PM, Ista Zahn <istaz...@gmail.com>
>>> >wrote:
>>> >> >
>>> >> >> Using numeric for missing sounds like asking for trouble. But
>>if
>>> >you
>>> >> >> must, something like
>>> >> >>
>>> >> >> mydata$confusingWillCauseProblemsLater <-
>>> >> >>   ifelse(
>>> >> >> is.na(mydata$sex),
>>> >> >> 0,
>>> >> >> as.numeric(factor(mydata$sex,
>>> >> >>   levels = c("M", "F"
>>> >> >>
>>> >> >> should do it.
>>

Re: [R] If else

2015-10-31 Thread Val
t;>> DCN:<jdnew...@dcn.davis.ca.us>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 October 31, 2015 8:23:05 AM PDT, Val <valkr...@gmail.com> wrote:
> >>> >Hi All,
> >>> >
> >>> >
> >>> >Yes I need  to change to numeric  because I am preparing a data set
> >>> >for
> >>> >further  analysis. The variable to be changed  from character to
> >>> >numeric
> >>> >(in this case, sex) will be a response variable.  Some records have
> >>> >missing
> >>> >observation on sex and it is blank.
> >>> > id  sex
> >>> >  1
> >>> >  2
> >>> >  3  M
> >>> >  4  F
> >>> >  5  M
> >>> >  6  F
> >>> >  7  F
> >>> >
> >>> >I am reading the data like this
> >>> >
> >>> >mydata <- read.csv(header=TRUE, text=', sep=", ")
> >>> > id  sex
> >>> >  1   NA
> >>> >  2  NA
> >>> >  3  M
> >>> >  4  F
> >>> >  5  M
> >>> >  6  F
> >>> >  7  F
> >>> >
> >>> >The  data set is huge   (>250,000)
> >>> >
> >>> >
> >>> >I want the output like this
> >>> >
> >>> > id  sexsex1
> >>> >  1   NA0
> >>> >  2  NA 0
> >>> >  3  M   1
> >>> >  4  F   2
> >>> >  5  M  1
> >>> >  6  F   2
> >>> >  7  F   2
> >>> >
> >>> >Thank you in advance
> >>> >
> >>> >
> >>> >On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com>
> >>wrote:
> >>> >
> >>> >> In line.
> >>> >>
> >>> >> John Kane
> >>> >> Kingston ON Canada
> >>> >>
> >>> >>
> >>> >> > -Original Message-
> >>> >> > From: valkr...@gmail.com
> >>> >> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
> >>> >> > To: istaz...@gmail.com
> >>> >> > Subject: Re: [R] If else
> >>> >> >
> >>> >> > I am trying to change the mydata$sex  from character to numeric
> >>> >>
> >>> >> Why?
> >>> >>  As Ista (mydata$confusingWillCauseProblemsLater) has pointed out
> >>> >this is
> >>> >> a very unusual thing to do in R.
> >>> >>
> >>> >> Is there a very specific reason for doing this in your analysis.
> >>> >> Otherwise it may better to leave the coding as NA. Some of the
> >>data
> >>> >mungers
> >>> >> here may be able to suggest which is the best strategy in R.
> >>> >>
> >>> >> R is 'weird' compared to more mundane stats packages such as SAS
> >>or
> >>> >SPSS
> >>> >> and common techniques that one would use with them often are not
> >>> >> appropriate in R.
> >>> >>
> >>> >>
> >>> >>
> >>> >>
> >>> >> > I want teh out put like
> >>> >> >id  sex
> >>> >> >   1  NA   0
> >>> >> >   2  NA   0
> >>> >> >   3  M 1
> >>> >> >   4  F 2
> >>> >> >   5  M1
> >>> >> >   6  F 2
> >>> >> >   7  F2
> >>> >> >
> >>> >> > mydata$sex1 <- 0
> >>> >> > if(mydata$sex =="M " ){
> >>> >> >   mydata$sex1<-1
> >>> >> > } else {
> &g

Re: [R] If else

2015-10-31 Thread Val
Hi All,


Yes I need  to change to numeric  because I am preparing a data set  for
further  analysis. The variable to be changed  from character to numeric
(in this case, sex) will be a response variable.  Some records have missing
observation on sex and it is blank.
 id  sex
  1
  2
  3  M
  4  F
  5  M
  6  F
  7  F

I am reading the data like this

mydata <- read.csv(header=TRUE, text=', sep=", ")
 id  sex
  1   NA
  2  NA
  3  M
  4  F
  5  M
  6  F
  7  F

The  data set is huge   (>250,000)


I want the output like this

 id  sexsex1
  1   NA0
  2  NA 0
  3  M   1
  4  F   2
  5  M  1
  6  F   2
  7  F   2

Thank you in advance


On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com> wrote:

> In line.
>
> John Kane
> Kingston ON Canada
>
>
> > -Original Message-
> > From: valkr...@gmail.com
> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
> > To: istaz...@gmail.com
> > Subject: Re: [R] If else
> >
> > I am trying to change the mydata$sex  from character to numeric
>
> Why?
>  As Ista (mydata$confusingWillCauseProblemsLater) has pointed out this is
> a very unusual thing to do in R.
>
> Is there a very specific reason for doing this in your analysis.
> Otherwise it may better to leave the coding as NA. Some of the data mungers
> here may be able to suggest which is the best strategy in R.
>
> R is 'weird' compared to more mundane stats packages such as SAS or SPSS
> and common techniques that one would use with them often are not
> appropriate in R.
>
>
>
>
> > I want teh out put like
> >id  sex
> >   1  NA   0
> >   2  NA   0
> >   3  M 1
> >   4  F 2
> >   5  M1
> >   6  F 2
> >   7  F2
> >
> > mydata$sex1 <- 0
> > if(mydata$sex =="M " ){
> >   mydata$sex1<-1
> > } else {
> >   mydata$sex1<-2
> > }
> >
> > mydata$sex1
> >
> > Warning message:In if (mydata$sex == "M ") { :
> >   the condition has length > 1 and only the first element will be
> > used> mydata$sex1[1] 2 2 2 2 2 2 2 2
> >
> >>
> >
> >
> > On Fri, Oct 30, 2015 at 8:28 PM, Ista Zahn <istaz...@gmail.com> wrote:
> >
> >> Using numeric for missing sounds like asking for trouble. But if you
> >> must, something like
> >>
> >> mydata$confusingWillCauseProblemsLater <-
> >>   ifelse(
> >> is.na(mydata$sex),
> >> 0,
> >> as.numeric(factor(mydata$sex,
> >>   levels = c("M", "F"
> >>
> >> should do it.
> >>
> >> Best,
> >> Ista
> >>
> >> On Fri, Oct 30, 2015 at 9:15 PM, Val <valkr...@gmail.com> wrote:
> >>> Hi all,
> >>> Iam trying to change character  to numeric but have probelm
> >>>
> >>> mydata <- read.table(header=TRUE, text=', sep=" "
> >>>  id  sex
> >>>   1  NA
> >>>   2  NA
> >>>   3  M
> >>>   4  F
> >>>   5  M
> >>>   6  F
> >>>   7  F
> >>>')
> >>>
> >>> if sex is missing then sex=0;
> >>> if sex is"M" then sex=1;
> >>> if sex is"F" then sex=2;
> >>>
> >>> Any help please ?
> >>>
> >>> [[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.
>
> 
> FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on
> your desktop!
> Check it out at http://www.inbox.com/marineaquarium
>
>
>

[[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] If else

2015-10-31 Thread Jeff Newmiller
You haven't actually answered John's question as to the type of analysis you 
plan to do. It still looks from here like you should be using factor data 
rather than numeric, but since you are not being clear we cannot give specifics 
as to how to proceed.
---
Jeff NewmillerThe .   .  Go Live...
DCN:<jdnew...@dcn.davis.ca.us>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 October 31, 2015 8:23:05 AM PDT, Val <valkr...@gmail.com> wrote:
>Hi All,
>
>
>Yes I need  to change to numeric  because I am preparing a data set 
>for
>further  analysis. The variable to be changed  from character to
>numeric
>(in this case, sex) will be a response variable.  Some records have
>missing
>observation on sex and it is blank.
> id  sex
>  1
>  2
>  3  M
>  4  F
>  5  M
>  6  F
>  7  F
>
>I am reading the data like this
>
>mydata <- read.csv(header=TRUE, text=', sep=", ")
> id  sex
>  1   NA
>  2  NA
>  3  M
>  4  F
>  5  M
>  6  F
>  7  F
>
>The  data set is huge   (>250,000)
>
>
>I want the output like this
>
> id  sexsex1
>  1   NA0
>  2  NA 0
>  3  M   1
>  4  F   2
>  5  M  1
>  6  F   2
>  7  F   2
>
>Thank you in advance
>
>
>On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com> wrote:
>
>> In line.
>>
>> John Kane
>> Kingston ON Canada
>>
>>
>> > -Original Message-
>> > From: valkr...@gmail.com
>> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
>> > To: istaz...@gmail.com
>> > Subject: Re: [R] If else
>> >
>> > I am trying to change the mydata$sex  from character to numeric
>>
>> Why?
>>  As Ista (mydata$confusingWillCauseProblemsLater) has pointed out
>this is
>> a very unusual thing to do in R.
>>
>> Is there a very specific reason for doing this in your analysis.
>> Otherwise it may better to leave the coding as NA. Some of the data
>mungers
>> here may be able to suggest which is the best strategy in R.
>>
>> R is 'weird' compared to more mundane stats packages such as SAS or
>SPSS
>> and common techniques that one would use with them often are not
>> appropriate in R.
>>
>>
>>
>>
>> > I want teh out put like
>> >id  sex
>> >   1  NA   0
>> >   2  NA   0
>> >   3  M 1
>> >   4  F 2
>> >   5  M1
>> >   6  F 2
>> >   7  F2
>> >
>> > mydata$sex1 <- 0
>> > if(mydata$sex =="M " ){
>> >   mydata$sex1<-1
>> > } else {
>> >   mydata$sex1<-2
>> > }
>> >
>> > mydata$sex1
>> >
>> > Warning message:In if (mydata$sex == "M ") { :
>> >   the condition has length > 1 and only the first element will be
>> > used> mydata$sex1[1] 2 2 2 2 2 2 2 2
>> >
>> >>
>> >
>> >
>> > On Fri, Oct 30, 2015 at 8:28 PM, Ista Zahn <istaz...@gmail.com>
>wrote:
>> >
>> >> Using numeric for missing sounds like asking for trouble. But if
>you
>> >> must, something like
>> >>
>> >> mydata$confusingWillCauseProblemsLater <-
>> >>   ifelse(
>> >> is.na(mydata$sex),
>> >> 0,
>> >> as.numeric(factor(mydata$sex,
>> >>   levels = c("M", "F"
>> >>
>> >> should do it.
>> >>
>> >> Best,
>> >> Ista
>> >>
>> >> On Fri, Oct 30, 2015 at 9:15 PM, Val <valkr...@gmail.com> wrote:
>> >>> Hi all,
>> >>> Iam trying to change character  to numeric but have probelm
>> >>>
>> >>> mydata <- read.table(header=TRUE, text=', sep=" "
>> >>>  id  sex
>> >>>   1  NA
>> >>>   2  NA
>> >>>   3  M
>> >>>   4  F
>> >>>   5  M
>> 

Re: [R] If else

2015-10-31 Thread Duncan Murdoch
looks from here like you should be using
>>> factor
>>>> data rather than numeric, but since you are not being clear we cannot
>>> give
>>>> specifics as to how to proceed.
>>>>
>>> ---
>>>> Jeff NewmillerThe .   .  Go
>>> Live...
>>>> DCN:<jdnew...@dcn.davis.ca.us>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 October 31, 2015 8:23:05 AM PDT, Val <valkr...@gmail.com> wrote:
>>>>> Hi All,
>>>>>
>>>>>
>>>>> Yes I need  to change to numeric  because I am preparing a data set
>>>>> for
>>>>> further  analysis. The variable to be changed  from character to
>>>>> numeric
>>>>> (in this case, sex) will be a response variable.  Some records have
>>>>> missing
>>>>> observation on sex and it is blank.
>>>>> id  sex
>>>>>  1
>>>>>  2
>>>>>      3  M
>>>>>  4  F
>>>>>  5  M
>>>>>  6  F
>>>>>  7  F
>>>>>
>>>>> I am reading the data like this
>>>>>
>>>>> mydata <- read.csv(header=TRUE, text=', sep=", ")
>>>>> id  sex
>>>>>  1   NA
>>>>>  2  NA
>>>>>  3  M
>>>>>  4  F
>>>>>  5  M
>>>>>  6  F
>>>>>  7  F
>>>>>
>>>>> The  data set is huge   (>250,000)
>>>>>
>>>>>
>>>>> I want the output like this
>>>>>
>>>>> id  sexsex1
>>>>>  1   NA0
>>>>>  2  NA 0
>>>>>  3  M   1
>>>>>  4  F   2
>>>>>  5  M  1
>>>>>  6  F   2
>>>>>  7  F   2
>>>>>
>>>>> Thank you in advance
>>>>>
>>>>>
>>>>> On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com>
>>> wrote:
>>>>>
>>>>>> In line.
>>>>>>
>>>>>> John Kane
>>>>>> Kingston ON Canada
>>>>>>
>>>>>>
>>>>>>> -Original Message-
>>>>>>> From: valkr...@gmail.com
>>>>>>> Sent: Fri, 30 Oct 2015 20:40:03 -0500
>>>>>>> To: istaz...@gmail.com
>>>>>>> Subject: Re: [R] If else
>>>>>>>
>>>>>>> I am trying to change the mydata$sex  from character to numeric
>>>>>>
>>>>>> Why?
>>>>>>  As Ista (mydata$confusingWillCauseProblemsLater) has pointed out
>>>>> this is
>>>>>> a very unusual thing to do in R.
>>>>>>
>>>>>> Is there a very specific reason for doing this in your analysis.
>>>>>> Otherwise it may better to leave the coding as NA. Some of the
>>> data
>>>>> mungers
>>>>>> here may be able to suggest which is the best strategy in R.
>>>>>>
>>>>>> R is 'weird' compared to more mundane stats packages such as SAS
>>> or
>>>>> SPSS
>>>>>> and common techniques that one would use with them often are not
>>>>>> appropriate in R.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>> I want teh out put like
>>>>>>>id  sex
>>>>>>>   1  NA   0
>>>>>>>   2  NA   0
>>>>>>>   3  M 1
>>>>>>>   4  F 2
>>>>>>>   5  M1
>>>>>>>   6  F 2
>>>>>>>   7  F2
>>>

Re: [R] If else

2015-10-31 Thread Rolf Turner


Ted:  You are either being deliberately obtuse or playing Devil's 
advocate or just stirring.  It is clear from his/her posts that the OP 
has limited understanding of both R and statistics.  Your sophisticated 
philosophising about the possibility of "three sexes" is very unlikely 
to have anything to do with what the OP wishes to accomplish.


The advice requested was not about how to treat "NA" as a "third sex"
but about how to convert categorical data coded as NA, "M", and "F" to 
numeric.  Which cannot possibly be a good idea.


It is not productive to encourage the OP to persist with wrong-headed 
notions.  Instead he or she should be encouraged to get to grips with 
the real issues of the analysis and to understand that treating 
categorical data as numeric is a recipe for disaster.


cheers,

Rolf

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

On 01/11/15 08:47, Ted Harding wrote:


[Apologies if the message below should arrive twice. When first
sent there was apparently something wrong with the email address
to r-help, and it was held for moderation because "Message has
implicit destination" (whatever that means). I have made sure
that this time the email address is correct.]

John Fox has given a neat expression to achieve the desired result!

I would like to comment, however, on the somewhat insistent criticism
of Val's request from several people.

It can make sense to have three "sex"es. Suppose, for example,
that the data are records of street crime reported by victims.
The victim may be able to identify the sex of the preprator
as definitely "M", or definitely "F". One of the aims of the
analysis is to investgate whether there is an association
between the gender of the offender and the type of crime.

But in some cases the victim may not have been able to recognise
the offender's sex. Then it would have to go in the record as "NA"
(or equivalent). There can be two kinds of reason why the victim
was unable to recognise the sex. One kind is where the victim
simply did not see the offender (e.g. their purse was stolen
while they were concentrating on something else, and they only
found out later). Another kind is where the offender deliberately
disguises their gender, so that it cannot be determined from their
appearance. This second kind could be associated with a particular
category of crime (and I leave it to people's lurid imaginations
to think of possible examples ... ).

Then one indeed has three "sex"es: Male, Female, and Indeterminate,
for each of which there is a potential assoctiation with type of crime.
With most analyses, however, a category of "NA" would be ignored
(at least by R).

And then one has a variable which is a factor with 3 levels, all
of which can (as above) be meaningful), and "NA" would not be
ignored.

Hoping this helps to clarify! (And, Val, does the above somehow
correspond to your objectives).

Best wishes to all,
Ted


__
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] If else

2015-10-31 Thread Jim Lemon
ew...@dcn.davis.ca.us>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 October 31, 2015 10:15:33 AM PDT, Val <valkr...@gmail.com> wrote:
> >>> Hi Jeff,
> >>>
> >>> I thought I answered. Yes I was not clear about it.  The further
> >>> analysis
> >>> will no  be done by R.  It is another software that will not accept a
> >>> character response variable.
> >>>
> >>> Why R is so complicated to do that.  If it is SAS then I can do it on
> >>> one
> >>> statement. .
> >>>
> >>>
> >>> On Sat, Oct 31, 2015 at 11:39 AM, Jeff Newmiller
> >>> <jdnew...@dcn.davis.ca.us>
> >>> wrote:
> >>>
> >>>> You haven't actually answered John's question as to the type of
> >>> analysis
> >>>> you plan to do. It still looks from here like you should be using
> >>> factor
> >>>> data rather than numeric, but since you are not being clear we cannot
> >>> give
> >>>> specifics as to how to proceed.
> >>>>
> >>>
> ---
> >>>> Jeff NewmillerThe .   .  Go
> >>> Live...
> >>>> DCN:<jdnew...@dcn.davis.ca.us>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 October 31, 2015 8:23:05 AM PDT, Val <valkr...@gmail.com> wrote:
> >>>>> Hi All,
> >>>>>
> >>>>>
> >>>>> Yes I need  to change to numeric  because I am preparing a data set
> >>>>> for
> >>>>> further  analysis. The variable to be changed  from character to
> >>>>> numeric
> >>>>> (in this case, sex) will be a response variable.  Some records have
> >>>>> missing
> >>>>> observation on sex and it is blank.
> >>>>> id  sex
> >>>>>  1
> >>>>>  2
> >>>>>  3  M
> >>>>>  4  F
> >>>>>  5  M
> >>>>>  6  F
> >>>>>  7  F
> >>>>>
> >>>>> I am reading the data like this
> >>>>>
> >>>>> mydata <- read.csv(header=TRUE, text=', sep=", ")
> >>>>> id  sex
> >>>>>  1   NA
> >>>>>  2  NA
> >>>>>  3  M
> >>>>>  4  F
> >>>>>  5  M
> >>>>>  6  F
> >>>>>  7  F
> >>>>>
> >>>>> The  data set is huge   (>250,000)
> >>>>>
> >>>>>
> >>>>> I want the output like this
> >>>>>
> >>>>> id  sexsex1
> >>>>>  1   NA0
> >>>>>  2  NA 0
> >>>>>  3  M   1
> >>>>>  4  F   2
> >>>>>  5  M  1
> >>>>>  6  F   2
> >>>>>  7  F   2
> >>>>>
> >>>>> Thank you in advance
> >>>>>
> >>>>>
> >>>>> On Sat, Oct 31, 2015 at 5:59 AM, John Kane <jrkrid...@inbox.com>
> >>> wrote:
> >>>>>
> >>>>>> In line.
> >>>>>>
> >>>>>> John Kane
> >>>>>> Kingston ON Canada
> >>>>>>
> >>>>>>
> >&g

Re: [R] If else

2015-10-30 Thread Ista Zahn
Using numeric for missing sounds like asking for trouble. But if you
must, something like

mydata$confusingWillCauseProblemsLater <-
  ifelse(
is.na(mydata$sex),
0,
as.numeric(factor(mydata$sex,
  levels = c("M", "F"

should do it.

Best,
Ista

On Fri, Oct 30, 2015 at 9:15 PM, Val  wrote:
> Hi all,
> Iam trying to change character  to numeric but have probelm
>
> mydata <- read.table(header=TRUE, text=', sep=" "
>  id  sex
>   1  NA
>   2  NA
>   3  M
>   4  F
>   5  M
>   6  F
>   7  F
>')
>
> if sex is missing then sex=0;
> if sex is"M" then sex=1;
> if sex is"F" then sex=2;
>
> Any help please ?
>
> [[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] If else

2015-10-30 Thread Val
Hi all,
Iam trying to change character  to numeric but have probelm

mydata <- read.table(header=TRUE, text=', sep=" "
 id  sex
  1  NA
  2  NA
  3  M
  4  F
  5  M
  6  F
  7  F
   ')

if sex is missing then sex=0;
if sex is"M" then sex=1;
if sex is"F" then sex=2;

Any help please ?

[[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] If else

2015-10-30 Thread Val
I am trying to change the mydata$sex  from character to numeric
I want teh out put like
   id  sex
  1  NA   0
  2  NA   0
  3  M 1
  4  F 2
  5  M1
  6  F 2
  7  F2

mydata$sex1 <- 0
if(mydata$sex =="M " ){
  mydata$sex1<-1
} else {
  mydata$sex1<-2
}

mydata$sex1

Warning message:In if (mydata$sex == "M ") { :
  the condition has length > 1 and only the first element will be
used> mydata$sex1[1] 2 2 2 2 2 2 2 2

>


On Fri, Oct 30, 2015 at 8:28 PM, Ista Zahn  wrote:

> Using numeric for missing sounds like asking for trouble. But if you
> must, something like
>
> mydata$confusingWillCauseProblemsLater <-
>   ifelse(
> is.na(mydata$sex),
> 0,
> as.numeric(factor(mydata$sex,
>   levels = c("M", "F"
>
> should do it.
>
> Best,
> Ista
>
> On Fri, Oct 30, 2015 at 9:15 PM, Val  wrote:
> > Hi all,
> > Iam trying to change character  to numeric but have probelm
> >
> > mydata <- read.table(header=TRUE, text=', sep=" "
> >  id  sex
> >   1  NA
> >   2  NA
> >   3  M
> >   4  F
> >   5  M
> >   6  F
> >   7  F
> >')
> >
> > if sex is missing then sex=0;
> > if sex is"M" then sex=1;
> > if sex is"F" then sex=2;
> >
> > Any help please ?
> >
> > [[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] if else statement for rain data to define zero for dry and one to wet

2015-06-07 Thread roslinazairimah zakaria
Dear all,

All works well. Thank you so much for your help.

D## Function 1
wet_dry1 - function(x,thresh=0.1)
 { for(column in 1:dim(x)[2]) x[,column] - ifelse(x[,column]=thresh,1,0)
 return(x)
 }

wet_dry1(dt)


## Function 2
wet_dry2 - ( dt = 0.1)*1
wet_dry2

wet_total - colSums(wet_dry2)
pp - wet_total/nrow(dt)
pp


## Function 3
rain - dt
wet_dry3 - ifelse(rain = 0.1, 1, 0)
wet_dry3

On Sun, Jun 7, 2015 at 5:48 AM, William Dunlap wdun...@tibco.com wrote:

 Your f1() has an unneeded for loop in it.
f1a - function(mat) mat  0.1, 1, 0)
 would do the same thing in a bit less time.

 However, I think that a simple
mat  0.1
 would be preferable.  The resulting TRUEs and FALSEs
 are easier to interpret than the 1s and 0s that f1a()
 produces and arithmetic functions treat them TRUE
 as 1 and FALSE as 0 internally.  E.g., mean(mat0.1)
 gives the proportion of wet(tish) days.



 Bill Dunlap
 TIBCO Software
 wdunlap tibco.com

 On Sat, Jun 6, 2015 at 1:55 PM, Dennis Murphy djmu...@gmail.com wrote:

 I'm sorry, but I have to take issue with this particular use case of
 ifelse(). When the goal is to generate a logical vector, ifelse() is
 very inefficient. It's better to apply a logical condition directly to
 the object in question and multiply the result by 1 to make it
 numeric/integer rather than logical.

 To illustrate this, consider the following toy example. The function
 f1 replicates the suggestion to apply ifelse() columnwise (with the
 additional overhead of preallocating storage for the result), whereas
 the function f2 applies the logical condition on the matrix itself
 using vectorization, with the recognition that a matrix is an atomic
 vector with a dim attribute.

 set.seed(5290)

 # 1000 x 1000 matrix
 m - matrix(sample(c(0, 0.05, 0.2), 1e6, replace = TRUE), ncol = 1000)

 f1 - function(mat)
   {
  newmat - matrix(NA, ncol = ncol(mat), nrow = nrow(mat))
  for(i in seq_len(ncol(mat)))
  newmat[, i] - ifelse(mat[, i]  0.1, 1, 0)
  newmat
   }

 f2 - function(mat) 1 * (mat  0.1)


 On my system, I got

  system.time(m1 - f1(m))
user  system elapsed
0.140.000.14

  system.time(m2 - f2(m))
user  system elapsed
0.010.000.01

  identical(m1, m2)
 [1] TRUE

 The all too common practice of using  ifelse(condition, 1, 0) on an
 atomic vector is easily replaced by 1 * (condition), where the result
 of condition is a logical atomic object coerced to numeric.

 To reduce memory, one should better define f2 as

 f2 - function(mat) 1L * (mat  0.1)

 but doing so in this example no longer creates identical objects since

  typeof(m1)
 [1] double

 Thus, f1 is not only inefficient in terms of execution time, it's also
 inefficient in terms of storage.

 Given several recent warnings in this forum about the inefficiency of
 ifelse() and the dozens of times I've seen the idiom implemented in f1
 as a solution over the last several years (to which I have likely
 contributed in my distant past as an R-helper), I felt compelled to
 say something about this practice, which BTW extends not just to 0/1
 return values but to
 0/x return values, where x is a nonzero real number.

 Dennis


 On Sat, Jun 6, 2015 at 12:50 AM, Jim Lemon drjimle...@gmail.com wrote:
  Hi rosalinazairimah,
  I think the problem is that you are using if instead of ifelse. Try
 this:
 
  wet_dry-function(x,thresh=0.1) {
   for(column in 1:dim(x)[2]) x[,column]-ifelse(x[,column]=thresh,1,0)
   return(x)
  }
  wet_dry(dt)
 
  and see what you get.
 
  Also, why can I read your message perfectly while everybody else can't?
 
  Jim
 
  -Original Message-
  From: roslina...@gmail.com
  Sent: Fri, 5 Jun 2015 16:49:08 +0800
  To: r-help@r-project.org
  Subject: [R] if else statement for rain data to define zero for dry
 and
  one to wet
 
  Dear r-users,
 
  I have a set of rain data:
 
  X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960
 X1961
  X1962
 
  1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0
  0.0
 
 
  2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0
  0.0
 
 
  3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0
 11.0
 
 
  4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0
  5.5
 
 
  5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0
 15.9
 
 
  I would like to go through each column and define each cell with value
  greater than 0.1 mm will be 1 and else zero. Hence I would like to
 attach
  the rain data and the category side by side:
 
 
  1950   state
 
  1 0.00
 
  2 0.00
 
  3 25.3   1
 
  4 12.7   1
 
  5 0.00
 
 
  ...
 
 
  This is my code:
 
 
  wet_dry  - function(dt)
 
  { cl   - length(dt)
 
tresh  - 0.1
 
 
for (i in 1:cl)
 
{  xi - dt[,i]
 
   if (xi  tresh ) 0 else 1
 
}
 
  dd - cbind(dt,xi)
 
  dd
 
  }
 
 
  wet_dry(dt)
 
 
  Results:
 
  wet_dry(dt)
 
 X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread Dennis Murphy
I'm sorry, but I have to take issue with this particular use case of
ifelse(). When the goal is to generate a logical vector, ifelse() is
very inefficient. It's better to apply a logical condition directly to
the object in question and multiply the result by 1 to make it
numeric/integer rather than logical.

To illustrate this, consider the following toy example. The function
f1 replicates the suggestion to apply ifelse() columnwise (with the
additional overhead of preallocating storage for the result), whereas
the function f2 applies the logical condition on the matrix itself
using vectorization, with the recognition that a matrix is an atomic
vector with a dim attribute.

set.seed(5290)

# 1000 x 1000 matrix
m - matrix(sample(c(0, 0.05, 0.2), 1e6, replace = TRUE), ncol = 1000)

f1 - function(mat)
  {
 newmat - matrix(NA, ncol = ncol(mat), nrow = nrow(mat))
 for(i in seq_len(ncol(mat)))
 newmat[, i] - ifelse(mat[, i]  0.1, 1, 0)
 newmat
  }

f2 - function(mat) 1 * (mat  0.1)


On my system, I got

 system.time(m1 - f1(m))
   user  system elapsed
   0.140.000.14

 system.time(m2 - f2(m))
   user  system elapsed
   0.010.000.01

 identical(m1, m2)
[1] TRUE

The all too common practice of using  ifelse(condition, 1, 0) on an
atomic vector is easily replaced by 1 * (condition), where the result
of condition is a logical atomic object coerced to numeric.

To reduce memory, one should better define f2 as

f2 - function(mat) 1L * (mat  0.1)

but doing so in this example no longer creates identical objects since

 typeof(m1)
[1] double

Thus, f1 is not only inefficient in terms of execution time, it's also
inefficient in terms of storage.

Given several recent warnings in this forum about the inefficiency of
ifelse() and the dozens of times I've seen the idiom implemented in f1
as a solution over the last several years (to which I have likely
contributed in my distant past as an R-helper), I felt compelled to
say something about this practice, which BTW extends not just to 0/1
return values but to
0/x return values, where x is a nonzero real number.

Dennis


On Sat, Jun 6, 2015 at 12:50 AM, Jim Lemon drjimle...@gmail.com wrote:
 Hi rosalinazairimah,
 I think the problem is that you are using if instead of ifelse. Try this:

 wet_dry-function(x,thresh=0.1) {
  for(column in 1:dim(x)[2]) x[,column]-ifelse(x[,column]=thresh,1,0)
  return(x)
 }
 wet_dry(dt)

 and see what you get.

 Also, why can I read your message perfectly while everybody else can't?

 Jim

 -Original Message-
 From: roslina...@gmail.com
 Sent: Fri, 5 Jun 2015 16:49:08 +0800
 To: r-help@r-project.org
 Subject: [R] if else statement for rain data to define zero for dry and
 one to wet

 Dear r-users,

 I have a set of rain data:

 X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
 X1962

 1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0   0.0


 2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0   0.0


 3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0  11.0


 4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0   5.5


 5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0  15.9


 I would like to go through each column and define each cell with value
 greater than 0.1 mm will be 1 and else zero. Hence I would like to attach
 the rain data and the category side by side:


 1950   state

 1 0.00

 2 0.00

 3 25.3   1

 4 12.7   1

 5 0.00


 ...


 This is my code:


 wet_dry  - function(dt)

 { cl   - length(dt)

   tresh  - 0.1


   for (i in 1:cl)

   {  xi - dt[,i]

  if (xi  tresh ) 0 else 1

   }

 dd - cbind(dt,xi)

 dd

 }


 wet_dry(dt)


 Results:

 wet_dry(dt)

X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960
 X1961
 X1962 X1963 X1964 X1965 X1966 X1967 X1968 X1969 X1970 X1971 X1972 X1973
 X1974 X1975 X1976 X1977

 10.0   0.0  14.3   0.0  13.5  13.2   4.0   0.0   3.3   0.0   0.0
 0.0
   4.2   0.0   2.2   0.0   4.4   5.1 0   7.2   0.0   0.0   0.0   5.1
 0   0.0 0   0.3

 20.0   0.0  21.9   0.0  10.9   6.6   2.1   0.0   0.0   0.0   0.0
 0.0
   8.4   0.0   4.0   0.0   4.9   0.7 0   0.0   0.0   0.0   0.0   5.4
 0   3.3 0   0.3

 3   25.3   6.7  18.6   0.8   2.3   0.0   8.0   0.0   0.0   0.0   0.0
 11.0
   4.2   0.0   2.0   0.0  14.2  17.1 0   0.0   0.0   0.0   0.0   2.1
 0   1.7 0   4.4

 4   12.7   3.4  37.2   0.9   8.4   0.0   5.8   0.0   0.0   0.0   0.0
 5.5
   0.0   0.0   5.4   0.0   6.4  14.9 0  10.1   2.9 143.4   0.0   6.1
 0   0.0 0  33.5


 It does not work and give me the original data.  Why is that?


 Thank you so much for your help.

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

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread William Dunlap
Your f1() has an unneeded for loop in it.
   f1a - function(mat) mat  0.1, 1, 0)
would do the same thing in a bit less time.

However, I think that a simple
   mat  0.1
would be preferable.  The resulting TRUEs and FALSEs
are easier to interpret than the 1s and 0s that f1a()
produces and arithmetic functions treat them TRUE
as 1 and FALSE as 0 internally.  E.g., mean(mat0.1)
gives the proportion of wet(tish) days.



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Jun 6, 2015 at 1:55 PM, Dennis Murphy djmu...@gmail.com wrote:

 I'm sorry, but I have to take issue with this particular use case of
 ifelse(). When the goal is to generate a logical vector, ifelse() is
 very inefficient. It's better to apply a logical condition directly to
 the object in question and multiply the result by 1 to make it
 numeric/integer rather than logical.

 To illustrate this, consider the following toy example. The function
 f1 replicates the suggestion to apply ifelse() columnwise (with the
 additional overhead of preallocating storage for the result), whereas
 the function f2 applies the logical condition on the matrix itself
 using vectorization, with the recognition that a matrix is an atomic
 vector with a dim attribute.

 set.seed(5290)

 # 1000 x 1000 matrix
 m - matrix(sample(c(0, 0.05, 0.2), 1e6, replace = TRUE), ncol = 1000)

 f1 - function(mat)
   {
  newmat - matrix(NA, ncol = ncol(mat), nrow = nrow(mat))
  for(i in seq_len(ncol(mat)))
  newmat[, i] - ifelse(mat[, i]  0.1, 1, 0)
  newmat
   }

 f2 - function(mat) 1 * (mat  0.1)


 On my system, I got

  system.time(m1 - f1(m))
user  system elapsed
0.140.000.14

  system.time(m2 - f2(m))
user  system elapsed
0.010.000.01

  identical(m1, m2)
 [1] TRUE

 The all too common practice of using  ifelse(condition, 1, 0) on an
 atomic vector is easily replaced by 1 * (condition), where the result
 of condition is a logical atomic object coerced to numeric.

 To reduce memory, one should better define f2 as

 f2 - function(mat) 1L * (mat  0.1)

 but doing so in this example no longer creates identical objects since

  typeof(m1)
 [1] double

 Thus, f1 is not only inefficient in terms of execution time, it's also
 inefficient in terms of storage.

 Given several recent warnings in this forum about the inefficiency of
 ifelse() and the dozens of times I've seen the idiom implemented in f1
 as a solution over the last several years (to which I have likely
 contributed in my distant past as an R-helper), I felt compelled to
 say something about this practice, which BTW extends not just to 0/1
 return values but to
 0/x return values, where x is a nonzero real number.

 Dennis


 On Sat, Jun 6, 2015 at 12:50 AM, Jim Lemon drjimle...@gmail.com wrote:
  Hi rosalinazairimah,
  I think the problem is that you are using if instead of ifelse. Try
 this:
 
  wet_dry-function(x,thresh=0.1) {
   for(column in 1:dim(x)[2]) x[,column]-ifelse(x[,column]=thresh,1,0)
   return(x)
  }
  wet_dry(dt)
 
  and see what you get.
 
  Also, why can I read your message perfectly while everybody else can't?
 
  Jim
 
  -Original Message-
  From: roslina...@gmail.com
  Sent: Fri, 5 Jun 2015 16:49:08 +0800
  To: r-help@r-project.org
  Subject: [R] if else statement for rain data to define zero for dry and
  one to wet
 
  Dear r-users,
 
  I have a set of rain data:
 
  X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
  X1962
 
  1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0
  0.0
 
 
  2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0
  0.0
 
 
  3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0
 11.0
 
 
  4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0
  5.5
 
 
  5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0
 15.9
 
 
  I would like to go through each column and define each cell with value
  greater than 0.1 mm will be 1 and else zero. Hence I would like to
 attach
  the rain data and the category side by side:
 
 
  1950   state
 
  1 0.00
 
  2 0.00
 
  3 25.3   1
 
  4 12.7   1
 
  5 0.00
 
 
  ...
 
 
  This is my code:
 
 
  wet_dry  - function(dt)
 
  { cl   - length(dt)
 
tresh  - 0.1
 
 
for (i in 1:cl)
 
{  xi - dt[,i]
 
   if (xi  tresh ) 0 else 1
 
}
 
  dd - cbind(dt,xi)
 
  dd
 
  }
 
 
  wet_dry(dt)
 
 
  Results:
 
  wet_dry(dt)
 
 X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960
  X1961
  X1962 X1963 X1964 X1965 X1966 X1967 X1968 X1969 X1970 X1971 X1972 X1973
  X1974 X1975 X1976 X1977
 
  10.0   0.0  14.3   0.0  13.5  13.2   4.0   0.0   3.3   0.0   0.0
  0.0
4.2   0.0   2.2   0.0   4.4   5.1 0   7.2   0.0   0.0   0.0   5.1
  0   0.0 0   0.3
 
  20.0   0.0  21.9   0.0  10.9   6.6   2.1   0.0   0.0   0.0   0.0
  0.0
8.4   0.0   4.0   0.0   4.9   0.7 0   0.0   0.0   0.0   0.0   5.4
  0   3.3 0   0.3
 
  3   25.3

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread Jim Lemon
Hi rosalinazairimah,
I think the problem is that you are using if instead of ifelse. Try this:

wet_dry-function(x,thresh=0.1) {
 for(column in 1:dim(x)[2]) x[,column]-ifelse(x[,column]=thresh,1,0)
 return(x)
}
wet_dry(dt)

and see what you get.

Also, why can I read your message perfectly while everybody else can't?

Jim

 -Original Message-
 From: roslina...@gmail.com
 Sent: Fri, 5 Jun 2015 16:49:08 +0800
 To: r-help@r-project.org
 Subject: [R] if else statement for rain data to define zero for dry and
 one to wet

 Dear r-users,

 I have a set of rain data:

 X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
 X1962

 1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0   0.0


 2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0   0.0


 3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0  11.0


 4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0   5.5


 5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0  15.9


 I would like to go through each column and define each cell with value
 greater than 0.1 mm will be 1 and else zero. Hence I would like to attach
 the rain data and the category side by side:


 1950   state

 1 0.00

 2 0.00

 3 25.3   1

 4 12.7   1

 5 0.00


 ...


 This is my code:


 wet_dry  - function(dt)

 { cl   - length(dt)

   tresh  - 0.1


   for (i in 1:cl)

   {  xi - dt[,i]

  if (xi  tresh ) 0 else 1

   }

 dd - cbind(dt,xi)

 dd

 }


 wet_dry(dt)


 Results:

 wet_dry(dt)

X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960
 X1961
 X1962 X1963 X1964 X1965 X1966 X1967 X1968 X1969 X1970 X1971 X1972 X1973
 X1974 X1975 X1976 X1977

 10.0   0.0  14.3   0.0  13.5  13.2   4.0   0.0   3.3   0.0   0.0
 0.0
   4.2   0.0   2.2   0.0   4.4   5.1 0   7.2   0.0   0.0   0.0   5.1
 0   0.0 0   0.3

 20.0   0.0  21.9   0.0  10.9   6.6   2.1   0.0   0.0   0.0   0.0
 0.0
   8.4   0.0   4.0   0.0   4.9   0.7 0   0.0   0.0   0.0   0.0   5.4
 0   3.3 0   0.3

 3   25.3   6.7  18.6   0.8   2.3   0.0   8.0   0.0   0.0   0.0   0.0
 11.0
   4.2   0.0   2.0   0.0  14.2  17.1 0   0.0   0.0   0.0   0.0   2.1
 0   1.7 0   4.4

 4   12.7   3.4  37.2   0.9   8.4   0.0   5.8   0.0   0.0   0.0   0.0
 5.5
   0.0   0.0   5.4   0.0   6.4  14.9 0  10.1   2.9 143.4   0.0   6.1
 0   0.0 0  33.5


 It does not work and give me the original data.  Why is that?


 Thank you so much for your help.

   [[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] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread roslinazairimah zakaria
Thank you jim.

On Saturday, June 6, 2015, Jim Lemon drjimle...@gmail.com wrote:

 Hi rosalinazairimah,
 I think the problem is that you are using if instead of ifelse. Try
 this:

 wet_dry-function(x,thresh=0.1) {
  for(column in 1:dim(x)[2]) x[,column]-ifelse(x[,column]=thresh,1,0)
  return(x)
 }
 wet_dry(dt)

 and see what you get.

 Also, why can I read your message perfectly while everybody else can't?

 Jim

  -Original Message-
  From: roslina...@gmail.com javascript:;
  Sent: Fri, 5 Jun 2015 16:49:08 +0800
  To: r-help@r-project.org javascript:;
  Subject: [R] if else statement for rain data to define zero for dry and
  one to wet
 
  Dear r-users,
 
  I have a set of rain data:
 
  X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
  X1962
 
  1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0
  0.0
 
 
  2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0
  0.0
 
 
  3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0
 11.0
 
 
  4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0
  5.5
 
 
  5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0
 15.9
 
 
  I would like to go through each column and define each cell with value
  greater than 0.1 mm will be 1 and else zero. Hence I would like to
 attach
  the rain data and the category side by side:
 
 
  1950   state
 
  1 0.00
 
  2 0.00
 
  3 25.3   1
 
  4 12.7   1
 
  5 0.00
 
 
  ...
 
 
  This is my code:
 
 
  wet_dry  - function(dt)
 
  { cl   - length(dt)
 
tresh  - 0.1
 
 
for (i in 1:cl)
 
{  xi - dt[,i]
 
   if (xi  tresh ) 0 else 1
 
}
 
  dd - cbind(dt,xi)
 
  dd
 
  }
 
 
  wet_dry(dt)
 
 
  Results:
 
  wet_dry(dt)
 
 X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960
  X1961
  X1962 X1963 X1964 X1965 X1966 X1967 X1968 X1969 X1970 X1971 X1972 X1973
  X1974 X1975 X1976 X1977
 
  10.0   0.0  14.3   0.0  13.5  13.2   4.0   0.0   3.3   0.0   0.0
  0.0
4.2   0.0   2.2   0.0   4.4   5.1 0   7.2   0.0   0.0   0.0   5.1
  0   0.0 0   0.3
 
  20.0   0.0  21.9   0.0  10.9   6.6   2.1   0.0   0.0   0.0   0.0
  0.0
8.4   0.0   4.0   0.0   4.9   0.7 0   0.0   0.0   0.0   0.0   5.4
  0   3.3 0   0.3
 
  3   25.3   6.7  18.6   0.8   2.3   0.0   8.0   0.0   0.0   0.0   0.0
  11.0
4.2   0.0   2.0   0.0  14.2  17.1 0   0.0   0.0   0.0   0.0   2.1
  0   1.7 0   4.4
 
  4   12.7   3.4  37.2   0.9   8.4   0.0   5.8   0.0   0.0   0.0   0.0
  5.5
0.0   0.0   5.4   0.0   6.4  14.9 0  10.1   2.9 143.4   0.0   6.1
  0   0.0 0  33.5
 
 
  It does not work and give me the original data.  Why is that?
 
 
  Thank you so much for your help.
 
[[alternative HTML version deleted]]
 
  __
  R-help@r-project.org javascript:; 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.


[R] if else statement for rain data to define zero for dry and one to wet

2015-06-05 Thread roslinazairimah zakaria
Dear r-users,

I have a set of rain data:

X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
X1962

1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0   0.0


2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0   0.0


3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0  11.0


4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0   5.5


5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0  15.9


I would like to go through each column and define each cell with value
greater than 0.1 mm will be 1 and else zero. Hence I would like to attach
the rain data and the category side by side:


1950   state

1 0.00

2 0.00

3 25.3   1

4 12.7   1

5 0.00


...


This is my code:


wet_dry  - function(dt)

{ cl   - length(dt)

  tresh  - 0.1


  for (i in 1:cl)

  {  xi - dt[,i]

 if (xi  tresh ) 0 else 1

  }

dd - cbind(dt,xi)

dd

}


wet_dry(dt)


Results:

 wet_dry(dt)

   X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
X1962 X1963 X1964 X1965 X1966 X1967 X1968 X1969 X1970 X1971 X1972 X1973
X1974 X1975 X1976 X1977

10.0   0.0  14.3   0.0  13.5  13.2   4.0   0.0   3.3   0.0   0.0   0.0
  4.2   0.0   2.2   0.0   4.4   5.1 0   7.2   0.0   0.0   0.0   5.1
0   0.0 0   0.3

20.0   0.0  21.9   0.0  10.9   6.6   2.1   0.0   0.0   0.0   0.0   0.0
  8.4   0.0   4.0   0.0   4.9   0.7 0   0.0   0.0   0.0   0.0   5.4
0   3.3 0   0.3

3   25.3   6.7  18.6   0.8   2.3   0.0   8.0   0.0   0.0   0.0   0.0  11.0
  4.2   0.0   2.0   0.0  14.2  17.1 0   0.0   0.0   0.0   0.0   2.1
0   1.7 0   4.4

4   12.7   3.4  37.2   0.9   8.4   0.0   5.8   0.0   0.0   0.0   0.0   5.5
  0.0   0.0   5.4   0.0   6.4  14.9 0  10.1   2.9 143.4   0.0   6.1
0   0.0 0  33.5


It does not work and give me the original data.  Why is that?


Thank you so much for your help.

[[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] if else statement for rain data to define zero for dry and one to wet

2015-06-05 Thread PIKAL Petr
Hi

I will not inspect your function as it is corrupted by HTML posting.

If your data frame is named rain

newrain - (rain.1)*1

gives you new data frame with reqired coding.

However I am not sure, what do you want to do next. Do you want to merge those 
2 data frames so as coded column is beside original column? Why? What do you 
want to do with such merged data? It seems to me without sense.

Cheers
Petr


 -Original Message-
 From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of
 roslinazairimah zakaria
 Sent: Friday, June 05, 2015 10:49 AM
 To: r-help@r-project.org
 Subject: [R] if else statement for rain data to define zero for dry and
 one to wet

 Dear r-users,

 I have a set of rain data:

 X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
 X1962

 1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0
 0.0


 2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0
 0.0


 3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0
 11.0


 4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0
 5.5


 5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0
 15.9


 I would like to go through each column and define each cell with value
 greater than 0.1 mm will be 1 and else zero. Hence I would like to
 attach the rain data and the category side by side:


 1950   state

 1 0.00

 2 0.00

 3 25.3   1

 4 12.7   1

 5 0.00


 ...


 This is my code:


 wet_dry  - function(dt)

 { cl   - length(dt)

   tresh  - 0.1


   for (i in 1:cl)

   {  xi - dt[,i]

  if (xi  tresh ) 0 else 1

   }

 dd - cbind(dt,xi)

 dd

 }


 wet_dry(dt)


 Results:

  wet_dry(dt)

X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960
 X1961
 X1962 X1963 X1964 X1965 X1966 X1967 X1968 X1969 X1970 X1971 X1972 X1973
 X1974 X1975 X1976 X1977

 10.0   0.0  14.3   0.0  13.5  13.2   4.0   0.0   3.3   0.0   0.0
 0.0
   4.2   0.0   2.2   0.0   4.4   5.1 0   7.2   0.0   0.0   0.0   5.1
 0   0.0 0   0.3

 20.0   0.0  21.9   0.0  10.9   6.6   2.1   0.0   0.0   0.0   0.0
 0.0
   8.4   0.0   4.0   0.0   4.9   0.7 0   0.0   0.0   0.0   0.0   5.4
 0   3.3 0   0.3

 3   25.3   6.7  18.6   0.8   2.3   0.0   8.0   0.0   0.0   0.0   0.0
 11.0
   4.2   0.0   2.0   0.0  14.2  17.1 0   0.0   0.0   0.0   0.0   2.1
 0   1.7 0   4.4

 4   12.7   3.4  37.2   0.9   8.4   0.0   5.8   0.0   0.0   0.0   0.0
 5.5
   0.0   0.0   5.4   0.0   6.4  14.9 0  10.1   2.9 143.4   0.0   6.1
 0   0.0 0  33.5


 It does not work and give me the original data.  Why is that?


 Thank you so much for your help.

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

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-05 Thread John Kane
Please do not post in HTML. It made your posting unreadable.  R-help is a plain 
text list and when it removes all the HTML tags often the result is gibberish

Have a look at 
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
 and http://adv-r.had.co.nz/Reproducibility.html for some suggestions on how to 
post to R-help.


John Kane
Kingston ON Canada


 -Original Message-
 From: roslina...@gmail.com
 Sent: Fri, 5 Jun 2015 16:49:08 +0800
 To: r-help@r-project.org
 Subject: [R] if else statement for rain data to define zero for dry and
 one to wet
 
 Dear r-users,
 
 I have a set of rain data:
 
 X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960 X1961
 X1962
 
 1   0.0   0.0  14.3   0.0  13.5  13.2   4.0 0   3.3 0 0   0.0
 
 
 2   0.0   0.0  21.9   0.0  10.9   6.6   2.1 0   0.0 0 0   0.0
 
 
 3  25.3   6.7  18.6   0.8   2.3   0.0   8.0 0   0.0 0 0  11.0
 
 
 4  12.7   3.4  37.2   0.9   8.4   0.0   5.8 0   0.0 0 0   5.5
 
 
 5   0.0   0.0  58.3   3.6  21.1   4.2   3.0 0   0.0 0 0  15.9
 
 
 I would like to go through each column and define each cell with value
 greater than 0.1 mm will be 1 and else zero. Hence I would like to attach
 the rain data and the category side by side:
 
 
 1950   state
 
 1 0.00
 
 2 0.00
 
 3 25.3   1
 
 4 12.7   1
 
 5 0.00
 
 
 ...
 
 
 This is my code:
 
 
 wet_dry  - function(dt)
 
 { cl   - length(dt)
 
   tresh  - 0.1
 
 
   for (i in 1:cl)
 
   {  xi - dt[,i]
 
  if (xi  tresh ) 0 else 1
 
   }
 
 dd - cbind(dt,xi)
 
 dd
 
 }
 
 
 wet_dry(dt)
 
 
 Results:
 
 wet_dry(dt)
 
X1950 X1951 X1952 X1953 X1954 X1955 X1956 X1957 X1958 X1959 X1960
 X1961
 X1962 X1963 X1964 X1965 X1966 X1967 X1968 X1969 X1970 X1971 X1972 X1973
 X1974 X1975 X1976 X1977
 
 10.0   0.0  14.3   0.0  13.5  13.2   4.0   0.0   3.3   0.0   0.0
 0.0
   4.2   0.0   2.2   0.0   4.4   5.1 0   7.2   0.0   0.0   0.0   5.1
 0   0.0 0   0.3
 
 20.0   0.0  21.9   0.0  10.9   6.6   2.1   0.0   0.0   0.0   0.0
 0.0
   8.4   0.0   4.0   0.0   4.9   0.7 0   0.0   0.0   0.0   0.0   5.4
 0   3.3 0   0.3
 
 3   25.3   6.7  18.6   0.8   2.3   0.0   8.0   0.0   0.0   0.0   0.0
 11.0
   4.2   0.0   2.0   0.0  14.2  17.1 0   0.0   0.0   0.0   0.0   2.1
 0   1.7 0   4.4
 
 4   12.7   3.4  37.2   0.9   8.4   0.0   5.8   0.0   0.0   0.0   0.0
 5.5
   0.0   0.0   5.4   0.0   6.4  14.9 0  10.1   2.9 143.4   0.0   6.1
 0   0.0 0  33.5
 
 
 It does not work and give me the original data.  Why is that?
 
 
 Thank you so much for your help.
 
   [[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.


Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords  protects your account.

__
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] if else for cumulative sum error

2014-12-03 Thread Jefferson Ferreira-Ferreira
Nice, David!!

Worked like a charm!!
Thank you very much.



Em Tue Dec 02 2014 at 19:22:48, David L Carlson dcarl...@tamu.edu
escreveu:

 Let's try a different approach. You don't need a loop for this. First we
 need a reproducible example:

  set.seed(42)
  dadosmax - data.frame(above=runif(150) + .5)

 Now compute your sums using cumsum() and diff() and then compute enchday
 using ifelse(). See the manual pages for each of these functions to
 understand how they work:

  sums - diff(c(0, cumsum(dadosmax$above)), 45)
  dadosmax$enchday - c(ifelse(sums = 45, 1, 0), rep(NA, 44))

  dadosmax$enchday
   [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
 1  1  1
  [26]  1  1  1  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  0
  [51]  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
  [76]  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
 1  1  1
 [101]  1  1  1  1  1  1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 NA NA
 [126] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 NA NA

 See the NA's? Those are what David Winsemius is talking about. For the
 106th value, 106+44 is 150, but for the 107th value 107+144 is 151 which
 does not exist. Fortunately diff() understands that and stops at 106, but
 we have to add 44 NA's because that is the number of rows in your data
 frame.

 You might find this plot informative as well:

  plot(sums, typ=l)
  abline(h=45)

 Another way to get there is to use sapply() which will add the NA's for us:

  sums - sapply(1:150, function(x) sum(dadosmax$above[x:(x+44)]))
  dadosmax$enchday - ifelse(sums = 45, 1, 0)

 But it won't be as fast if you have a large data set.

 -
 David L Carlson
 Department of Anthropology
 Texas AM University
 College Station, TX 77840-4352

 -Original Message-
 From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David
 Winsemius
 Sent: Tuesday, December 2, 2014 2:50 PM
 To: Jefferson Ferreira-Ferreira
 Cc: r-help@r-project.org
 Subject: Re: [R] if else for cumulative sum error


 On Dec 2, 2014, at 12:26 PM, Jefferson Ferreira-Ferreira wrote:

  Thank you for replies.
 
  David,
 
  I tried your modified form
 
  for (i in 1:seq_along(rownames(dadosmax))){


 No. it is either 1:  or seq_along(...). in this case perhaps
 1:(nrow(dadosmax)-44 would be safer

 You do not seem to have understood that you cannot use an index of i+44
 when i is going to be the entire set of rows of the dataframe. There is no
 there there to quote Gertrude Stein's slur against Oakland. In fact there
 is not there there at i+1 when you get to the end. You either need to only
 go to row

   dadosmax$enchday[i] - if ( (sum(dadosmax$above[i:(i+44)])) = 45) 1
 else
  0
  }
 
  However, I'm receiving this warning:
  Warning message:
  In 1:seq_along(rownames(dadosmax)) :
   numerical expression has 2720 elements: only the first used
 
  I can't figure out why only the first row was calculated...

 You should of course read these, but the error is not from your
 if-statement but rahter you for-loop-indexing.

 ?'if'
 ?ifelse


  Any ideas?
 
 
 
  Em Tue Dec 02 2014 at 15:22:25, John McKown 
 john.archie.mck...@gmail.com
  escreveu:
 
  On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira 
  jeco...@gmail.com wrote:
 
  Hello everybody;
 
  I'm writing a code where part of it is as follows:
 
  for (i in nrow(dadosmax)){
   dadosmax$enchday[i] - if (sum(dadosmax$above[i:(i+44)]) = 45) 1
 else 0
  }
 
 
  ​Without some test data for any validation, I would try the following
  formula
 
  dadosmax$enchday[i] - if
  (sum(dadosmax$above[i:(min(i+44,nrow(dadosmax)))] = 45) 1 else 0​
 
 
 
 
  That is for each row of my data frame, sum an specific column (0 or 1)
 of
  that row plus 44 rows. If It is =45 than enchday is 1 else 0.
 
  The following error is returned:
 
  Error in if (sum(dadosmax$above[i:(i + 44)]) = 45) 1 else 0 :
   missing value where TRUE/FALSE needed
 
  I've tested the ifelse statement assigning different values to i and it
  works. So I'm wondering if this error is due the fact that at the
 final of
  my data frame there aren't 45 rows to sum anymore. I tried to use try
  but
  It's simply hide the error.
 
  How can I deal with this? Any ideas?
  Thank you very much.
 
 [[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.
 
 
 
 
  --
  The temperature of the aqueous content of an unremittingly ogled
  culinary vessel will not achieve 100 degrees on the Celsius scale.
 
  Maranatha! 
  John McKown
 
 
[[alternative HTML version deleted

[R] if else for cumulative sum error

2014-12-02 Thread Jefferson Ferreira-Ferreira
Hello everybody;

I'm writing a code where part of it is as follows:

for (i in nrow(dadosmax)){
  dadosmax$enchday[i] - if (sum(dadosmax$above[i:(i+44)]) = 45) 1 else 0
}

That is for each row of my data frame, sum an specific column (0 or 1) of
that row plus 44 rows. If It is =45 than enchday is 1 else 0.

The following error is returned:

Error in if (sum(dadosmax$above[i:(i + 44)]) = 45) 1 else 0 :
  missing value where TRUE/FALSE needed

I've tested the ifelse statement assigning different values to i and it
works. So I'm wondering if this error is due the fact that at the final of
my data frame there aren't 45 rows to sum anymore. I tried to use try but
It's simply hide the error.

How can I deal with this? Any ideas?
Thank you very much.

[[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] if else for cumulative sum error

2014-12-02 Thread David Winsemius

On Dec 2, 2014, at 10:08 AM, Jefferson Ferreira-Ferreira wrote:

 Hello everybody;
 
 I'm writing a code where part of it is as follows:
 
 for (i in nrow(dadosmax)){
  dadosmax$enchday[i] - if (sum(dadosmax$above[i:(i+44)]) = 45) 1 else 0
 }
 
 That is for each row of my data frame, sum an specific column (0 or 1) of
 that row plus 44 rows. If It is =45 than enchday is 1 else 0.
 
 The following error is returned:
 
 Error in if (sum(dadosmax$above[i:(i + 44)]) = 45) 1 else 0 :
  missing value where TRUE/FALSE needed
 
 I've tested the ifelse statement assigning different values to i and it
 works. So I'm wondering if this error is due the fact that at the final of
 my data frame there aren't 45 rows to sum anymore. I tried to use try but
 It's simply hide the error.
 
 How can I deal with this? Any ideas?

You should think about what would happen at the high end of values (for what is 
now being erroneously coded) for `nrow(dadosmax)` which a assume was meant to 
be `1:nrow(dadosmax)`. Should have been `seq_along(rownames(dadosmax))`. But 
even if that error were fixed you would be referencing non-existent rows as 
soon as you were within 44 rows of the end of the dataframe.

-- 
David.


 Thank you very much.
 
   [[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.

David Winsemius
Alameda, CA, USA

__
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] if else for cumulative sum error

2014-12-02 Thread John McKown
On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira 
jeco...@gmail.com wrote:

 Hello everybody;

 I'm writing a code where part of it is as follows:

 for (i in nrow(dadosmax)){
   dadosmax$enchday[i] - if (sum(dadosmax$above[i:(i+44)]) = 45) 1 else 0
 }


​Without some test data for any validation, I would try the following
formula

dadosmax$enchday[i] - if (sum(dadosmax$above[i:(min(i+44,nrow(dadosmax)))]
= 45) 1 else 0​




 That is for each row of my data frame, sum an specific column (0 or 1) of
 that row plus 44 rows. If It is =45 than enchday is 1 else 0.

 The following error is returned:

 Error in if (sum(dadosmax$above[i:(i + 44)]) = 45) 1 else 0 :
   missing value where TRUE/FALSE needed

 I've tested the ifelse statement assigning different values to i and it
 works. So I'm wondering if this error is due the fact that at the final of
 my data frame there aren't 45 rows to sum anymore. I tried to use try but
 It's simply hide the error.

 How can I deal with this? Any ideas?
 Thank you very much.

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




-- 
The temperature of the aqueous content of an unremittingly ogled
culinary vessel will not achieve 100 degrees on the Celsius scale.

Maranatha! 
John McKown

[[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] if else for cumulative sum error

2014-12-02 Thread Jefferson Ferreira-Ferreira
Thank you for replies.

David,

I tried your modified form

for (i in 1:seq_along(rownames(dadosmax))){
  dadosmax$enchday[i] - if ( (sum(dadosmax$above[i:(i+44)])) = 45) 1 else
0
}

However, I'm receiving this warning:
Warning message:
In 1:seq_along(rownames(dadosmax)) :
  numerical expression has 2720 elements: only the first used

I can't figure out why only the first row was calculated...
Any ideas?



Em Tue Dec 02 2014 at 15:22:25, John McKown john.archie.mck...@gmail.com
escreveu:

 On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira 
 jeco...@gmail.com wrote:

 Hello everybody;

 I'm writing a code where part of it is as follows:

 for (i in nrow(dadosmax)){
   dadosmax$enchday[i] - if (sum(dadosmax$above[i:(i+44)]) = 45) 1 else 0
 }


 ​Without some test data for any validation, I would try the following
 formula

 dadosmax$enchday[i] - if
 (sum(dadosmax$above[i:(min(i+44,nrow(dadosmax)))] = 45) 1 else 0​




 That is for each row of my data frame, sum an specific column (0 or 1) of
 that row plus 44 rows. If It is =45 than enchday is 1 else 0.

 The following error is returned:

 Error in if (sum(dadosmax$above[i:(i + 44)]) = 45) 1 else 0 :
   missing value where TRUE/FALSE needed

 I've tested the ifelse statement assigning different values to i and it
 works. So I'm wondering if this error is due the fact that at the final of
 my data frame there aren't 45 rows to sum anymore. I tried to use try
 but
 It's simply hide the error.

 How can I deal with this? Any ideas?
 Thank you very much.

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




 --
 The temperature of the aqueous content of an unremittingly ogled
 culinary vessel will not achieve 100 degrees on the Celsius scale.

 Maranatha! 
 John McKown


[[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] if else for cumulative sum error

2014-12-02 Thread David Winsemius

On Dec 2, 2014, at 12:26 PM, Jefferson Ferreira-Ferreira wrote:

 Thank you for replies.
 
 David,
 
 I tried your modified form
 
 for (i in 1:seq_along(rownames(dadosmax))){


No. it is either 1:  or seq_along(...). in this case perhaps 
1:(nrow(dadosmax)-44 would be safer

You do not seem to have understood that you cannot use an index of i+44 when i 
is going to be the entire set of rows of the dataframe. There is no there 
there to quote Gertrude Stein's slur against Oakland. In fact there is not 
there there at i+1 when you get to the end. You either need to only go to row

  dadosmax$enchday[i] - if ( (sum(dadosmax$above[i:(i+44)])) = 45) 1 else
 0
 }
 
 However, I'm receiving this warning:
 Warning message:
 In 1:seq_along(rownames(dadosmax)) :
  numerical expression has 2720 elements: only the first used
 
 I can't figure out why only the first row was calculated...

You should of course read these, but the error is not from your if-statement 
but rahter you for-loop-indexing.

?'if'
?ifelse


 Any ideas?
 
 
 
 Em Tue Dec 02 2014 at 15:22:25, John McKown john.archie.mck...@gmail.com
 escreveu:
 
 On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira 
 jeco...@gmail.com wrote:
 
 Hello everybody;
 
 I'm writing a code where part of it is as follows:
 
 for (i in nrow(dadosmax)){
  dadosmax$enchday[i] - if (sum(dadosmax$above[i:(i+44)]) = 45) 1 else 0
 }
 
 
 ​Without some test data for any validation, I would try the following
 formula
 
 dadosmax$enchday[i] - if
 (sum(dadosmax$above[i:(min(i+44,nrow(dadosmax)))] = 45) 1 else 0​
 
 
 
 
 That is for each row of my data frame, sum an specific column (0 or 1) of
 that row plus 44 rows. If It is =45 than enchday is 1 else 0.
 
 The following error is returned:
 
 Error in if (sum(dadosmax$above[i:(i + 44)]) = 45) 1 else 0 :
  missing value where TRUE/FALSE needed
 
 I've tested the ifelse statement assigning different values to i and it
 works. So I'm wondering if this error is due the fact that at the final of
 my data frame there aren't 45 rows to sum anymore. I tried to use try
 but
 It's simply hide the error.
 
 How can I deal with this? Any ideas?
 Thank you very much.
 
[[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.
 
 
 
 
 --
 The temperature of the aqueous content of an unremittingly ogled
 culinary vessel will not achieve 100 degrees on the Celsius scale.
 
 Maranatha! 
 John McKown
 
 
   [[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.

David Winsemius
Alameda, CA, USA

__
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] if else for cumulative sum error

2014-12-02 Thread David L Carlson
Let's try a different approach. You don't need a loop for this. First we need a 
reproducible example:

 set.seed(42)
 dadosmax - data.frame(above=runif(150) + .5)

Now compute your sums using cumsum() and diff() and then compute enchday using 
ifelse(). See the manual pages for each of these functions to understand how 
they work:

 sums - diff(c(0, cumsum(dadosmax$above)), 45)
 dadosmax$enchday - c(ifelse(sums = 45, 1, 0), rep(NA, 44))

 dadosmax$enchday
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
 [26]  1  1  1  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 [51]  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
 [76]  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
[101]  1  1  1  1  1  1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[126] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

See the NA's? Those are what David Winsemius is talking about. For the 106th 
value, 106+44 is 150, but for the 107th value 107+144 is 151 which does not 
exist. Fortunately diff() understands that and stops at 106, but we have to add 
44 NA's because that is the number of rows in your data frame.

You might find this plot informative as well:

 plot(sums, typ=l)
 abline(h=45)

Another way to get there is to use sapply() which will add the NA's for us:

 sums - sapply(1:150, function(x) sum(dadosmax$above[x:(x+44)]))
 dadosmax$enchday - ifelse(sums = 45, 1, 0)

But it won't be as fast if you have a large data set.

-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius
Sent: Tuesday, December 2, 2014 2:50 PM
To: Jefferson Ferreira-Ferreira
Cc: r-help@r-project.org
Subject: Re: [R] if else for cumulative sum error


On Dec 2, 2014, at 12:26 PM, Jefferson Ferreira-Ferreira wrote:

 Thank you for replies.
 
 David,
 
 I tried your modified form
 
 for (i in 1:seq_along(rownames(dadosmax))){


No. it is either 1:  or seq_along(...). in this case perhaps 
1:(nrow(dadosmax)-44 would be safer

You do not seem to have understood that you cannot use an index of i+44 when i 
is going to be the entire set of rows of the dataframe. There is no there 
there to quote Gertrude Stein's slur against Oakland. In fact there is not 
there there at i+1 when you get to the end. You either need to only go to row

  dadosmax$enchday[i] - if ( (sum(dadosmax$above[i:(i+44)])) = 45) 1 else
 0
 }
 
 However, I'm receiving this warning:
 Warning message:
 In 1:seq_along(rownames(dadosmax)) :
  numerical expression has 2720 elements: only the first used
 
 I can't figure out why only the first row was calculated...

You should of course read these, but the error is not from your if-statement 
but rahter you for-loop-indexing.

?'if'
?ifelse


 Any ideas?
 
 
 
 Em Tue Dec 02 2014 at 15:22:25, John McKown john.archie.mck...@gmail.com
 escreveu:
 
 On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira 
 jeco...@gmail.com wrote:
 
 Hello everybody;
 
 I'm writing a code where part of it is as follows:
 
 for (i in nrow(dadosmax)){
  dadosmax$enchday[i] - if (sum(dadosmax$above[i:(i+44)]) = 45) 1 else 0
 }
 
 
 ​Without some test data for any validation, I would try the following
 formula
 
 dadosmax$enchday[i] - if
 (sum(dadosmax$above[i:(min(i+44,nrow(dadosmax)))] = 45) 1 else 0​
 
 
 
 
 That is for each row of my data frame, sum an specific column (0 or 1) of
 that row plus 44 rows. If It is =45 than enchday is 1 else 0.
 
 The following error is returned:
 
 Error in if (sum(dadosmax$above[i:(i + 44)]) = 45) 1 else 0 :
  missing value where TRUE/FALSE needed
 
 I've tested the ifelse statement assigning different values to i and it
 works. So I'm wondering if this error is due the fact that at the final of
 my data frame there aren't 45 rows to sum anymore. I tried to use try
 but
 It's simply hide the error.
 
 How can I deal with this? Any ideas?
 Thank you very much.
 
[[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.
 
 
 
 
 --
 The temperature of the aqueous content of an unremittingly ogled
 culinary vessel will not achieve 100 degrees on the Celsius scale.
 
 Maranatha! 
 John McKown
 
 
   [[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

Re: [R] if else statement in loop

2014-09-29 Thread PIKAL Petr
Hi

Please, be more clear in what do you want. I get many errors trying your code 
and your explanation does not help much.

 for(i in length(1:(2*nrow(X{
+ Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i])  
X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
Error: unexpected ',' in:
for(i in length(1:(2*nrow(X{
Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i])  X$IID1new 
!= '') ,
 }
Error: unexpected '}' in }
 for(i in length(1:(2*nrow(X{
+ Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
+ X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
Error: unexpected ',' in:
Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
X$IID1new != '') ,
 }


Beside, this column X$IID1new != '' does not exist in X

Here you clearly ask for nonexistent column, and why the heck you want to 
select column by number of rows?

 as.character(as.matrix(X[,(2*nrow(X)+1)]))
Error in `[.data.frame`(X, , (2 * nrow(X) + 1)) :
  undefined columns selected

So based on your toy data frames, what shall be the result after your 
computation.

Regards
Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Kate Ignatius
 Sent: Sunday, September 28, 2014 9:14 PM
 To: r-help
 Subject: [R] if else statement in loop

 I have two data frames

 For simplicity:

 X=

 V1 V2 V3  V4 V5 V6
 samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling
 samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling
 samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling

 Y=

 FID IID
 FAM01 samas4
 FAM01 samas5
 FAM01 samas6

 I want to set to create a new IID in Y using V4 V5 V6 in X using an
 ifelse statement in a loop.  I've used something like the following
 (after figuring out my factor problem):

 for(i in length(1:(2*nrow(X{
 Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
 X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
 }

 But of course this tends to overwrite.

 Is there an easy way to set up a loop to replace missing values? This
 didn't work either but not sure if its as easy as this:

 Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
 X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')

 for(i in length(2:(2*nrow(X{
 ifelse((as.character(Y[,i]) == as.character(Xl[,i])),
 X[is.na(X$IID1new)] - as.character(as.matrix(X[(2*nrow(X)+i)])),'')
 }

 Thanks!

 K.

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


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

Re: [R] if else statement in loop

2014-09-29 Thread Kate Ignatius
Ooops,

I edited the code wrong to make it more easier for interpretation and
got X and Y's mixed up.  Try this:

for(i in length(1:(nrow(X{
Y$IID1new - ifelse((as.character(Y[,2]) == as.character(X[,i]) 
Y$IID1new != ''), as.character(as.matrix(X[,(nrow(X)+i)])),'')
}

The second should be like this:

Y$IID1new - ifelse((as.character(Y[,2]) == as.character(X[,1])),
as.character(as.matrix(X[,(nrow(X)+1)])),'')

for(i in length(2:(nrow(X{
ifelse((as.character(Y[,i]) == as.character(X[,i])),
Y$IID1new[is.na(Y$IID1new)] -
as.character(as.matrix(X[,(nrow(X)+i)])),'')
}

The reason why I'm selecting for number of rows seems a little odd
here I know but in real life this actually relies on a third data
frame, say Z, which for simplicity I didn't include here. But I only
want to start looking at the Nth column after twice as many rows in Z.
For instance, if Z has 4 rows, I want to  take values for IID1new
starting from column 9 in X to make IID1new in Y. Does that make
sense? Will this cause a problem?

So maybe it will probably be more like this if there were a Z

for(i in length(1:(2*nrow(Z{
Y$IID1new - ifelse((as.character(Y[,2]) == as.character(X[,i]) 
Y$IID1new != ''), as.character(as.matrix(X[,(2*nrow(Z)+i)])),'')
}

But essentially what I would like is this:

FID IID IID1new
FAM01 samas4 samas4_father
FAM01 samas5 samas5_mother
FAM01 samas6 samas6_sibling

I hope this is a little clearer...

Let me know if there are more errors.

K.

On Mon, Sep 29, 2014 at 2:39 AM, PIKAL Petr petr.pi...@precheza.cz wrote:
 Hi

 Please, be more clear in what do you want. I get many errors trying your code 
 and your explanation does not help much.

 for(i in length(1:(2*nrow(X{
 + Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i])  
 X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
 Error: unexpected ',' in:
 for(i in length(1:(2*nrow(X{
 Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i])  
 X$IID1new != '') ,
 }
 Error: unexpected '}' in }
 for(i in length(1:(2*nrow(X{
 + Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
 + X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
 Error: unexpected ',' in:
 Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
 X$IID1new != '') ,
 }


 Beside, this column X$IID1new != '' does not exist in X

 Here you clearly ask for nonexistent column, and why the heck you want to 
 select column by number of rows?

 as.character(as.matrix(X[,(2*nrow(X)+1)]))
 Error in `[.data.frame`(X, , (2 * nrow(X) + 1)) :
   undefined columns selected

 So based on your toy data frames, what shall be the result after your 
 computation.

 Regards
 Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Kate Ignatius
 Sent: Sunday, September 28, 2014 9:14 PM
 To: r-help
 Subject: [R] if else statement in loop

 I have two data frames

 For simplicity:

 X=

 V1 V2 V3  V4 V5 V6
 samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling
 samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling
 samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling

 Y=

 FID IID
 FAM01 samas4
 FAM01 samas5
 FAM01 samas6

 I want to set to create a new IID in Y using V4 V5 V6 in X using an
 ifelse statement in a loop.  I've used something like the following
 (after figuring out my factor problem):

 for(i in length(1:(2*nrow(X{
 Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
 X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
 }

 But of course this tends to overwrite.

 Is there an easy way to set up a loop to replace missing values? This
 didn't work either but not sure if its as easy as this:

 Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
 X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')

 for(i in length(2:(2*nrow(X{
 ifelse((as.character(Y[,i]) == as.character(Xl[,i])),
 X[is.na(X$IID1new)] - as.character(as.matrix(X[(2*nrow(X)+i)])),'')
 }

 Thanks!

 K.

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

 
 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í

Re: [R] if else statement in loop

2014-09-29 Thread PIKAL Petr
Hi

Still cloudy, still errors.

 for(i in length(1:(nrow(X{
+ Y$IID1new - ifelse((as.character(Y[,2]) == as.character(X[,i]) 
+ Y$IID1new != ''), as.character(as.matrix(X[,(nrow(X)+i)])),'')
+ }
Error in `$-.data.frame`(`*tmp*`, IID1new, value = logical(0)) :
  replacement has 0 rows, data has 3

You need first to add column IID1new to your Y data frame to use this piece of 
code. After that no errors from the code

However I wonder why the result shall be like this? What is the logic behind 
this.


 FID IID IID1new
 FAM01 samas4 samas4_father
 FAM01 samas5 samas5_mother
 FAM01 samas6 samas6_sibling

The only pattern I can see is that in first row (Y) you want item from column 
V4 (X), in second from V5 and in third you wan item from column V6. Is it 
always true? Or the pattern can change in following rows.

Based on your explanation you want to fill a column in Y from some column of X 
based on rownumber of Z?

For your special case it is like this.

Y$IID1new-diag(as.matrix(X[,4:6]))

Anyway, it seems to me that you want some kind of

?merge

Regards
Petr

Here are data I use for your code.

 dput(X)
structure(list(V1 = structure(c(1L, 1L, 1L), .Label = samas4, class = 
factor),
V2 = structure(c(1L, 1L, 1L), .Label = samas5, class = factor),
V3 = structure(c(1L, 1L, 1L), .Label = samas6, class = factor),
V4 = structure(c(1L, 1L, 1L), .Label = samas4_father, class = factor),
V5 = structure(c(1L, 1L, 1L), .Label = samas5_mother, class = factor),
V6 = structure(c(1L, 1L, 1L), .Label = samas6_sibling, class = 
factor)), .Names = c(V1,
V2, V3, V4, V5, V6), class = data.frame, row.names = c(NA,
-3L))
 dput(Y)
structure(list(FID = structure(c(1L, 1L, 1L), .Label = FAM01, class = 
factor),
IID = structure(1:3, .Label = c(samas4, samas5, samas6
), class = factor)), .Names = c(FID, IID), class = data.frame, 
row.names = c(NA,
-3L))



 -Original Message-
 From: Kate Ignatius [mailto:kate.ignat...@gmail.com]
 Sent: Monday, September 29, 2014 12:54 PM
 To: PIKAL Petr
 Cc: r-help
 Subject: Re: [R] if else statement in loop

 Ooops,

 I edited the code wrong to make it more easier for interpretation and
 got X and Y's mixed up.  Try this:

 for(i in length(1:(nrow(X{
 Y$IID1new - ifelse((as.character(Y[,2]) == as.character(X[,i]) 
 Y$IID1new != ''), as.character(as.matrix(X[,(nrow(X)+i)])),'')
 }

 The second should be like this:

 Y$IID1new - ifelse((as.character(Y[,2]) == as.character(X[,1])),
 as.character(as.matrix(X[,(nrow(X)+1)])),'')

 for(i in length(2:(nrow(X{
 ifelse((as.character(Y[,i]) == as.character(X[,i])),
 Y$IID1new[is.na(Y$IID1new)] -
 as.character(as.matrix(X[,(nrow(X)+i)])),'')
 }

 The reason why I'm selecting for number of rows seems a little odd
 here I know but in real life this actually relies on a third data
 frame, say Z, which for simplicity I didn't include here. But I only
 want to start looking at the Nth column after twice as many rows in Z.
 For instance, if Z has 4 rows, I want to  take values for IID1new
 starting from column 9 in X to make IID1new in Y. Does that make
 sense? Will this cause a problem?

 So maybe it will probably be more like this if there were a Z

 for(i in length(1:(2*nrow(Z{
 Y$IID1new - ifelse((as.character(Y[,2]) == as.character(X[,i]) 
 Y$IID1new != ''), as.character(as.matrix(X[,(2*nrow(Z)+i)])),'')
 }

 But essentially what I would like is this:

 FID IID IID1new
 FAM01 samas4 samas4_father
 FAM01 samas5 samas5_mother
 FAM01 samas6 samas6_sibling

 I hope this is a little clearer...

 Let me know if there are more errors.

 K.

 On Mon, Sep 29, 2014 at 2:39 AM, PIKAL Petr petr.pi...@precheza.cz
 wrote:
  Hi
 
  Please, be more clear in what do you want. I get many errors trying
 your code and your explanation does not help much.
 
  for(i in length(1:(2*nrow(X{
  + Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i])
  X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
  Error: unexpected ',' in:
  for(i in length(1:(2*nrow(X{
  Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
 X$IID1new != '') ,
  }
  Error: unexpected '}' in }
  for(i in length(1:(2*nrow(X{
  + Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i])
 
  + X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
  Error: unexpected ',' in:
  Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i])
 
  X$IID1new != '') ,
  }
 
 
  Beside, this column X$IID1new != '' does not exist in X
 
  Here you clearly ask for nonexistent column, and why the heck you
 want to select column by number of rows?
 
  as.character(as.matrix(X[,(2*nrow(X)+1)]))
  Error in `[.data.frame`(X, , (2 * nrow(X) + 1)) :
undefined columns selected
 
  So based on your toy data frames, what shall be the result after your
 computation.
 
  Regards
  Petr
 
 
  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org

[R] if else statement in loop

2014-09-28 Thread Kate Ignatius
I have two data frames

For simplicity:

X=

V1 V2 V3  V4 V5 V6
samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling
samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling
samas4 samas5 samas6 samas4_father samas5_mother samas6_sibling

Y=

FID IID
FAM01 samas4
FAM01 samas5
FAM01 samas6

I want to set to create a new IID in Y using V4 V5 V6 in X using an
ifelse statement in a loop.  I've used something like the following
(after figuring out my factor problem):

for(i in length(1:(2*nrow(X{
Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')
}

But of course this tends to overwrite.

Is there an easy way to set up a loop to replace missing values? This
didn't work either but not sure if its as easy as this:

Y$IID1new - ifelse((as.character(Y[,2]) == as.characterXl[,i]) 
X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')

for(i in length(2:(2*nrow(X{
ifelse((as.character(Y[,i]) == as.character(Xl[,i])),
X[is.na(X$IID1new)] - as.character(as.matrix(X[(2*nrow(X)+i)])),'')
}

Thanks!

K.

__
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] if else in R

2013-11-19 Thread Gary Dong
Dear R users,

I am a R beginner and I am having trouble in using If Else in R. Here is
an example:

## create a data frame

a-c(1,2,3,4)
b-c(2,0,5,0)
ab-data.frame(cbind(a,b))

##calculate c, which is the ratio between a and b

if(ab$b0) {
 ab$c-ab$a/ab$b
} else {
 ab$c-0
 }

here is the error I got:

Warning message:
In if (ab$b  0) { :
  the condition has length  1 and only the first element will be used.

Any help is appreciated!

Gary

[[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] if else in R

2013-11-19 Thread Nick Matzke
Hi,

This would be an issue with if() as well as if/else.  ab$b has 4
numbers in it, so ab$b  0 evaluates to TRUE TRUE FALSE TRUE or
whatever. if() can only take a single true or false. Cheers! Nick

On Tue, Nov 19, 2013 at 8:30 PM, Gary Dong pdxgary...@gmail.com wrote:
 Dear R users,

 I am a R beginner and I am having trouble in using If Else in R. Here is
 an example:

 ## create a data frame

 a-c(1,2,3,4)
 b-c(2,0,5,0)
 ab-data.frame(cbind(a,b))

 ##calculate c, which is the ratio between a and b

 if(ab$b0) {
  ab$c-ab$a/ab$b
 } else {
  ab$c-0
  }

 here is the error I got:

 Warning message:
 In if (ab$b  0) { :
   the condition has length  1 and only the first element will be used.

 Any help is appreciated!

 Gary

 [[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] if else in R

2013-11-19 Thread Ben Tupper
Hi,

On Nov 19, 2013, at 9:41 PM, Nick Matzke mat...@berkeley.edu wrote:

 Hi,
 
 This would be an issue with if() as well as if/else.  ab$b has 4
 numbers in it, so ab$b  0 evaluates to TRUE TRUE FALSE TRUE or
 whatever. if() can only take a single true or false. Cheers! Nick
 

As a follow up, you could make a logical index for the condition as Nick 
suggests.

a-c(1,2,3,4)
b-c(2,0,5,0)
ab-data.frame(a,b)
ix - ab[,'b']  0
ix
# [1]  TRUE FALSE  TRUE FALSE

ab[ix,'c'] - ab[ix,'a']/ab[ix,'b']
ab[!ix,'c'] - 0
ab
#  a b   c
# 1 1 2 0.5
# 2 2 0 0.0
# 3 3 5 0.6
# 4 4 0 0.0

I'm not a big fan of the ab$b form of subsetting, I find ab[,'b'] more readable 
for my fading eyesight.  But you could do it your way, too.

ab$c[ix] - ab$a[ix]/ab$b[ix]

Cheers,
Ben


 On Tue, Nov 19, 2013 at 8:30 PM, Gary Dong pdxgary...@gmail.com wrote:
 Dear R users,
 
 I am a R beginner and I am having trouble in using If Else in R. Here is
 an example:
 
 ## create a data frame
 
 a-c(1,2,3,4)
 b-c(2,0,5,0)
 ab-data.frame(cbind(a,b))
 
 ##calculate c, which is the ratio between a and b
 
 if(ab$b0) {
 ab$c-ab$a/ab$b
 } else {
 ab$c-0
 }
 
 here is the error I got:
 
 Warning message:
 In if (ab$b  0) { :
  the condition has length  1 and only the first element will be used.
 
 Any help is appreciated!
 
 Gary
 
[[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.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.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.


Re: [R] if else in R

2013-11-19 Thread arun
Hi,
Try:
within(ab, {c - ifelse(b0, a/b,0)})
A.K.




On Tuesday, November 19, 2013 8:51 PM, Gary Dong pdxgary...@gmail.com wrote:
Dear R users,

I am a R beginner and I am having trouble in using If Else in R. Here is
an example:

## create a data frame

a-c(1,2,3,4)
b-c(2,0,5,0)
ab-data.frame(cbind(a,b))

##calculate c, which is the ratio between a and b

if(ab$b0) {
ab$c-ab$a/ab$b
} else {
ab$c-0
}

here is the error I got:

Warning message:
In if (ab$b  0) { :
  the condition has length  1 and only the first element will be used.

Any help is appreciated!

Gary

    [[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] if else in R

2013-11-19 Thread Jeff Newmiller
Which means you should use the ifelse function...

ab$c - ifelse( ab$b0, ab$a/ab$b, 0 )
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  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.

Nick Matzke mat...@berkeley.edu wrote:
Hi,

This would be an issue with if() as well as if/else.  ab$b has 4
numbers in it, so ab$b  0 evaluates to TRUE TRUE FALSE TRUE or
whatever. if() can only take a single true or false. Cheers! Nick

On Tue, Nov 19, 2013 at 8:30 PM, Gary Dong pdxgary...@gmail.com
wrote:
 Dear R users,

 I am a R beginner and I am having trouble in using If Else in R.
Here is
 an example:

 ## create a data frame

 a-c(1,2,3,4)
 b-c(2,0,5,0)
 ab-data.frame(cbind(a,b))

 ##calculate c, which is the ratio between a and b

 if(ab$b0) {
  ab$c-ab$a/ab$b
 } else {
  ab$c-0
  }

 here is the error I got:

 Warning message:
 In if (ab$b  0) { :
   the condition has length  1 and only the first element will be
used.

 Any help is appreciated!

 Gary

 [[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] if else in R

2013-11-19 Thread David Winsemius

On Nov 19, 2013, at 5:30 PM, Gary Dong wrote:

 Dear R users,
 
 I am a R beginner and I am having trouble in using If Else in R. Here is
 an example:
 
 ## create a data frame
 
 a-c(1,2,3,4)
 b-c(2,0,5,0)
 ab-data.frame(cbind(a,b))
 
 ##calculate c, which is the ratio between a and b
 
 if(ab$b0) {
 ab$c-ab$a/ab$b
 } else {
 ab$c-0
 }
 
 here is the error I got:

Consider this alternative:

 ab$c - (ab  0 ) * ab$a/ab$b

Although in general, you will probably use `ifelse`.

?ifelse   # different than ?if

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


[R] If else loop problem: the condition has length 1 and only the first element will be used

2013-08-26 Thread Fethe, Michael
Hi all,

I'm running an if else loop to normalize my data to a known control.
I have calculated values that need to be subtracted from each treatment 
applied. I'm trying this within a for loop with if and else commands to apply 
to correct subtraction. This is done as follows:


attach(data.2013)

#Normalize to known control

for(i in signal2) {

if(t.tr==Pst 24) {signal3=(signal2 - 17.29)}

if(t.tr==Pst 48) {signal3=(signal2 - 43.93256)}

if(t.tr==Pst 72) {signal3=(signal2 - 43.477468)}

if(t.tr==Pto 24) {signal3=(signal2 - 29.39875)}

if(t.tr==Pto 48) {signal3=(signal2 - 76.796645)}

if(t.tr==Pto 72) {signal3=(signal2 - 68.176174)}

if(t.tr==Pm 24) {signal3=(signal2 + 0.498333)}

if(t.tr==Pm 48) {signal3=(signal2 - 0.200417)}

if(t.tr==Pm 72) {signal3=(signal2 - 10.465507)}

else {signal3=signal2}

}


This code ran once with no problems or warnings. Now it produces a ton of 
warnings of

the condition has length  1 and only the first element will be used.


Here is str(data.2013)


'data.frame': 4293 obs. of  8 variables:

 $ rep  : Factor w/ 4 levels R1,R2,R3,..: 1 1 1 1 1 1 1 1 1 1 ...

 $ construct: Factor w/ 10 levels 35S,46S,E1,..: 1 1 1 1 1 1 1 1 1 1 ...

 $ time : int  24 24 24 24 24 24 24 24 24 24 ...

 $ treatment: Factor w/ 8 levels Mock,MockUN,..: 1 1 1 1 1 1 2 2 2 3 ...

 $ sample   : int  1 2 3 4 5 6 1 2 3 1 ...

 $ signal   : int  733750 665790 659550 676270 682770 600420 676350 652110 
665350 798850 ...

 $ signal2  : num  734 666 660 676 683 ...

 $ t.tr : Factor w/ 24 levels Mock 24,Mock 48,..: 1 1 1 1 1 1 4 4 4 7 
...



I'm really not sure why this ran once and now is giving me errors.

Can anyone help? I've heard ifelse can be effective when running into this 
error. However, this command does not function the way i would like. I need a 
specific value subtracted from each treatment/time value. The only value that 
should matter for subtracting the correct value is t.tr.



[[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] If else loop problem: the condition has length 1 and only the first element will be used

2013-08-26 Thread arun
HI,

It may be better to provide an example dataset using ?dput().
dput(head(dataset),20)

Try:
signal3- ifelse(t.tr==Pst 24, signal2-17.29, ifelse(t.tr==Pst 48, signal2 
- 43.93256, etc.)) 


A.K.

- Original Message -
From: Fethe, Michael mfet...@utk.edu
To: r-help@r-project.org r-help@r-project.org
Cc: 
Sent: Sunday, August 25, 2013 9:31 PM
Subject: [R] If else loop problem: the condition has length  1 and only the 
first element will be used

Hi all,

I'm running an if else loop to normalize my data to a known control.
I have calculated values that need to be subtracted from each treatment 
applied. I'm trying this within a for loop with if and else commands to apply 
to correct subtraction. This is done as follows:


attach(data.2013)

#Normalize to known control

for(i in signal2) {

if(t.tr==Pst 24) {signal3=(signal2 - 17.29)}

if(t.tr==Pst 48) {signal3=(signal2 - 43.93256)}

if(t.tr==Pst 72) {signal3=(signal2 - 43.477468)}

if(t.tr==Pto 24) {signal3=(signal2 - 29.39875)}

if(t.tr==Pto 48) {signal3=(signal2 - 76.796645)}

if(t.tr==Pto 72) {signal3=(signal2 - 68.176174)}

if(t.tr==Pm 24) {signal3=(signal2 + 0.498333)}

if(t.tr==Pm 48) {signal3=(signal2 - 0.200417)}

if(t.tr==Pm 72) {signal3=(signal2 - 10.465507)}

else {signal3=signal2}

}


This code ran once with no problems or warnings. Now it produces a ton of 
warnings of

the condition has length  1 and only the first element will be used.


Here is str(data.2013)


'data.frame': 4293 obs. of  8 variables:

$ rep      : Factor w/ 4 levels R1,R2,R3,..: 1 1 1 1 1 1 1 1 1 1 ...

$ construct: Factor w/ 10 levels 35S,46S,E1,..: 1 1 1 1 1 1 1 1 1 1 ...

$ time     : int  24 24 24 24 24 24 24 24 24 24 ...

$ treatment: Factor w/ 8 levels Mock,MockUN,..: 1 1 1 1 1 1 2 2 2 3 ...

$ sample   : int  1 2 3 4 5 6 1 2 3 1 ...

$ signal   : int  733750 665790 659550 676270 682770 600420 676350 652110 
665350 798850 ...

$ signal2  : num  734 666 660 676 683 ...

$ t.tr     : Factor w/ 24 levels Mock 24,Mock 48,..: 1 1 1 1 1 1 4 4 4 7 ...



I'm really not sure why this ran once and now is giving me errors.

Can anyone help? I've heard ifelse can be effective when running into this 
error. However, this command does not function the way i would like. I need a 
specific value subtracted from each treatment/time value. The only value that 
should matter for subtracting the correct value is t.tr.



    [[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] If else loop problem: the condition has length 1 and only the first element will be used

2013-08-26 Thread Bert Gunter
Suggestion:

Don't do the ifelse stuff below.

See ?switch  instead.

-- Bert

On Sun, Aug 25, 2013 at 11:32 PM, arun smartpink...@yahoo.com wrote:
 HI,

 It may be better to provide an example dataset using ?dput().
 dput(head(dataset),20)

 Try:
 signal3- ifelse(t.tr==Pst 24, signal2-17.29, ifelse(t.tr==Pst 48, 
 signal2 - 43.93256, etc.))


 A.K.

 - Original Message -
 From: Fethe, Michael mfet...@utk.edu
 To: r-help@r-project.org r-help@r-project.org
 Cc:
 Sent: Sunday, August 25, 2013 9:31 PM
 Subject: [R] If else loop problem: the condition has length  1 and only the 
 first element will be used

 Hi all,

 I'm running an if else loop to normalize my data to a known control.
 I have calculated values that need to be subtracted from each treatment 
 applied. I'm trying this within a for loop with if and else commands to apply 
 to correct subtraction. This is done as follows:


 attach(data.2013)

 #Normalize to known control

 for(i in signal2) {

 if(t.tr==Pst 24) {signal3=(signal2 - 17.29)}

 if(t.tr==Pst 48) {signal3=(signal2 - 43.93256)}

 if(t.tr==Pst 72) {signal3=(signal2 - 43.477468)}

 if(t.tr==Pto 24) {signal3=(signal2 - 29.39875)}

 if(t.tr==Pto 48) {signal3=(signal2 - 76.796645)}

 if(t.tr==Pto 72) {signal3=(signal2 - 68.176174)}

 if(t.tr==Pm 24) {signal3=(signal2 + 0.498333)}

 if(t.tr==Pm 48) {signal3=(signal2 - 0.200417)}

 if(t.tr==Pm 72) {signal3=(signal2 - 10.465507)}

 else {signal3=signal2}

 }


 This code ran once with no problems or warnings. Now it produces a ton of 
 warnings of

 the condition has length  1 and only the first element will be used.


 Here is str(data.2013)


 'data.frame': 4293 obs. of  8 variables:

 $ rep  : Factor w/ 4 levels R1,R2,R3,..: 1 1 1 1 1 1 1 1 1 1 ...

 $ construct: Factor w/ 10 levels 35S,46S,E1,..: 1 1 1 1 1 1 1 1 1 1 ...

 $ time : int  24 24 24 24 24 24 24 24 24 24 ...

 $ treatment: Factor w/ 8 levels Mock,MockUN,..: 1 1 1 1 1 1 2 2 2 3 ...

 $ sample   : int  1 2 3 4 5 6 1 2 3 1 ...

 $ signal   : int  733750 665790 659550 676270 682770 600420 676350 652110 
 665350 798850 ...

 $ signal2  : num  734 666 660 676 683 ...

 $ t.tr : Factor w/ 24 levels Mock 24,Mock 48,..: 1 1 1 1 1 1 4 4 4 7 
 ...



 I'm really not sure why this ran once and now is giving me errors.

 Can anyone help? I've heard ifelse can be effective when running into this 
 error. However, this command does not function the way i would like. I need a 
 specific value subtracted from each treatment/time value. The only value that 
 should matter for subtracting the correct value is t.tr.



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


  1   2   >