Re: [R] Pipe operator

2023-01-04 Thread avi.e.gross
Yes, not every use of a word has the same meaning. The UNIX pipe was in many
ways a very different animal where the PIPE was a very real thing and looked
like a sort of temporary file in the file system with special properties.
Basically it was a fixed-size buffer that effectively was written into by a
process that was paused when the buffer was getting full and allowed to
continue when it was drained by a second process reading from it that also
was similarly managed. It assured many things that a temporary file would
not have supplied including uniqueness and privacy. Later they created a
related animal with persistence called a NAMED PIPE.

So the pipelines we are discussing in R do indeed run very synchronously in
whatever order they need to be run so one finishes producing an output into
an anonymous variable that can then be handed to the next function in the
pipeline. 

If you look at a language like Python or perhaps JavaScript, there are ways
to simulate a relatively asynchronous way to run functions on whatever data
is available from other functions, using ideas like generators and iterators
and more. You can create functions that call other functions just to get one
item such as the next prime number, and do some work and call it again so it
yields just one more value and so on. You can build these in chains so that
lots of functions stay resident in memory and only keep producing data just
in time as needed and perhaps even running on multiple processors in even
more parallelism.

R can possibly add such things and it has elements with things not being
evaluated till needed that can have interesting results and of course it is
possible to spawn additional processes, as with many languages, that are
linked together to run at once, but all such speculation is beyond the
bounds of what operators we call PIPES, such as %>% and |> are doing. It
remains very much syntactic sugar that makes life easier for some and annoys
others.

I note some code I see has people hedging their bets a bit about the missing
first argument. They harmlessly keep the first argument and call it a period
as in:
mutate(mydata, ...) %>%
filter( ., ...) %>%
group_by( ., ...) %>%
summarize( ., ...)


In the above, "..." means fill it in and not an alternate meaning, and the
point is the first argument is a period which is replaced by the
passed-along object but that would have been done without it by default. It
remains a reminder that there still is that first argument and I guess it
could be helpful in some ways too and avoids some potential confusion if
others read your code and look up a man page and understand what the second
and subsequent arguments match up to.


-Original Message-
From: R-help  On Behalf Of Richard O'Keefe
Sent: Wednesday, January 4, 2023 1:56 AM
To: Milan Glacier 
Cc: R-help Mailing List 
Subject: Re: [R] Pipe operator

This is both true and misleading.
The shell pipe operation came from functional programming.  In fact the
shell pipe operation is NOT "flip apply", which is what |> is, but it is
functional composition.  That is out =
let out = command
cmd1 | cmd2 = \x.cmd2(cmd1(x)).

Pragmatically, the Unix shell pipe operator does something very important,
which |> (and even functional composition doesn't in F#):
 out *interleaves* the computation of cmd1 and cmd2,
streaming the data.  But in R, x |> f() |> g() is by definition g(f(x)), and
if g needs the value of its argument, the *whole* of f(x) is evaluated
before g resumes.  This is much closer to what the pipe syntax in the MS-DOS
shell did, if I recall correctly.



On Wed, 4 Jan 2023 at 17:46, Milan Glacier  wrote:

> With 50 years of programming experience, just think about how useful 
> pipe operator is in shell scripting. The output of previous call 
> becomes the input of next call... Genious idea from our beloved unix 
> conversion...
>
>
> On 01/03/23 16:48, Sorkin, John wrote:
> >I am trying to understand the reason for existence of the pipe 
> >operator,
> %>%, and when one should use it. It is my understanding that the 
> operator sends the file to the left of the operator to the function 
> immediately to the right of the operator:
> >
> >c(1:10) %>% mean results in a value of 5.5 which is exactly the same 
> >as
> the result one obtains using the mean function directly, viz.
> mean(c(1:10)). What is the reason for having two syntactically 
> different but semantically identical ways to call a function? Is one 
> more efficient than the other? Does one use less memory than the other?
> >
> >P.S. Please forgive what might seem to be a question with an obvious
> answer. I am a programmer dinosaur. I have been programming for more 
> than
> 50 years. When I started programming in the 1960s the only pipe one 
> spoke about was a bong.
> >
> >John
>

Re: [R] Pipe operator

2023-01-03 Thread Ivan Calandra
Maybe I missed it in the whole discussion, but since R 4.2.0 the base R 
pipe operator also has a placeholder '_' to specify where the result of 
the left-hand side should be used in the right-hand side (see 
https://stat.ethz.ch/pipermail/r-announce/2022/000683.html).


So the only difference in usage between >%> and |> is that the 
placeholder '.' of the magrittr pipe can appear several times.
It would also be nice if R/Rstudio had a default keyboard shortcut to 
insert the base R pipe like for the magrittr pipe (Ctrl+Shift+M or 
Cmd+Shift+M). The vertical bar is not always easy to find (especially 
when you switch between Mac, Windows and different languages).


Ivan


On 03/01/2023 19:34, avi.e.gr...@gmail.com wrote:


Tim,

There are differences and this one can be huge.

The other pipe operators let you pass the current object to a later argument
instead of the first by using a period to represent where to put it. The new
one has a harder albeit flexible method by creating an anonymous function.

-Original Message-
From: R-help  On Behalf Of Ebert,Timothy Aaron
Sent: Tuesday, January 3, 2023 12:08 PM
To: Sorkin, John ; 'R-help Mailing List'

Subject: Re: [R] Pipe operator

The pipe shortens code and results in fewer variables because you do not
have to save intermediate steps. Once you get used to the idea it is useful.
Note that there is also the |> pipe that is part of base R. As far as I know
it does the same thing as %>%, or at my level of programing I have not
encountered a difference.

Tim

-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Tuesday, January 3, 2023 11:49 AM
To: 'R-help Mailing List' 
Subject: [R] Pipe operator

[External Email]

I am trying to understand the reason for existence of the pipe operator,
%>%, and when one should use it. It is my understanding that the operator
sends the file to the left of the operator to the function immediately to
the right of the operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the
result one obtains using the mean function directly, viz. mean(c(1:10)).
What is the reason for having two syntactically different but semantically
identical ways to call a function? Is one more efficient than the other?
Does one use less memory than the other?

P.S. Please forgive what might seem to be a question with an obvious answer.
I am a programmer dinosaur. I have been programming for more than 50 years.
When I started programming in the 1960s the only pipe one spoke about was a
bong.

John

__
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-help=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4
e084253a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C6380836
13362415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJB
TiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=fV9Ca3OAleDX%2BwuPJIONYStrA
daQhXTsq61jh2pLtDY%3D=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-proje
ct.org%2Fposting-guide.html=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4e0842
53a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083613362
415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6I
k1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=YUnV9kE1RcbB3BwM5gKwKwc3qNKhIVNF
txOxKmpbGrQ%3D=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.

__
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] Pipe operator

2023-01-03 Thread Richard O'Keefe
This is both true and misleading.
The shell pipe operation came from functional
programming.  In fact the shell pipe operation
is NOT "flip apply", which is what |> is, but
it is functional composition.  That is
out = let out = command
cmd1 | cmd2 = \x.cmd2(cmd1(x)).

Pragmatically, the Unix shell pipe operator does
something very important, which |> (and even
functional composition doesn't in F#):
 out *interleaves* the computation
of cmd1 and cmd2, streaming the data.  But in R,
x |> f() |> g()
is by definition g(f(x)), and if g needs the value
of its argument, the *whole* of f(x) is evaluated
before g resumes.  This is much closer to what the
pipe syntax in the MS-DOS shell did, if I recall
correctly.



On Wed, 4 Jan 2023 at 17:46, Milan Glacier  wrote:

> With 50 years of programming experience, just think about how useful
> pipe operator is in shell scripting. The output of previous call becomes
> the input of next call... Genious idea from our beloved unix
> conversion...
>
>
> On 01/03/23 16:48, Sorkin, John wrote:
> >I am trying to understand the reason for existence of the pipe operator,
> %>%, and when one should use it. It is my understanding that the operator
> sends the file to the left of the operator to the function immediately to
> the right of the operator:
> >
> >c(1:10) %>% mean results in a value of 5.5 which is exactly the same as
> the result one obtains using the mean function directly, viz.
> mean(c(1:10)). What is the reason for having two syntactically different
> but semantically identical ways to call a function? Is one more efficient
> than the other? Does one use less memory than the other?
> >
> >P.S. Please forgive what might seem to be a question with an obvious
> answer. I am a programmer dinosaur. I have been programming for more than
> 50 years. When I started programming in the 1960s the only pipe one spoke
> about was a bong.
> >
> >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.
>

[[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] Pipe operator

2023-01-03 Thread Milan Glacier

With 50 years of programming experience, just think about how useful
pipe operator is in shell scripting. The output of previous call becomes
the input of next call... Genious idea from our beloved unix
conversion...


On 01/03/23 16:48, Sorkin, John wrote:

I am trying to understand the reason for existence of the pipe operator, %>%, 
and when one should use it. It is my understanding that the operator sends the 
file to the left of the operator to the function immediately to the right of the 
operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
result one obtains using the mean function directly, viz. mean(c(1:10)). What is 
the reason for having two syntactically different but semantically identical ways 
to call a function? Is one more efficient than the other? Does one use less memory 
than the other?

P.S. Please forgive what might seem to be a question with an obvious answer. I 
am a programmer dinosaur. I have been programming for more than 50 years. When 
I started programming in the 1960s the only pipe one spoke about was a bong.

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] Pipe operator

2023-01-03 Thread Richard O'Keefe
"Does saving of variables speed up processing" no
"or save memory" no.
The manual is quite explicit:
> ?"|>"
...
Currently, pipe operations are implemented as syntax
transformations.  So an expression written as 'x |> f(y)' is
parsed as 'f(x, y)'.

Strictly speaking, using |> *doesn't* save any variables.
x |> f(y) |> g() |> h(1,z)
simply is h(g(f(x,y)),1,z) in which precisely the same
variables appear.  All that changes is the order in which
you write the function names; the order in which things are
evaluated does not change (the manual is explicit about
that too).

I personally find |> in R extremely confusing because
in x |> f(y) |> g() |> h(1,z)
it LOOKS as if there are calls to f(f), to g(), and to
h(1,z) and in Haskell or F# that would be true, but in
R the expressions f(y), g(), and h(1,z) are NOT
evaluated.  |> is and has to be special syntax with a
very restricted right-hand side.

Eliminating well-chosen variables can of course make
code much less readable.  It's funny how my code seems
prettier using |> but other people's code seems hopelessly
obscure...


On Wed, 4 Jan 2023 at 06:19, Sorkin, John  wrote:

> Tim,
>
> Thank you for your reply. I did not know about the |> operator. Do both
> %>% and |> work in base R?
>
> You suggested that the pipe operator can produce code with fewer
> variables. May I ask you to send a short example in which the pipe operator
> saves variables. Does said saving of variables speed up processing or
> result in less memory usage?
>
> Thank you,
> John
>
> 
> From: Ebert,Timothy Aaron 
> Sent: Tuesday, January 3, 2023 12:07 PM
> To: Sorkin, John; 'R-help Mailing List'
> Subject: RE: Pipe operator
>
> The pipe shortens code and results in fewer variables because you do not
> have to save intermediate steps. Once you get used to the idea it is
> useful. Note that there is also the |> pipe that is part of base R. As far
> as I know it does the same thing as %>%, or at my level of programing I
> have not encountered a difference.
>
> Tim
>
> -Original Message-
> From: R-help  On Behalf Of Sorkin, John
> Sent: Tuesday, January 3, 2023 11:49 AM
> To: 'R-help Mailing List' 
> Subject: [R] Pipe operator
>
> [External Email]
>
> I am trying to understand the reason for existence of the pipe operator,
> %>%, and when one should use it. It is my understanding that the operator
> sends the file to the left of the operator to the function immediately to
> the right of the operator:
>
> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as
> the result one obtains using the mean function directly, viz.
> mean(c(1:10)). What is the reason for having two syntactically different
> but semantically identical ways to call a function? Is one more efficient
> than the other? Does one use less memory than the other?
>
> P.S. Please forgive what might seem to be a question with an obvious
> answer. I am a programmer dinosaur. I have been programming for more than
> 50 years. When I started programming in the 1960s the only pipe one spoke
> about was a bong.
>
> John
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>
> https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=7dDMSg%2FmPQ5xXP6zu6MWLmARdtdlrYWb3mXPZQj0La0%3D=0
> PLEASE do read the posting guide
> https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=l5BZyjup%2Bho%2FijE1zQMxb5JE3F5VfKBZpUKHYW4k4Fg%3D=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.
>

[[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] Pipe operator

2023-01-03 Thread avi.e.gross
Boris,

There are MANY variations possible and yours does not seem that common or
useful albeit perfectly useful.

I am not talking about making it a one-liner, albeit I find the multi-line
version more useful.

The pipeline concept seems sort of atomic in the following sense. R allows
several in-line variants of assignment besides something like:

Assign("string", value)

And, variations on the above that are more useful when making multiple
assignments in a loop or using other environments.

What is more common is:

Name <- Expression

And of course occasionally:

Expression -> Name

So back to pipelines, you have two perfectly valid ways to do a pipeline and
assign the result. I showed a version like:

Name <-
Variable |>
Pipeline.item(...) |>
... |>
Pipeline.item(...)


But you can equally well assign it at the end:

Variable |>
Pipeline.item(...) |>
... |>
Pipeline.item(...) -> Name


I think a more valid use of assign is in mid-pipeline as one way to save an
intermediate result in a variable or perhaps in another environment, such as
may be useful when debugging:

Name <-
Variable |>
Pipeline.item(...) |>
assign("temp1", _) |>
... |>
Pipeline.item(...)

This works because assign(), like print() also returns a copy of the
argument that can be passed along the pipeline and thus captured for a side
effect. When done debugging, removing some lines makes it continue working
seamlessly.

BTW, your example does something I am not sure you intended:

  x |> cos() |> max(pi/4) |> round(3) |> assign("x", value = _)

I prefer showing it like this:

 x |> 
cos() |> 
max(pi/4) |> 
round(3) |> 
assign("x", value = _)

Did you notice you changed "x" by assigning a new value to the one you
started with? That is perfectly legal but may not have been intended.

And, yes, for completeness, there are two more assignment operators I
generally have no use for of <<- and ->> that work in a global sense.

And for even more completeness you can also use the operators above like
this:

> z = `<-`("x", 7)
> z
[1] 7
> x
[1] 7

For even more completeness, the example we are using can use the above
notation with a silly twist. Placing the results in z instead, I find the
new pipe INSISTS _ can only be used with a named argument. Duh, `<-` does
not have named arguments, just positional. So I see any valid name is just
ignored and the following works!

x |> cos() |> max(pi/4) |> round(3) |> `<-`("z", any.identifier = _)

And, frankly, many functions that need the pipe to feed a second or later
position can easily be changed to use the first argument. If you feel the
need to use "assign" make this function before using the pipeline:

assignyx <- function(x, y) assign(y, x)

Then your code can save a variable without an underscore and keyword:

x |> cos() |> max(pi/4) |> round(3) |> assignyx("x")

Or use the new lambda function somewhat designed for this case use which I
find a bit ugly but it is a matter of taste.

But to end this, there is no reason to make things complex in situations
like this. Just use a simple assignment pre or post as meets your needs.





-Original Message-
From: Boris Steipe  
Sent: Tuesday, January 3, 2023 2:01 PM
To: R-help Mailing List 
Cc: avi.e.gr...@gmail.com
Subject: Re: [R] Pipe operator

Working off Avi's example - would:

  x |> cos() |> max(pi/4) |> round(3) |> assign("x", value = _)

...be even more intuitive to read? Or are there hidden problems with that?



Cheers,
Boris


> On 2023-01-03, at 12:40, avi.e.gr...@gmail.com wrote:
> 
> John,
> 
> The topic has indeed been discussed here endlessly but new people 
> still stumble upon it.
> 
> Until recently, the formal R language did not have a built-in pipe 
> functionality. It was widely used through an assortment of packages 
> and there are quite a few variations on the theme including different 
> implementations.
> 
> Most existing code does use the operator %>% but there is now a 
> built-in |> operator that is generally faster but is not as easy to use in
a few cases.
> 
> Please forget the use of the word FILE here. Pipes are a form of 
> syntactic sugar that generally is about the FIRST argument to a 
> function. They are NOT meant to be used just for the trivial case you 
> mention where indeed there is an easy way to do things. Yes, they work 
> in such situations. But consider a deeply nested expression like this:
> 
> Result <- round(max(cos(x), 3.14159/4), 3)
> 
> There are MANY deeper nested expressions like this commonly used. The 
> above can be written linearly as in
> 
> Temp1 <- cos(x)
> Temp2 <- 

Re: [R] Pipe operator

2023-01-03 Thread Richard O'Keefe
The simplest and best answer is "fashion".
In FSharp,
> (|>);;
val it: ('a -> ('a -> 'b) -> 'b)
The ability to turn f x y into y |> f x
makes perfect sense in a programming language
where Currying (representing a function of n
arguments as a function of 1 argument that
returns a function of n-1 arguments, similarly
represented) is a way of life.  It can result
in code that is more readable.  And it is
pretty much unavoidable:
let x |> f = f x
is definable in the language.

In programming languages like Erlang and R,
where Currying is *not* a way of life, the
matter is otherwise.

Really, it's all about whether you talk like Luke
or like Yoda talk, it's not about what you say or
efficiency or anything but perceived readability.


On Wed, 4 Jan 2023 at 05:49, Sorkin, John  wrote:

> I am trying to understand the reason for existence of the pipe operator,
> %>%, and when one should use it. It is my understanding that the operator
> sends the file to the left of the operator to the function immediately to
> the right of the operator:
>
> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as
> the result one obtains using the mean function directly, viz.
> mean(c(1:10)). What is the reason for having two syntactically different
> but semantically identical ways to call a function? Is one more efficient
> than the other? Does one use less memory than the other?
>
> P.S. Please forgive what might seem to be a question with an obvious
> answer. I am a programmer dinosaur. I have been programming for more than
> 50 years. When I started programming in the 1960s the only pipe one spoke
> about was a bong.
>
> 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.
>

[[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] Pipe operator

2023-01-03 Thread Sorkin, John
Jeff,
Thank you for contributing important information to this thread. 


From: Jeff Newmiller 
Sent: Tuesday, January 3, 2023 2:07 PM
To: r-help@r-project.org; Sorkin, John; Ebert,Timothy Aaron; 'R-help Mailing 
List'
Subject: Re: [R] Pipe operator

The other responses here have been very good, but I felt it necessary to point 
out that the concept of a pipe originated around when you started programming 
[1] (text based). It did take awhile for it to migrate into programming 
languages such as OCaml, but Powershell makes extensive use of (object-based) 
pipes.

Re memory use: not so much. Variables are small... it is the data they point to 
that is large, and it is not possible to analyze data without storing it 
somewhere. But when the variables are numerous they can interfere with our 
ability to understand the program... using pipes lets us focus on results 
obtained after several steps so fewer intermediate values clutter the variable 
space.

Re speed: the magrittr pipe (%>%) is much slower than the built-in pipe at 
coordinating the transfer of data from left to right, but that is not usually 
significant compared to the computation speed on the actual data in the 
functions.

 [1] 
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fen.m.wikipedia.org%2Fwiki%2FPipeline_=05%7C01%7Cjsorkin%40som.umaryland.edu%7C94e1ec7b93c642286aae08daedbdc79f%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083696601759531%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=gdooVKcK8iDNN0X6ZaYmDNk9pQ1Pe%2BgQiUGioPGB%2Fps%3D=0(Unix)#:~:text=The%20concept%20of%20pipelines%20was,Ritchie%20%26%20Thompson%2C%201974).

On January 3, 2023 9:13:22 AM PST, "Sorkin, John"  
wrote:
>Tim,
>
>Thank you for your reply. I did not know about the |> operator. Do both %>% 
>and |> work in base R?
>
>You suggested that the pipe operator can produce code with fewer variables. 
>May I ask you to send a short example in which the pipe operator saves 
>variables. Does said saving of variables speed up processing or result in less 
>memory usage?
>
>Thank you,
>John
>
>
>From: Ebert,Timothy Aaron 
>Sent: Tuesday, January 3, 2023 12:07 PM
>To: Sorkin, John; 'R-help Mailing List'
>Subject: RE: Pipe operator
>
>The pipe shortens code and results in fewer variables because you do not have 
>to save intermediate steps. Once you get used to the idea it is useful. Note 
>that there is also the |> pipe that is part of base R. As far as I know it 
>does the same thing as %>%, or at my level of programing I have not 
>encountered a difference.
>
>Tim
>
>-Original Message-
>From: R-help  On Behalf Of Sorkin, John
>Sent: Tuesday, January 3, 2023 11:49 AM
>To: 'R-help Mailing List' 
>Subject: [R] Pipe operator
>
>[External Email]
>
>I am trying to understand the reason for existence of the pipe operator, %>%, 
>and when one should use it. It is my understanding that the operator sends the 
>file to the left of the operator to the function immediately to the right of 
>the operator:
>
>c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
>result one obtains using the mean function directly, viz. mean(c(1:10)). What 
>is the reason for having two syntactically different but semantically 
>identical ways to call a function? Is one more efficient than the other? Does 
>one use less memory than the other?
>
>P.S. Please forgive what might seem to be a question with an obvious answer. I 
>am a programmer dinosaur. I have been programming for more than 50 years. When 
>I started programming in the 1960s the only pipe one spoke about was a bong.
>
>John
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Cjsorkin%40som.umaryland.edu%7C94e1ec7b93c642286aae08daedbdc79f%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083696601759531%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=jQx8iLm1i%2BQky6NTJ05AmhH6Fb6gJScFuafmEEFs2nM%3D=0
>PLEASE do read the posting guide 
>https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7Cjsorkin%40som.umaryland.edu%7C94e1ec7b93c642286aae08daedbdc79f%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083696601759531%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=jHwquXRkVY6hOIB7dKo4jcEiuA%2F5lz%2FiFeAle2CrBbY%3D=0
>and provide commented, minimal, self-contained, reproducible code.
>
>

Re: [R] Pipe operator

2023-01-03 Thread Jeff Newmiller
> R is a functional language, hence the pipe operator is not needed.

Not factual... just opinion. Please be conscious of your biases and preface 
opinion with a disclaimer.

I heard identical complaints from embedded assembly language programmers when C 
became all the rage... "don't need another way to say the same thing."

>Also it makes the code unreadable as it is less obvious how a call stack looks 
>like and what the arguments to the function calls are.

How can it make the code unreadable if there is a 1:1 mapping between nested 
function calls and a pipe?

If _you_ don't like pipes, that is your opinion, but that statement is 
factually incorrect... both the parser equivalence and the popularity of the 
syntax prove you wrong. Many people find it more readable than nested prefix 
notation.

>It is relevant for a shell for piping text streams.

So you are willing to allow that it makes sense in shell script but not in R 
script? This is not a self-consistent position. The same expressive principles 
can apply in both shell and in R.

On January 3, 2023 2:32:17 PM PST, Uwe Ligges  
wrote:
>R is a functional language, hence the pipe operator is not needed.
>Also it makes the code unreadable as it is less obvious how a call stack looks 
>like and what the arguments to the function calls are.
>
>It is relevant for a shell for piping text streams.
>
>If people cannot live without the pipe operator (and I wonder why you want to 
>add a level of complexity, as it is more obfuscated what the actual function 
>calls are), please use R's internal one, as it is known by the parser and 
>hence debugging etc is better integrated.
>
>Best,
>Uwe Ligges
>
>
>
>On 03.01.2023 17:48, Sorkin, John wrote:
>> I am trying to understand the reason for existence of the pipe operator, 
>> %>%, and when one should use it. It is my understanding that the operator 
>> sends the file to the left of the operator to the function immediately to 
>> the right of the operator:
>> 
>> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
>> result one obtains using the mean function directly, viz. mean(c(1:10)). 
>> What is the reason for having two syntactically different but semantically 
>> identical ways to call a function? Is one more efficient than the other? 
>> Does one use less memory than the other?
>> 
>> P.S. Please forgive what might seem to be a question with an obvious answer. 
>> I am a programmer dinosaur. I have been programming for more than 50 years. 
>> When I started programming in the 1960s the only pipe one spoke about was a 
>> bong.
>> 
>> 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.
>
>__
>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] Pipe operator

2023-01-03 Thread Uwe Ligges

R is a functional language, hence the pipe operator is not needed.
Also it makes the code unreadable as it is less obvious how a call stack 
looks like and what the arguments to the function calls are.


It is relevant for a shell for piping text streams.

If people cannot live without the pipe operator (and I wonder why you 
want to add a level of complexity, as it is more obfuscated what the 
actual function calls are), please use R's internal one, as it is known 
by the parser and hence debugging etc is better integrated.


Best,
Uwe Ligges



On 03.01.2023 17:48, Sorkin, John wrote:

I am trying to understand the reason for existence of the pipe operator, %>%, 
and when one should use it. It is my understanding that the operator sends the 
file to the left of the operator to the function immediately to the right of the 
operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
result one obtains using the mean function directly, viz. mean(c(1:10)). What is 
the reason for having two syntactically different but semantically identical ways 
to call a function? Is one more efficient than the other? Does one use less memory 
than the other?

P.S. Please forgive what might seem to be a question with an obvious answer. I 
am a programmer dinosaur. I have been programming for more than 50 years. When 
I started programming in the 1960s the only pipe one spoke about was a bong.

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.


__
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] Pipe operator

2023-01-03 Thread Ivan Calandra

Dear John,

some more experienced users might give you a different and more helpful 
answer, but I was not really convinced by the pipe operator until I 
tried it out, for the same reasons as you.


In my opinion, the pipe operator is there only to improve the 
readability of your code. Think about e.g. format()ing or round()ing the 
example you gave: you start having a lot of imbricated functions and it 
becomes difficult to read (because of lots of brackets, commas and so 
on, and it gets worse when adding arguments). The pipe operator makes it 
clearer.
An alternative to the pipe operator with good readability is creating 
intermediary objects, but you create a lot of useless objects. Depending 
on the size of the objects, it could become problematic.


Somehow, I just ended up paraphrasing Wickham & Grolemund 
(https://r4ds.had.co.nz/pipes.html); they explain the advantages much 
better than I can.


In any case, once I started using it, I realized that all the pros for 
the pipe operator are real and now I like using it!


Best,
Ivan




*LEIBNIZ-ZENTRUM*
*FÜR ARCHÄOLOGIE*

*Dr. Ivan CALANDRA*
**Imaging Lab

MONREPOS Archaeological Research Centre, Schloss Monrepos
56567 Neuwied, Germany

T: +49 2631 9772 243
T: +49 6131 8885 543
ivan.calan...@leiza.de

leiza.de 

ORCID 
ResearchGate


LEIZA is a foundation under public law of the State of 
Rhineland-Palatinate and the City of Mainz. Its headquarters are in 
Mainz. Supervision is carried out by the Ministry of Science and Health 
of the State of Rhineland-Palatinate. LEIZA is a research museum of the 
Leibniz Association.


On 03/01/2023 17:48, Sorkin, John wrote:

I am trying to understand the reason for existence of the pipe operator, %>%, 
and when one should use it. It is my understanding that the operator sends the 
file to the left of the operator to the function immediately to the right of the 
operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
result one obtains using the mean function directly, viz. mean(c(1:10)). What is 
the reason for having two syntactically different but semantically identical ways 
to call a function? Is one more efficient than the other? Does one use less memory 
than the other?

P.S. Please forgive what might seem to be a question with an obvious answer. I 
am a programmer dinosaur. I have been programming for more than 50 years. When 
I started programming in the 1960s the only pipe one spoke about was a bong.

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 guidehttp://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] Pipe operator

2023-01-03 Thread Andrew Hart via R-help
Keep in mind that in thie example you're processing x and placing the 
result back in x (so x must already exist). You can write this a bit 
more cleanly using the -> variant of the assignment operator as follows:


  x |> cos() |> max(pi/4) |> round(3) -> x

Hth,
Andrew.

On 3/01/2023 16:00, Boris Steipe wrote:

Working off Avi's example - would:

   x |> cos() |> max(pi/4) |> round(3) |> assign("x", value = _)

...be even more intuitive to read? Or are there hidden problems with that?



Cheers,
Boris



On 2023-01-03, at 12:40, avi.e.gr...@gmail.com wrote:

John,

The topic has indeed been discussed here endlessly but new people still
stumble upon it.

Until recently, the formal R language did not have a built-in pipe
functionality. It was widely used through an assortment of packages and
there are quite a few variations on the theme including different
implementations.

Most existing code does use the operator %>% but there is now a built-in |>
operator that is generally faster but is not as easy to use in a few cases.

Please forget the use of the word FILE here. Pipes are a form of syntactic
sugar that generally is about the FIRST argument to a function. They are NOT
meant to be used just for the trivial case you mention where indeed there is
an easy way to do things. Yes, they work in such situations. But consider a
deeply nested expression like this:

Result <- round(max(cos(x), 3.14159/4), 3)

There are MANY deeper nested expressions like this commonly used. The above
can be written linearly as in

Temp1 <- cos(x)
Temp2 <- max(Temp1, 3.14159/4)
Result <- round(Temp2, 3)

Translation, for some variable x, calculate the cosine and take the maximum
value of it as compared to pi/4 and round the result to three decimal
places. Not an uncommon kind of thing to do and sometimes you can nest such
things many layers deep and get hopelessly confused if not done somewhat
linearly.

What pipes allow is to write this closer to the second way while not seeing
or keeping any temporary variables around. The goal is to replace the FIRST
argument to a function with whatever resulted as the value of the previous
expression. That is often a vector or data.frame or list or any kind of
object but can also be fairly complex as in a list of lists of matrices.

So you can still start with cos(x) OR you can write this where the x is
removed from within and leaves cos() empty:

x %>% cos
or
x |> cos()

In the previous version of pipes the parentheses after cos() are optional if
there are no additional arguments but the new pipe requires them.

So continuing the above, using multiple lines, the pipe looks like:

Result <-
  x %>%
  cos() %>%
  max(3.14159/4) %>%
  round(3)

This gives the same result but is arguably easier for some to read and
follow. Nobody forces you to use it and for simple cases, most people don't.

There is a grouping of packages called the tidyverse that makes heavy use of
pipes routine as they made most or all their functions such that the first
argument is the one normally piped to and it can be very handy to write code
that says, read in your data into a variable (a data.frame or tibble often)
and PIPE IT to a function that renames some columns and PIPE the resulting
modified object to a function that retains only selected rows and pipe that
to a function that drops some of the columns and pipe that to a function
that groups the items or sorts them and pipe that to a function that does a
join with another object or generates a report or so many other things.

So the real answer is that piping is another WAY of doing things from a
programmers perspective. Underneath it all, it is mostly syntactic sugar and
the interpreter rearranges your code and performs the steps in what seems
like a different order at times. Generally, you do not need to care.



-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Tuesday, January 3, 2023 11:49 AM
To: 'R-help Mailing List' 
Subject: [R] Pipe operator

I am trying to understand the reason for existence of the pipe operator,
%>%, and when one should use it. It is my understanding that the operator
sends the file to the left of the operator to the function immediately to
the right of the operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the
result one obtains using the mean function directly, viz. mean(c(1:10)).
What is the reason for having two syntactically different but semantically
identical ways to call a function? Is one more efficient than the other?
Does one use less memory than the other?

P.S. Please forgive what might seem to be a question with an obvious answer.
I am a programmer dinosaur. I have been programming for more than 50 years.
When I started programming in the 1960s the only pipe one spoke about was a
bong.

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 

Re: [R] Pipe operator

2023-01-03 Thread Rui Barradas

Às 19:14 de 03/01/2023, Rui Barradas escreveu:

Às 17:35 de 03/01/2023, Greg Snow escreveu:

To expand a little on Christopher's answer.

The short answer is that having the different syntaxes can lead to
more readable code (when used properly).

Note that there are now 2 different (but somewhat similar) pipes
available in R (there could be more in some package(s) that I don't
know about, but will just talk about the main 2).

The %>% pipe comes from the magrittr package, but many other packages
now import that package.  But you need to load the magrittr package,
either directly or indirectly, before you can use that pipe.  The
magrittr pipe is a function call, so there is small increase in time
and memory for using it, but it is a small fraction of a second and a
few bytes of memory, so you probably will not notice the increased
usage.

The core R language now has a built in pipe |> which is handled by the
parser, so no extra function calls and you do not need to load any
extra packages (though you need a somewhat recent version of R, within
the last year or so).

The built-in |> pipe is a little pickier, you need to include the
parentheses in a function call, e.g. 1:10 |> mean() where the magrittr
pipe can work with that call or the function without parentheses, e.g.
1:10 %>% mean or 1:10 %>% mean(), this makes %>% a little easier to
work with anonymous functions.  If the previous return needs to be
passed to an argument other than the first, then %>% uses "." and |>
uses "_".

The magrittr package has additional versions of the pipe and some
functions that wrap around common operators to make it easier to use
them with pipes, so there are still advantages to loading that package
if any of those are helpful.

For a simple case like your example, the pipe probably does not help
with readability much, but as we string more function calls together.
For example, here are 3 ways to compute the geometric mean of the data
in a vector "x":

exp(mean(log(x)))

logx <- log(x)
mlx <- mean(logx)
exp(mtx)

x |>
    log() |>
    mean() |>
    exp()

These all do the same thing, but the first option is read from the
middle outward (which can be tricky) and is even more complicated if
you use additional arguments to any of the functions.
The second option reads top down, but requires creating intermediate
variables.  The last reads similar to the second, but without the
extra variables.  Spreading the series of function calls across
multiple rows makes it easier to read and easily lets you insert a
line like `print() |>` for debugging or checking intermediate results,
and single lines can easily be commented out to skip that step.

I have found myself using code like the following to compute a table,
print it, and compute the proportions all in one step:

table(f, g) |>
   print() |>
   prop.table()

The pipes also work very well with the tidyverse, or even the tidy
data ideas without those packages where we use a single function for
each change, e.g. start with a data frame, select a subset of the
columns, filter to a subset of the rows, mutate a column, join to
another data frame, then pass the final result to a modeling function
like `lm` (and then pass that result to a summary function).  This is
nicely readable when each step is its own line.

On Tue, Jan 3, 2023 at 9:49 AM Sorkin, John 
 wrote:


I am trying to understand the reason for existence of the pipe 
operator, %>%, and when one should use it. It is my understanding 
that the operator sends the file to the left of the operator to the 
function immediately to the right of the operator:


c(1:10) %>% mean results in a value of 5.5 which is exactly the same 
as the result one obtains using the mean function directly, viz. 
mean(c(1:10)). What is the reason for having two syntactically 
different but semantically identical ways to call a function? Is one 
more efficient than the other? Does one use less memory than the other?


P.S. Please forgive what might seem to be a question with an obvious 
answer. I am a programmer dinosaur. I have been programming for more 
than 50 years. When I started programming in the 1960s the only pipe 
one spoke about was a bong.


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.






Hello,

Not a long time ago, there was (very) relevant post to r-devel [1] by 
Paul Murrell linking to a YouTube video [2].


[1] https://stat.ethz.ch/pipermail/r-devel/2022-September/081959.html
[2] https://youtu.be/IMpXB30MP48

Hope this helps,

Rui Barradas

__
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 

Re: [R] Pipe operator

2023-01-03 Thread Jeff Newmiller
Ick.

Some people like

x |> cos() |> max(pi/4) |> round(3) -> x

but I much prefer

x <- x |> cos() |> max(pi/4) |> round(3)

On January 3, 2023 11:00:46 AM PST, Boris Steipe  
wrote:
>Working off Avi's example - would:
>
>  x |> cos() |> max(pi/4) |> round(3) |> assign("x", value = _)
>
>...be even more intuitive to read? Or are there hidden problems with that?
>
>
>
>Cheers,
>Boris
>
>
>> On 2023-01-03, at 12:40, avi.e.gr...@gmail.com wrote:
>> 
>> John,
>> 
>> The topic has indeed been discussed here endlessly but new people still
>> stumble upon it.
>> 
>> Until recently, the formal R language did not have a built-in pipe
>> functionality. It was widely used through an assortment of packages and
>> there are quite a few variations on the theme including different
>> implementations.
>> 
>> Most existing code does use the operator %>% but there is now a built-in |>
>> operator that is generally faster but is not as easy to use in a few cases.
>> 
>> Please forget the use of the word FILE here. Pipes are a form of syntactic
>> sugar that generally is about the FIRST argument to a function. They are NOT
>> meant to be used just for the trivial case you mention where indeed there is
>> an easy way to do things. Yes, they work in such situations. But consider a
>> deeply nested expression like this:
>> 
>> Result <- round(max(cos(x), 3.14159/4), 3)
>> 
>> There are MANY deeper nested expressions like this commonly used. The above
>> can be written linearly as in
>> 
>> Temp1 <- cos(x)
>> Temp2 <- max(Temp1, 3.14159/4)
>> Result <- round(Temp2, 3)
>> 
>> Translation, for some variable x, calculate the cosine and take the maximum
>> value of it as compared to pi/4 and round the result to three decimal
>> places. Not an uncommon kind of thing to do and sometimes you can nest such
>> things many layers deep and get hopelessly confused if not done somewhat
>> linearly.
>> 
>> What pipes allow is to write this closer to the second way while not seeing
>> or keeping any temporary variables around. The goal is to replace the FIRST
>> argument to a function with whatever resulted as the value of the previous
>> expression. That is often a vector or data.frame or list or any kind of
>> object but can also be fairly complex as in a list of lists of matrices.
>> 
>> So you can still start with cos(x) OR you can write this where the x is
>> removed from within and leaves cos() empty:
>> 
>> x %>% cos
>> or
>> x |> cos()
>> 
>> In the previous version of pipes the parentheses after cos() are optional if
>> there are no additional arguments but the new pipe requires them.
>> 
>> So continuing the above, using multiple lines, the pipe looks like:
>> 
>> Result <-
>>  x %>%
>>  cos() %>%
>>  max(3.14159/4) %>%
>>  round(3)
>> 
>> This gives the same result but is arguably easier for some to read and
>> follow. Nobody forces you to use it and for simple cases, most people don't.
>> 
>> There is a grouping of packages called the tidyverse that makes heavy use of
>> pipes routine as they made most or all their functions such that the first
>> argument is the one normally piped to and it can be very handy to write code
>> that says, read in your data into a variable (a data.frame or tibble often)
>> and PIPE IT to a function that renames some columns and PIPE the resulting
>> modified object to a function that retains only selected rows and pipe that
>> to a function that drops some of the columns and pipe that to a function
>> that groups the items or sorts them and pipe that to a function that does a
>> join with another object or generates a report or so many other things.
>> 
>> So the real answer is that piping is another WAY of doing things from a
>> programmers perspective. Underneath it all, it is mostly syntactic sugar and
>> the interpreter rearranges your code and performs the steps in what seems
>> like a different order at times. Generally, you do not need to care.
>> 
>> 
>> 
>> -Original Message-
>> From: R-help  On Behalf Of Sorkin, John
>> Sent: Tuesday, January 3, 2023 11:49 AM
>> To: 'R-help Mailing List' 
>> Subject: [R] Pipe operator
>> 
>> I am trying to understand the reason for existence of the pipe operator,
>> %>%, and when one should use it. It is my understanding that the operator
>> sends the file to the left of the operator to the function immediately to
>> the right of the operator:
>> 
>> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the
>> result one obtains using the mean function directly, viz. mean(c(1:10)).
>> What is the reason for having two syntactically different but semantically
>> identical ways to call a function? Is one more efficient than the other?
>> Does one use less memory than the other? 
>> 
>> P.S. Please forgive what might seem to be a question with an obvious answer.
>> I am a programmer dinosaur. I have been programming for more than 50 years.
>> When I started programming in the 1960s the only pipe one spoke about was a
>> bong.  
>> 

Re: [R] Pipe operator

2023-01-03 Thread Rui Barradas

Às 17:35 de 03/01/2023, Greg Snow escreveu:

To expand a little on Christopher's answer.

The short answer is that having the different syntaxes can lead to
more readable code (when used properly).

Note that there are now 2 different (but somewhat similar) pipes
available in R (there could be more in some package(s) that I don't
know about, but will just talk about the main 2).

The %>% pipe comes from the magrittr package, but many other packages
now import that package.  But you need to load the magrittr package,
either directly or indirectly, before you can use that pipe.  The
magrittr pipe is a function call, so there is small increase in time
and memory for using it, but it is a small fraction of a second and a
few bytes of memory, so you probably will not notice the increased
usage.

The core R language now has a built in pipe |> which is handled by the
parser, so no extra function calls and you do not need to load any
extra packages (though you need a somewhat recent version of R, within
the last year or so).

The built-in |> pipe is a little pickier, you need to include the
parentheses in a function call, e.g. 1:10 |> mean() where the magrittr
pipe can work with that call or the function without parentheses, e.g.
1:10 %>% mean or 1:10 %>% mean(), this makes %>% a little easier to
work with anonymous functions.  If the previous return needs to be
passed to an argument other than the first, then %>% uses "." and |>
uses "_".

The magrittr package has additional versions of the pipe and some
functions that wrap around common operators to make it easier to use
them with pipes, so there are still advantages to loading that package
if any of those are helpful.

For a simple case like your example, the pipe probably does not help
with readability much, but as we string more function calls together.
For example, here are 3 ways to compute the geometric mean of the data
in a vector "x":

exp(mean(log(x)))

logx <- log(x)
mlx <- mean(logx)
exp(mtx)

x |>
log() |>
mean() |>
exp()

These all do the same thing, but the first option is read from the
middle outward (which can be tricky) and is even more complicated if
you use additional arguments to any of the functions.
The second option reads top down, but requires creating intermediate
variables.  The last reads similar to the second, but without the
extra variables.  Spreading the series of function calls across
multiple rows makes it easier to read and easily lets you insert a
line like `print() |>` for debugging or checking intermediate results,
and single lines can easily be commented out to skip that step.

I have found myself using code like the following to compute a table,
print it, and compute the proportions all in one step:

table(f, g) |>
   print() |>
   prop.table()

The pipes also work very well with the tidyverse, or even the tidy
data ideas without those packages where we use a single function for
each change, e.g. start with a data frame, select a subset of the
columns, filter to a subset of the rows, mutate a column, join to
another data frame, then pass the final result to a modeling function
like `lm` (and then pass that result to a summary function).  This is
nicely readable when each step is its own line.

On Tue, Jan 3, 2023 at 9:49 AM Sorkin, John  wrote:


I am trying to understand the reason for existence of the pipe operator, %>%, 
and when one should use it. It is my understanding that the operator sends the 
file to the left of the operator to the function immediately to the right of the 
operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
result one obtains using the mean function directly, viz. mean(c(1:10)). What is 
the reason for having two syntactically different but semantically identical ways 
to call a function? Is one more efficient than the other? Does one use less memory 
than the other?

P.S. Please forgive what might seem to be a question with an obvious answer. I 
am a programmer dinosaur. I have been programming for more than 50 years. When 
I started programming in the 1960s the only pipe one spoke about was a bong.

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.






Hello,

Not a long time ago, there was (very) relevant post to r-devel [1] by 
Paul Murrell linking to a YouTube video [2].


[1] https://stat.ethz.ch/pipermail/r-devel/2022-September/081959.html
[2] https://youtu.be/IMpXB30MP48

Hope this helps,

Rui Barradas

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

Re: [R] Pipe operator

2023-01-03 Thread Jeff Newmiller
The other responses here have been very good, but I felt it necessary to point 
out that the concept of a pipe originated around when you started programming 
[1] (text based). It did take awhile for it to migrate into programming 
languages such as OCaml, but Powershell makes extensive use of (object-based) 
pipes.

Re memory use: not so much. Variables are small... it is the data they point to 
that is large, and it is not possible to analyze data without storing it 
somewhere. But when the variables are numerous they can interfere with our 
ability to understand the program... using pipes lets us focus on results 
obtained after several steps so fewer intermediate values clutter the variable 
space.

Re speed: the magrittr pipe (%>%) is much slower than the built-in pipe at 
coordinating the transfer of data from left to right, but that is not usually 
significant compared to the computation speed on the actual data in the 
functions.

 [1] 
https://en.m.wikipedia.org/wiki/Pipeline_(Unix)#:~:text=The%20concept%20of%20pipelines%20was,Ritchie%20%26%20Thompson%2C%201974).

On January 3, 2023 9:13:22 AM PST, "Sorkin, John"  
wrote:
>Tim,
>
>Thank you for your reply. I did not know about the |> operator. Do both %>% 
>and |> work in base R?
>
>You suggested that the pipe operator can produce code with fewer variables. 
>May I ask you to send a short example in which the pipe operator saves 
>variables. Does said saving of variables speed up processing or result in less 
>memory usage?
>
>Thank you,
>John
>
>
>From: Ebert,Timothy Aaron 
>Sent: Tuesday, January 3, 2023 12:07 PM
>To: Sorkin, John; 'R-help Mailing List'
>Subject: RE: Pipe operator
>
>The pipe shortens code and results in fewer variables because you do not have 
>to save intermediate steps. Once you get used to the idea it is useful. Note 
>that there is also the |> pipe that is part of base R. As far as I know it 
>does the same thing as %>%, or at my level of programing I have not 
>encountered a difference.
>
>Tim
>
>-Original Message-
>From: R-help  On Behalf Of Sorkin, John
>Sent: Tuesday, January 3, 2023 11:49 AM
>To: 'R-help Mailing List' 
>Subject: [R] Pipe operator
>
>[External Email]
>
>I am trying to understand the reason for existence of the pipe operator, %>%, 
>and when one should use it. It is my understanding that the operator sends the 
>file to the left of the operator to the function immediately to the right of 
>the operator:
>
>c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
>result one obtains using the mean function directly, viz. mean(c(1:10)). What 
>is the reason for having two syntactically different but semantically 
>identical ways to call a function? Is one more efficient than the other? Does 
>one use less memory than the other?
>
>P.S. Please forgive what might seem to be a question with an obvious answer. I 
>am a programmer dinosaur. I have been programming for more than 50 years. When 
>I started programming in the 1960s the only pipe one spoke about was a bong.
>
>John
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=7dDMSg%2FmPQ5xXP6zu6MWLmARdtdlrYWb3mXPZQj0La0%3D=0
>PLEASE do read the posting guide 
>https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=l5BZyjup%2Bho%2FijE1zQMxb5JE3F5VfKBZpUKHYW4k4Fg%3D=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.

-- 
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] Pipe operator

2023-01-03 Thread Boris Steipe
Working off Avi's example - would:

  x |> cos() |> max(pi/4) |> round(3) |> assign("x", value = _)

...be even more intuitive to read? Or are there hidden problems with that?



Cheers,
Boris


> On 2023-01-03, at 12:40, avi.e.gr...@gmail.com wrote:
> 
> John,
> 
> The topic has indeed been discussed here endlessly but new people still
> stumble upon it.
> 
> Until recently, the formal R language did not have a built-in pipe
> functionality. It was widely used through an assortment of packages and
> there are quite a few variations on the theme including different
> implementations.
> 
> Most existing code does use the operator %>% but there is now a built-in |>
> operator that is generally faster but is not as easy to use in a few cases.
> 
> Please forget the use of the word FILE here. Pipes are a form of syntactic
> sugar that generally is about the FIRST argument to a function. They are NOT
> meant to be used just for the trivial case you mention where indeed there is
> an easy way to do things. Yes, they work in such situations. But consider a
> deeply nested expression like this:
> 
> Result <- round(max(cos(x), 3.14159/4), 3)
> 
> There are MANY deeper nested expressions like this commonly used. The above
> can be written linearly as in
> 
> Temp1 <- cos(x)
> Temp2 <- max(Temp1, 3.14159/4)
> Result <- round(Temp2, 3)
> 
> Translation, for some variable x, calculate the cosine and take the maximum
> value of it as compared to pi/4 and round the result to three decimal
> places. Not an uncommon kind of thing to do and sometimes you can nest such
> things many layers deep and get hopelessly confused if not done somewhat
> linearly.
> 
> What pipes allow is to write this closer to the second way while not seeing
> or keeping any temporary variables around. The goal is to replace the FIRST
> argument to a function with whatever resulted as the value of the previous
> expression. That is often a vector or data.frame or list or any kind of
> object but can also be fairly complex as in a list of lists of matrices.
> 
> So you can still start with cos(x) OR you can write this where the x is
> removed from within and leaves cos() empty:
> 
> x %>% cos
> or
> x |> cos()
> 
> In the previous version of pipes the parentheses after cos() are optional if
> there are no additional arguments but the new pipe requires them.
> 
> So continuing the above, using multiple lines, the pipe looks like:
> 
> Result <-
>  x %>%
>  cos() %>%
>  max(3.14159/4) %>%
>  round(3)
> 
> This gives the same result but is arguably easier for some to read and
> follow. Nobody forces you to use it and for simple cases, most people don't.
> 
> There is a grouping of packages called the tidyverse that makes heavy use of
> pipes routine as they made most or all their functions such that the first
> argument is the one normally piped to and it can be very handy to write code
> that says, read in your data into a variable (a data.frame or tibble often)
> and PIPE IT to a function that renames some columns and PIPE the resulting
> modified object to a function that retains only selected rows and pipe that
> to a function that drops some of the columns and pipe that to a function
> that groups the items or sorts them and pipe that to a function that does a
> join with another object or generates a report or so many other things.
> 
> So the real answer is that piping is another WAY of doing things from a
> programmers perspective. Underneath it all, it is mostly syntactic sugar and
> the interpreter rearranges your code and performs the steps in what seems
> like a different order at times. Generally, you do not need to care.
> 
> 
> 
> -Original Message-
> From: R-help  On Behalf Of Sorkin, John
> Sent: Tuesday, January 3, 2023 11:49 AM
> To: 'R-help Mailing List' 
> Subject: [R] Pipe operator
> 
> I am trying to understand the reason for existence of the pipe operator,
> %>%, and when one should use it. It is my understanding that the operator
> sends the file to the left of the operator to the function immediately to
> the right of the operator:
> 
> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the
> result one obtains using the mean function directly, viz. mean(c(1:10)).
> What is the reason for having two syntactically different but semantically
> identical ways to call a function? Is one more efficient than the other?
> Does one use less memory than the other? 
> 
> P.S. Please forgive what might seem to be a question with an obvious answer.
> I am a programmer dinosaur. I have been programming for more than 50 years.
> When I started programming in the 1960s the only pipe one spoke about was a
> bong.  
> 
> 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, 

Re: [R] Pipe operator

2023-01-03 Thread avi.e.gross
Tim,

There are differences and this one can be huge.

The other pipe operators let you pass the current object to a later argument
instead of the first by using a period to represent where to put it. The new
one has a harder albeit flexible method by creating an anonymous function.

-Original Message-
From: R-help  On Behalf Of Ebert,Timothy Aaron
Sent: Tuesday, January 3, 2023 12:08 PM
To: Sorkin, John ; 'R-help Mailing List'

Subject: Re: [R] Pipe operator

The pipe shortens code and results in fewer variables because you do not
have to save intermediate steps. Once you get used to the idea it is useful.
Note that there is also the |> pipe that is part of base R. As far as I know
it does the same thing as %>%, or at my level of programing I have not
encountered a difference.

Tim

-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Tuesday, January 3, 2023 11:49 AM
To: 'R-help Mailing List' 
Subject: [R] Pipe operator

[External Email]

I am trying to understand the reason for existence of the pipe operator,
%>%, and when one should use it. It is my understanding that the operator
sends the file to the left of the operator to the function immediately to
the right of the operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the
result one obtains using the mean function directly, viz. mean(c(1:10)).
What is the reason for having two syntactically different but semantically
identical ways to call a function? Is one more efficient than the other?
Does one use less memory than the other?

P.S. Please forgive what might seem to be a question with an obvious answer.
I am a programmer dinosaur. I have been programming for more than 50 years.
When I started programming in the 1960s the only pipe one spoke about was a
bong.

John

__
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-help=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4
e084253a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C6380836
13362415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJB
TiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=fV9Ca3OAleDX%2BwuPJIONYStrA
daQhXTsq61jh2pLtDY%3D=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-proje
ct.org%2Fposting-guide.html=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4e0842
53a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083613362
415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6I
k1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=YUnV9kE1RcbB3BwM5gKwKwc3qNKhIVNF
txOxKmpbGrQ%3D=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.

__
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] Pipe operator

2023-01-03 Thread avi.e.gross
John,

The topic has indeed been discussed here endlessly but new people still
stumble upon it.

Until recently, the formal R language did not have a built-in pipe
functionality. It was widely used through an assortment of packages and
there are quite a few variations on the theme including different
implementations.

Most existing code does use the operator %>% but there is now a built-in |>
operator that is generally faster but is not as easy to use in a few cases.

Please forget the use of the word FILE here. Pipes are a form of syntactic
sugar that generally is about the FIRST argument to a function. They are NOT
meant to be used just for the trivial case you mention where indeed there is
an easy way to do things. Yes, they work in such situations. But consider a
deeply nested expression like this:

Result <- round(max(cos(x), 3.14159/4), 3)

There are MANY deeper nested expressions like this commonly used. The above
can be written linearly as in

Temp1 <- cos(x)
Temp2 <- max(Temp1, 3.14159/4)
Result <- round(Temp2, 3)

Translation, for some variable x, calculate the cosine and take the maximum
value of it as compared to pi/4 and round the result to three decimal
places. Not an uncommon kind of thing to do and sometimes you can nest such
things many layers deep and get hopelessly confused if not done somewhat
linearly.

What pipes allow is to write this closer to the second way while not seeing
or keeping any temporary variables around. The goal is to replace the FIRST
argument to a function with whatever resulted as the value of the previous
expression. That is often a vector or data.frame or list or any kind of
object but can also be fairly complex as in a list of lists of matrices.

So you can still start with cos(x) OR you can write this where the x is
removed from within and leaves cos() empty:

x %>% cos
or
x |> cos()

In the previous version of pipes the parentheses after cos() are optional if
there are no additional arguments but the new pipe requires them.

So continuing the above, using multiple lines, the pipe looks like:

Result <-
  x %>%
  cos() %>%
  max(3.14159/4) %>%
  round(3)

This gives the same result but is arguably easier for some to read and
follow. Nobody forces you to use it and for simple cases, most people don't.

There is a grouping of packages called the tidyverse that makes heavy use of
pipes routine as they made most or all their functions such that the first
argument is the one normally piped to and it can be very handy to write code
that says, read in your data into a variable (a data.frame or tibble often)
and PIPE IT to a function that renames some columns and PIPE the resulting
modified object to a function that retains only selected rows and pipe that
to a function that drops some of the columns and pipe that to a function
that groups the items or sorts them and pipe that to a function that does a
join with another object or generates a report or so many other things.

So the real answer is that piping is another WAY of doing things from a
programmers perspective. Underneath it all, it is mostly syntactic sugar and
the interpreter rearranges your code and performs the steps in what seems
like a different order at times. Generally, you do not need to care.



-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Tuesday, January 3, 2023 11:49 AM
To: 'R-help Mailing List' 
Subject: [R] Pipe operator

I am trying to understand the reason for existence of the pipe operator,
%>%, and when one should use it. It is my understanding that the operator
sends the file to the left of the operator to the function immediately to
the right of the operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the
result one obtains using the mean function directly, viz. mean(c(1:10)).
What is the reason for having two syntactically different but semantically
identical ways to call a function? Is one more efficient than the other?
Does one use less memory than the other? 

P.S. Please forgive what might seem to be a question with an obvious answer.
I am a programmer dinosaur. I have been programming for more than 50 years.
When I started programming in the 1960s the only pipe one spoke about was a
bong.  

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.

__
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] Pipe operator

2023-01-03 Thread Greg Snow
To expand a little on Christopher's answer.

The short answer is that having the different syntaxes can lead to
more readable code (when used properly).

Note that there are now 2 different (but somewhat similar) pipes
available in R (there could be more in some package(s) that I don't
know about, but will just talk about the main 2).

The %>% pipe comes from the magrittr package, but many other packages
now import that package.  But you need to load the magrittr package,
either directly or indirectly, before you can use that pipe.  The
magrittr pipe is a function call, so there is small increase in time
and memory for using it, but it is a small fraction of a second and a
few bytes of memory, so you probably will not notice the increased
usage.

The core R language now has a built in pipe |> which is handled by the
parser, so no extra function calls and you do not need to load any
extra packages (though you need a somewhat recent version of R, within
the last year or so).

The built-in |> pipe is a little pickier, you need to include the
parentheses in a function call, e.g. 1:10 |> mean() where the magrittr
pipe can work with that call or the function without parentheses, e.g.
1:10 %>% mean or 1:10 %>% mean(), this makes %>% a little easier to
work with anonymous functions.  If the previous return needs to be
passed to an argument other than the first, then %>% uses "." and |>
uses "_".

The magrittr package has additional versions of the pipe and some
functions that wrap around common operators to make it easier to use
them with pipes, so there are still advantages to loading that package
if any of those are helpful.

For a simple case like your example, the pipe probably does not help
with readability much, but as we string more function calls together.
For example, here are 3 ways to compute the geometric mean of the data
in a vector "x":

exp(mean(log(x)))

logx <- log(x)
mlx <- mean(logx)
exp(mtx)

x |>
   log() |>
   mean() |>
   exp()

These all do the same thing, but the first option is read from the
middle outward (which can be tricky) and is even more complicated if
you use additional arguments to any of the functions.
The second option reads top down, but requires creating intermediate
variables.  The last reads similar to the second, but without the
extra variables.  Spreading the series of function calls across
multiple rows makes it easier to read and easily lets you insert a
line like `print() |>` for debugging or checking intermediate results,
and single lines can easily be commented out to skip that step.

I have found myself using code like the following to compute a table,
print it, and compute the proportions all in one step:

table(f, g) |>
  print() |>
  prop.table()

The pipes also work very well with the tidyverse, or even the tidy
data ideas without those packages where we use a single function for
each change, e.g. start with a data frame, select a subset of the
columns, filter to a subset of the rows, mutate a column, join to
another data frame, then pass the final result to a modeling function
like `lm` (and then pass that result to a summary function).  This is
nicely readable when each step is its own line.

On Tue, Jan 3, 2023 at 9:49 AM Sorkin, John  wrote:
>
> I am trying to understand the reason for existence of the pipe operator, %>%, 
> and when one should use it. It is my understanding that the operator sends 
> the file to the left of the operator to the function immediately to the right 
> of the operator:
>
> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
> result one obtains using the mean function directly, viz. mean(c(1:10)). What 
> is the reason for having two syntactically different but semantically 
> identical ways to call a function? Is one more efficient than the other? Does 
> one use less memory than the other?
>
> P.S. Please forgive what might seem to be a question with an obvious answer. 
> I am a programmer dinosaur. I have been programming for more than 50 years. 
> When I started programming in the 1960s the only pipe one spoke about was a 
> bong.
>
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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] Pipe operator

2023-01-03 Thread Ebert,Timothy Aaron
Christopher Ryan sent this example
c(1:10) %>% sqrt() %>% mean() %>% plot()
I could code this as

A <- c(1:10)
B <- sqrt(A)
C<- mean(B)
plot(C)

I can then clean up by removing variables that I have no further use for.
rm(A, B, C)

The %>% operator is from the magriter package. It can be installed directly, or 
it is also installed if you use the tidyverse package (and possibly many 
others). The |> is base R, but it was added in R version 4.1.0.

I do not know if it increases processing speed.
It can save memory usage, especially if one is a messy programmer and does not 
tidy up after each task.
If you wanted to test execution times for bits of code there is the 
microbenchmark package.

Tim

-Original Message-
From: Sorkin, John  
Sent: Tuesday, January 3, 2023 12:13 PM
To: Ebert,Timothy Aaron ; 'R-help Mailing List' 

Subject: Re: Pipe operator

[External Email]

Tim,

Thank you for your reply. I did not know about the |> operator. Do both %>% and 
|> work in base R?

You suggested that the pipe operator can produce code with fewer variables. May 
I ask you to send a short example in which the pipe operator saves variables. 
Does said saving of variables speed up processing or result in less memory 
usage?

Thank you,
John


From: Ebert,Timothy Aaron 
Sent: Tuesday, January 3, 2023 12:07 PM
To: Sorkin, John; 'R-help Mailing List'
Subject: RE: Pipe operator

The pipe shortens code and results in fewer variables because you do not have 
to save intermediate steps. Once you get used to the idea it is useful. Note 
that there is also the |> pipe that is part of base R. As far as I know it does 
the same thing as %>%, or at my level of programing I have not encountered a 
difference.

Tim

-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Tuesday, January 3, 2023 11:49 AM
To: 'R-help Mailing List' 
Subject: [R] Pipe operator

[External Email]

I am trying to understand the reason for existence of the pipe operator, %>%, 
and when one should use it. It is my understanding that the operator sends the 
file to the left of the operator to the function immediately to the right of 
the operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
result one obtains using the mean function directly, viz. mean(c(1:10)). What 
is the reason for having two syntactically different but semantically identical 
ways to call a function? Is one more efficient than the other? Does one use 
less memory than the other?

P.S. Please forgive what might seem to be a question with an obvious answer. I 
am a programmer dinosaur. I have been programming for more than 50 years. When 
I started programming in the 1960s the only pipe one spoke about was a bong.

John

__
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-help=05%7C01%7Ctebert%40ufl.edu%7Cfa39e74a28354e3b3f6c08daedadd2ab%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083628073049849%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=qCK4H%2BtClknwzT9sQpQAUeei9I6dFz7vP904X0n39cw%3D=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7Ctebert%40ufl.edu%7Cfa39e74a28354e3b3f6c08daedadd2ab%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083628073049849%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=%2F75VhIpUPDD1VjEHWJ5HBKcQO6cYciTJSMPJ9nETmMQ%3D=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] Pipe operator

2023-01-03 Thread Sorkin, John
Tim,

Thank you for your reply. I did not know about the |> operator. Do both %>% and 
|> work in base R?

You suggested that the pipe operator can produce code with fewer variables. May 
I ask you to send a short example in which the pipe operator saves variables. 
Does said saving of variables speed up processing or result in less memory 
usage?

Thank you,
John


From: Ebert,Timothy Aaron 
Sent: Tuesday, January 3, 2023 12:07 PM
To: Sorkin, John; 'R-help Mailing List'
Subject: RE: Pipe operator

The pipe shortens code and results in fewer variables because you do not have 
to save intermediate steps. Once you get used to the idea it is useful. Note 
that there is also the |> pipe that is part of base R. As far as I know it does 
the same thing as %>%, or at my level of programing I have not encountered a 
difference.

Tim

-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Tuesday, January 3, 2023 11:49 AM
To: 'R-help Mailing List' 
Subject: [R] Pipe operator

[External Email]

I am trying to understand the reason for existence of the pipe operator, %>%, 
and when one should use it. It is my understanding that the operator sends the 
file to the left of the operator to the function immediately to the right of 
the operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
result one obtains using the mean function directly, viz. mean(c(1:10)). What 
is the reason for having two syntactically different but semantically identical 
ways to call a function? Is one more efficient than the other? Does one use 
less memory than the other?

P.S. Please forgive what might seem to be a question with an obvious answer. I 
am a programmer dinosaur. I have been programming for more than 50 years. When 
I started programming in the 1960s the only pipe one spoke about was a bong.

John

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=7dDMSg%2FmPQ5xXP6zu6MWLmARdtdlrYWb3mXPZQj0La0%3D=0
PLEASE do read the posting guide 
https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=l5BZyjup%2Bho%2FijE1zQMxb5JE3F5VfKBZpUKHYW4k4Fg%3D=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] Pipe operator

2023-01-03 Thread Ebert,Timothy Aaron
The pipe shortens code and results in fewer variables because you do not have 
to save intermediate steps. Once you get used to the idea it is useful. Note 
that there is also the |> pipe that is part of base R. As far as I know it does 
the same thing as %>%, or at my level of programing I have not encountered a 
difference.

Tim

-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Tuesday, January 3, 2023 11:49 AM
To: 'R-help Mailing List' 
Subject: [R] Pipe operator

[External Email]

I am trying to understand the reason for existence of the pipe operator, %>%, 
and when one should use it. It is my understanding that the operator sends the 
file to the left of the operator to the function immediately to the right of 
the operator:

c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the 
result one obtains using the mean function directly, viz. mean(c(1:10)). What 
is the reason for having two syntactically different but semantically identical 
ways to call a function? Is one more efficient than the other? Does one use 
less memory than the other?

P.S. Please forgive what might seem to be a question with an obvious answer. I 
am a programmer dinosaur. I have been programming for more than 50 years. When 
I started programming in the 1960s the only pipe one spoke about was a bong.

John

__
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-help=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4e084253a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083613362415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=fV9Ca3OAleDX%2BwuPJIONYStrAdaQhXTsq61jh2pLtDY%3D=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7Ctebert%40ufl.edu%7C73edce5d4e084253a39008daedaa653f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083613362415015%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=YUnV9kE1RcbB3BwM5gKwKwc3qNKhIVNFtxOxKmpbGrQ%3D=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] $ subset operator behavior in lapply

2022-10-28 Thread Hilmar Berger
Hi Andrew,

thanks a lot, that fully explains it.

Sorry for the HTML text. For the record I put the original code again
below.

Best regards

Hilmar


On 27.10.22 18:34, Andrew Simmons wrote:
> $ does not evaluate its second argument, it does something like
> as.character(substitute(name)).
>
> You should be using
>
> lapply(list, function(x) x$a)
>
> or
>
> lapply(list, `[[`, "a")
>
>
> On Thu, Oct 27, 2022, 12:29 Hilmar Berger  wrote:
>
> Dear all,
>
> I'm a little bit surprised by the behavior of the $ operator when used
> in lapply - any indication what might be wrong is appreciated.
>

 > xx = list(A=list(a=1:3, b=LETTERS[1:3]),"B"=list(a=7:9, b=LETTERS[7:9]))
 > lapply(xx,`$`,"a")
$A
NULL

$B
NULL

 > `$`(xx[[1]],"a")
[1] 1 2 3
 > lapply(xx,`[`,"a")
$A
$A$a
[1] 1 2 3


$B
$B$a
[1] 7 8 9


> Any idea why I
> `$`(object, name) works when applied to the single list element
> but not
> within lapply (in contrast to `[`)?
> I checked the help page of the extraction operators but could not find
> anything that explains this. Thanks and best regards Hilmar >
> sessionInfo() R version 4.2.1 (2022-06-23) Platform:
> x86_64-pc-linux-gnu
> (64-bit) Running under: Ubuntu 20.04.5 LTS Matrix products: default
> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 LAPACK:
> /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 locale: [1]
> LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=de_DE.UTF-8
> LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=de_DE.UTF-8
> LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C [9]
> LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=de_DE.UTF-8
> LC_IDENTIFICATION=C attached base packages: [1] stats graphics
> grDevices
> utils datasets methods base loaded via a namespace (and not attached):
> [1] compiler_4.2.1
>
>         [[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] $ subset operator behavior in lapply

2022-10-27 Thread Jeff Newmiller
Your message is garbled. Please send plain text to the mailing list.

On October 27, 2022 2:31:47 AM PDT, Hilmar Berger  wrote:
>Dear all,
>
>I'm a little bit surprised by the behavior of the $ operator when used
>in lapply - any indication what might be wrong is appreciated.
>
>> xx = list(A=list(a=1:3, b=LETTERS[1:3]),"B"=list(a=7:9, b=LETTERS[7:9]))  > 
>> lapply(xx,`$`,"a") $A NULL $B NULL > `$`(xx[[1]],"a") [1] 1 2 3 >
>lapply(xx,`[`,"a") $A $A$a [1] 1 2 3 $B $B$a [1] 7 8 9 Any idea why I
>`$`(object, name) works when applied to the single list element but not
>within lapply (in contrast to `[`)?
>I checked the help page of the extraction operators but could not find
>anything that explains this. Thanks and best regards Hilmar >
>sessionInfo() R version 4.2.1 (2022-06-23) Platform: x86_64-pc-linux-gnu
>(64-bit) Running under: Ubuntu 20.04.5 LTS Matrix products: default
>BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 LAPACK:
>/usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 locale: [1]
>LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=de_DE.UTF-8
>LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=de_DE.UTF-8
>LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C [9]
>LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=de_DE.UTF-8
>LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices
>utils datasets methods base loaded via a namespace (and not attached):
>[1] compiler_4.2.1
>
>   [[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] $ subset operator behavior in lapply

2022-10-27 Thread Andrew Simmons
$ does not evaluate its second argument, it does something like
as.character(substitute(name)).

You should be using

lapply(list, function(x) x$a)

or

lapply(list, `[[`, "a")


On Thu, Oct 27, 2022, 12:29 Hilmar Berger  wrote:

> Dear all,
>
> I'm a little bit surprised by the behavior of the $ operator when used
> in lapply - any indication what might be wrong is appreciated.
>
> > xx = list(A=list(a=1:3, b=LETTERS[1:3]),"B"=list(a=7:9,
> b=LETTERS[7:9]))  > lapply(xx,`$`,"a") $A NULL $B NULL > `$`(xx[[1]],"a")
> [1] 1 2 3 >
> lapply(xx,`[`,"a") $A $A$a [1] 1 2 3 $B $B$a [1] 7 8 9 Any idea why I
> `$`(object, name) works when applied to the single list element but not
> within lapply (in contrast to `[`)?
> I checked the help page of the extraction operators but could not find
> anything that explains this. Thanks and best regards Hilmar >
> sessionInfo() R version 4.2.1 (2022-06-23) Platform: x86_64-pc-linux-gnu
> (64-bit) Running under: Ubuntu 20.04.5 LTS Matrix products: default
> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 LAPACK:
> /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 locale: [1]
> LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=de_DE.UTF-8
> LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=de_DE.UTF-8
> LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C [9]
> LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=de_DE.UTF-8
> LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices
> utils datasets methods base loaded via a namespace (and not attached):
> [1] compiler_4.2.1
>
> [[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] modulus operator?

2017-05-21 Thread Peter Dalgaard
Or maybe Mod(), but please, Heidi, don't expect the rest of the world to guess 
your intentions like that...

-pd

> On 21 May 2017, at 10:27 , Jim Lemon  wrote:
> 
> Hi Heidi,
> I think you are looking for the %% operator. See the Arithmetic help
> page in the base package.
> 
> Jim
> 
> On Sun, May 21, 2017 at 10:28 AM, McGann, Heidi  wrote:
>> 
>> __
>> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.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] modulus operator?

2017-05-21 Thread Jim Lemon
Hi Heidi,
I think you are looking for the %% operator. See the Arithmetic help
page in the base package.

Jim

On Sun, May 21, 2017 at 10:28 AM, McGann, Heidi  wrote:
>
> __
> 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] Logical operator in R

2016-01-22 Thread li li
I see. Thanks!

2016-01-22 10:57 GMT-05:00 Rmh :

> FAQ 7.31
>
> in this case subtract the two numbers and see that
> they differ by about 1e-16
>
> Sent from my iPhone
>
> > On Jan 22, 2016, at 10:46, li li  wrote:
> >
> > Hi all,
> >  I encountered the following strange phenomenon.
> > For some reason, the obs_p[1] and res1$st_p[89] have
> > the same value but when I run "==", it returns FALSE.
> > Can anyone help give some explanation on this?
> >  Thanks very much!
> >Hanna
> >
> >> obs_p[1]
> > [1] 0.002201438
> >> res1$st_p[89]
> > [1] 0.002201438
> >> res1$st_p[89]==obs_p[1]
> > [1] FALSE
> >> res1$st_p[89] > [1] FALSE
> >> res1$st_p[89]>obs_p[1]
> > [1] TRUE
> >
> >[[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] Logical operator in R

2016-01-22 Thread Rick Bilonick

On 01/22/2016 10:46 AM, li li wrote:

Hi all,
   I encountered the following strange phenomenon.
For some reason, the obs_p[1] and res1$st_p[89] have
the same value but when I run "==", it returns FALSE.
Can anyone help give some explanation on this?
   Thanks very much!
 Hanna


obs_p[1]

[1] 0.002201438

res1$st_p[89]

[1] 0.002201438

res1$st_p[89]==obs_p[1]

[1] FALSE

res1$st_p[89]obs_p[1]

[1] TRUE

[[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.
I believe the decimal representation is only approximate. The real 
internal values in binary are different. If you want to have comparisons 
like this result in being considered equal, I think there is a way to 
use a fuzzy comparison but I don't remember the details.


Rick

--
Richard A. Bilonick, PhD
Assistant Professor
Dept. of Ophthalmology, School of Medicine
Dept. of Biostatistics, Graduate School of Public Health
Dept. of Orthodontics, School of Dental Medicine
University of Pittsburgh
Principal Investigator for the Pittsburgh Aerosol Research
 and Inhalation Epidemiology Study (PARIES)
412 647 5756

__
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] Logical operator in R

2016-01-22 Thread Bert Gunter
FAQ 7.31

-- Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jan 22, 2016 at 7:46 AM, li li  wrote:
> Hi all,
>   I encountered the following strange phenomenon.
> For some reason, the obs_p[1] and res1$st_p[89] have
> the same value but when I run "==", it returns FALSE.
> Can anyone help give some explanation on this?
>   Thanks very much!
> Hanna
>
>> obs_p[1]
> [1] 0.002201438
>> res1$st_p[89]
> [1] 0.002201438
>> res1$st_p[89]==obs_p[1]
> [1] FALSE
>> res1$st_p[89] [1] FALSE
>> res1$st_p[89]>obs_p[1]
> [1] TRUE
>
> [[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] Logical operator in R

2016-01-22 Thread Fábio Magalhães
Hi,

This can get you started:
https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f


--Fabio

On Fri, Jan 22, 2016 at 1:46 PM, li li  wrote:

> Hi all,
>   I encountered the following strange phenomenon.
> For some reason, the obs_p[1] and res1$st_p[89] have
> the same value but when I run "==", it returns FALSE.
> Can anyone help give some explanation on this?
>   Thanks very much!
> Hanna
>
> > obs_p[1]
> [1] 0.002201438
> > res1$st_p[89]
> [1] 0.002201438
> > res1$st_p[89]==obs_p[1]
> [1] FALSE
> > res1$st_p[89] [1] FALSE
> > res1$st_p[89]>obs_p[1]
> [1] TRUE
>
> [[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] Logical operator in R

2016-01-22 Thread Rmh
FAQ 7.31

in this case subtract the two numbers and see that
they differ by about 1e-16

Sent from my iPhone

> On Jan 22, 2016, at 10:46, li li  wrote:
> 
> Hi all,
>  I encountered the following strange phenomenon.
> For some reason, the obs_p[1] and res1$st_p[89] have
> the same value but when I run "==", it returns FALSE.
> Can anyone help give some explanation on this?
>  Thanks very much!
>Hanna
> 
>> obs_p[1]
> [1] 0.002201438
>> res1$st_p[89]
> [1] 0.002201438
>> res1$st_p[89]==obs_p[1]
> [1] FALSE
>> res1$st_p[89] [1] FALSE
>> res1$st_p[89]>obs_p[1]
> [1] TRUE
> 
>[[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] S4 / operator [ : Compatibility issue between lme4 and kml

2015-06-06 Thread cgenolin
Thanks a lot for your time. Two questions:

1/ Shall I submit a bug report?

2/ In your point 2), I cannot find the verb... Is my english not good enough
to understand it, or is it a not-ended sentence?

:-)



--
View this message in context: 
http://r.789695.n4.nabble.com/S4-operator-Compatibility-issue-between-lme4-and-kml-tp4708236p4708282.html
Sent from the R help mailing list archive at Nabble.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] S4 / operator [ : Compatibility issue between lme4 and kml

2015-06-06 Thread Martin Morgan

On 06/05/2015 10:52 AM, Martin Maechler wrote:

Christophe Genolini cgeno...@u-paris10.fr
 on Fri, 5 Jun 2015 00:36:42 -0700 writes:


  Hi all,
  There is a compatibility issue between the package 'lme4' and my package
  'kml'. I define the [ operator. It works just fine in my package (1). 
If I
  try to use the lme4 package, then it does no longer work (2). Moreover, 
it
  has some kind of strange behavior (3). Do you know what is wrong? Any 
idea
  of how I can correct that?

  Here is a reproductible example, and the same code with the result 
follows.

  Thanks for your help
  Christophe

   [ ... I'm providing slightly different code below  ]


--- 8 - Execution of the previous code ---



library(kml)

Le chargement a nécessité le package : clv
Le chargement a nécessité le package : cluster
Le chargement a nécessité le package : class
Le chargement a nécessité le package : longitudinalData
Le chargement a nécessité le package : rgl
Le chargement a nécessité le package : misc3d

dn - gald(1)



  ###
### (1) the [ operator works just fine



dn[traj]

   t0   t1t2t3t4   t5   t6t7t8t9   t10
i1 -3.11 4.32  2.17  1.82  4.90 7.34 0.83 -2.70  5.36  4.96  3.16
i2 -7.11 1.40 -2.40 -2.96  4.31 0.50 1.25  0.52 -0.04  7.55  5.50
i3  2.80 6.23  6.08  2.87  2.58 2.88 6.58 -2.38  2.30 -1.74 -3.23
i4  2.24 0.91  6.50 10.92 11.32 7.79 7.78 10.69  9.15  1.07 -0.51



  ###
### (2) using 'lme4', it does no longer work



library(lme4)

Le chargement a nécessité le package : Matrix
Le chargement a nécessité le package : Rcpp

dn[traj]

Error in x[i, j] :
   erreur d'évaluation de l'argument 'j' lors de la sélection d'une méthode
pour la fonction '[' : Erreur : l'argument j est manquant, avec aucune
valeur par défaut



  ###
### (3) If I define again the [, it does not work the first time I call
it, but it work the second time!

setMethod([,

+   signature=signature(x=ClusterLongData, i=character, j=ANY,drop=ANY),
+   definition=function (x, i, j=missing, ..., drop = TRUE){


Your file has two definitions of

  setMethod([, c(ClusterLongData, ...

I deleted the first one.

The second definition had

signature=signature(x=ClusterLongData, i=character, j=ANY,drop=ANY),

whereas probably you mean to say that you'll handle

signature=signature(x=ClusterLongData, i=character,
j=missing, drop=ANY)

The next line says

definition=function (x, i, j=missing, ..., drop = TRUE){

which provides a default value for 'j' when j is not provided by the user. Thus 
later when you say


   x[i, j]

you are performing dn[traj, missing] when probably you meant

  x[i, , drop=drop]

Making these changes, so the definition is

setMethod(
[,
signature=signature(x=ClusterLongData, i=character, j=missing,
  drop=ANY),
definition=function (x, i, j, ..., drop = TRUE){
if (is.numeric(i)) {
stop([ClusterLongData:getteur]: to get a clusters list, use 
['ci'])
}else{}
if (i %in% c(criterionValues, criterionValuesAsMatrix)){
j - x['criterionActif']
}else{}
if (i %in% c(CRITERION_NAMES, criterionActif, CLUSTER_NAMES,
 criterionValues, criterionValuesAsMatrix, sorted,
 initializationMethod)) {
x - as(x, ListPartition)
}else{
x - as(x, LongData)
}
x[i, , drop=drop]
})

Allows operations to work correctly.

 library(kml)
Loading required package: clv
Loading required package: cluster
Loading required package: class
Loading required package: longitudinalData
Loading required package: rgl
Loading required package: misc3d
 library(Matrix)
 x = gald(1)[traj]
 x
  t0t1t2t3t4t5t6t7t8t9   t10
i1 -3.18 -1.19 -1.17  1.56 -0.70  1.78 -0.95 -2.00 -5.05  1.05  2.84
i2  3.51  1.72  6.97  6.09  7.81  8.33  9.54 14.38 16.14 12.82 13.86
i3  9.60 11.59  9.09  6.31  9.24  7.69  4.26 -0.80  2.70  1.63  1.21
i4 -0.54  3.80  6.05 10.41 12.60 12.32 10.33 11.05  7.89  5.21  0.67

It's hard to tell whether is an issue with the methods package, or just that 
Matrix offered a better nearest 'method' than those provided by kml / 
longitudinalData.





+   x - as(x, LongData)
+   return(x[i, j])
+ }
+ )
[1] [



### No working the first time I use it

dn[traj]

Error in dn[traj] :
   l'argument j est manquant, avec aucune valeur par défaut



### But working the second time

dn[traj]

   t0   t1t2t3t4   t5   t6t7t8t9   t10
i1 -3.11 4.32  2.17  1.82  4.90 7.34 0.83 -2.70  5.36  4.96  3.16
i2 -7.11 1.40 -2.40 -2.96  4.31 0.50 1.25  0.52 -0.04  7.55  5.50
i3  2.80 6.23  6.08  2.87  2.58 2.88 6.58 -2.38  2.30 -1.74 -3.23
i4  2.24 0.91  6.50 10.92 11.32 7.79 7.78 10.69  9.15  1.07 -0.51


I have made some investigations, but have to stop for now, and
leave this hopefully to 

Re: [R] S4 / operator [ : Compatibility issue between lme4 and kml

2015-06-05 Thread Martin Maechler
 Christophe Genolini cgeno...@u-paris10.fr
 on Fri, 5 Jun 2015 00:36:42 -0700 writes:

 Hi all,
 There is a compatibility issue between the package 'lme4' and my package
 'kml'. I define the [ operator. It works just fine in my package (1). 
If I
 try to use the lme4 package, then it does no longer work (2). Moreover, it
 has some kind of strange behavior (3). Do you know what is wrong? Any idea
 of how I can correct that?

 Here is a reproductible example, and the same code with the result 
follows.

Dear Christophe,
can you please specify the exact sessionInfo() ?
(after loading both kml and lme4).

'Matrix' has been updated on CRAN very recently, and 
'lme4' is about to be update very soon,
so this *is* a somewhat interesting and important problem,
but the exact versions of the packages *do* matter.

Bonnes salutations!

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] S4 / operator [ : Compatibility issue between lme4 and kml

2015-06-05 Thread Martin Maechler
 Martin Maechler maech...@stat.math.ethz.ch
 on Fri, 5 Jun 2015 11:33:46 +0200 writes:

 Christophe Genolini cgeno...@u-paris10.fr
 on Fri, 5 Jun 2015 00:36:42 -0700 writes:

 Hi all,
 There is a compatibility issue between the package 'lme4' and my package
 'kml'. I define the [ operator. It works just fine in my package (1). 
If I
 try to use the lme4 package, then it does no longer work (2). Moreover, 
it
 has some kind of strange behavior (3). Do you know what is wrong? Any 
idea
 of how I can correct that?

 Here is a reproductible example, and the same code with the result 
follows.

 Dear Christophe,
 can you please specify the exact sessionInfo() ?
 (after loading both kml and lme4).

 'Matrix' has been updated on CRAN very recently, and 
 'lme4' is about to be update very soon,
 so this *is* a somewhat interesting and important problem,
 but the exact versions of the packages *do* matter.

As a matter of fact,  

1) the package versions don't seem to matter much.

2) lme4 is *not* involved directly:
   Much worse, it is the 'Matrix' package.

If you replace 'lme4' by 'Matrix' in your code, you get the same
symptoms.

... I'm investigating ..

Martin

__
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] S4 / operator [ : Compatibility issue between lme4 and kml

2015-06-05 Thread Martin Maechler
 Christophe Genolini cgeno...@u-paris10.fr
 on Fri, 5 Jun 2015 00:36:42 -0700 writes:

 Hi all,
 There is a compatibility issue between the package 'lme4' and my package
 'kml'. I define the [ operator. It works just fine in my package (1). 
If I
 try to use the lme4 package, then it does no longer work (2). Moreover, it
 has some kind of strange behavior (3). Do you know what is wrong? Any idea
 of how I can correct that?

 Here is a reproductible example, and the same code with the result 
follows.

 Thanks for your help
 Christophe

  [ ... I'm providing slightly different code below  ]

 --- 8 - Execution of the previous code ---

  library(kml)
 Le chargement a nécessité le package : clv
 Le chargement a nécessité le package : cluster
 Le chargement a nécessité le package : class
 Le chargement a nécessité le package : longitudinalData
 Le chargement a nécessité le package : rgl
 Le chargement a nécessité le package : misc3d
  dn - gald(1)

  ###
 ### (1) the [ operator works just fine

  dn[traj]
   t0   t1t2t3t4   t5   t6t7t8t9   t10
 i1 -3.11 4.32  2.17  1.82  4.90 7.34 0.83 -2.70  5.36  4.96  3.16
 i2 -7.11 1.40 -2.40 -2.96  4.31 0.50 1.25  0.52 -0.04  7.55  5.50
 i3  2.80 6.23  6.08  2.87  2.58 2.88 6.58 -2.38  2.30 -1.74 -3.23
 i4  2.24 0.91  6.50 10.92 11.32 7.79 7.78 10.69  9.15  1.07 -0.51

  ###
 ### (2) using 'lme4', it does no longer work

  library(lme4)
 Le chargement a nécessité le package : Matrix
 Le chargement a nécessité le package : Rcpp
  dn[traj]
 Error in x[i, j] :
   erreur d'évaluation de l'argument 'j' lors de la sélection d'une méthode
 pour la fonction '[' : Erreur : l'argument j est manquant, avec aucune
 valeur par défaut

  ###
 ### (3) If I define again the [, it does not work the first time I call
 it, but it work the second time!
  setMethod([,
 +   signature=signature(x=ClusterLongData, i=character, 
 j=ANY,drop=ANY),
 +   definition=function (x, i, j=missing, ..., drop = TRUE){
 +   x - as(x, LongData)
 +   return(x[i, j])
 + }
 + )
 [1] [

 ### No working the first time I use it
  dn[traj]
 Error in dn[traj] :
   l'argument j est manquant, avec aucune valeur par défaut

 ### But working the second time
  dn[traj]
   t0   t1t2t3t4   t5   t6t7t8t9   t10
 i1 -3.11 4.32  2.17  1.82  4.90 7.34 0.83 -2.70  5.36  4.96  3.16
 i2 -7.11 1.40 -2.40 -2.96  4.31 0.50 1.25  0.52 -0.04  7.55  5.50
 i3  2.80 6.23  6.08  2.87  2.58 2.88 6.58 -2.38  2.30 -1.74 -3.23
 i4  2.24 0.91  6.50 10.92 11.32 7.79 7.78 10.69  9.15  1.07 -0.51 

I have made some investigations, but have to stop for now, and
leave this hopefully to others knowledgable about S4 method
dispatch, etc :

1) I am confident to say that you have uncovered an unfelicity if
  not a bug in R.

2) I am also pretty confident that the [ methods that you
  define in 'kml' and in the package '

3) Diagnosing is not easy: As you have shown yourself above,
  in some situations the bug bites and if you repeat the *same*
  code, things work.

  This is related to the fact that S4 methods are __cached__
  (so next time they are found more quickly) under some
  circumstances, and the cache is cleared under other such circumstances.

3b) Actually, I am sure that we have seen +/- the same problem many
months ago, in other contexts but did not get down to it;
and at the moment, I cannot quickly find where to look for
the problem there...


##--- 8 Commented (incl output) reproducible code--
library(kml)

### Creating some data
dn - gald(1)
(dnt - dn[traj])

showMethods([)
## Function: [ (package base)
## x=ClusterLongData, i=character
## x=ListPartition, i=ANY
## x=LongData, i=ANY
## x=LongData, i=character
## (inherited from: x=LongData, i=ANY)
## x=LongData3d, i=ANY
## x=nonStructure, i=ANY
## x=ParChoice, i=ANY
## x=ParKml, i=ANY
## x=ParLongData, i=ANY
## x=Partition, i=ANY
## x=ParWindows, i=ANY

### using Matrix  (or lme4, which 'Depends' on Matrix; hence same effect)
library(Matrix)
dn[traj]
## Error in x[i, j] :
##   error in evaluating the argument 'j' in selecting a method for function 
'[': Error: argument j is missing, with no default
traceback()
## 3: x[i, j]
## 2: dn[traj]
## 1: dn[traj]
(ms - methods(`[`)) ## 81 methods

## MM: debugging :
trace([, browser, signature=c(ClusterLongData, character, missing,   
missing))
trace([, browser, signature=c(LongData,character, character, 
missing))
dn[traj]
## - you get into the browser, just press   c   twice (once for each trace)
## == it works !!

## Remove the tracing :
untrace([, signature=c(ClusterLongData, character, missing,   
missing))
untrace([, signature=c(LongData,character, character, 
missing))
dn[traj]
## Error in dn[traj] : argument j is missing, with no default

## Debugging only the *inner* function:
trace([, browser, 

Re: [R] lag operator on a zoo object - code sharing

2014-10-15 Thread Achim Zeileis

On Wed, 15 Oct 2014, jpm miao wrote:


Hi,
  I could not find a nice lag operator on zoo object. Perhaps there is,
but I just couldn't find it.


See ?lag.zoo.


Basically I want the operator to return the
lagged zoo object (with one or more variables ) with the original date. For
example, if I write lag(x, -3), then I got the lagged series, but the first
three observations are deleted.


Set na.pad = TRUE.


My code could work, but is not polished.


Yes, the ncol() does not work on vectors without dim and leads cannot be 
computed.



Someone helps or comments?


lagzoo-function(x, lag_n)
{

 if(is.zoo(x)==FALSE)
 {
   stop(zoo objects for lagzoo, please)
 }
 if(ncol(x)==1)
 {
   y-x
 t-time(x)
 n-length(t)

 y[(lag_n+1):n]-x[1:(n-lag_n)]
 y[1:lag_n]-NA
 return(y)
 }
 else
 {
   y-x
   n-nrow(x)
   y[(lag_n+1):n,]-x[1:(n-lag_n),]
   y[1:lag_n,]-NA
   return(y)
 }
}

[[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] lag operator on a zoo object - code sharing

2014-10-15 Thread Pascal Oettli
Hi,

You probably missed the na.pad argument of the lag function (for
zoo objects).
?zoo:::lag.zoo

Regards,
Pascal

On Wed, Oct 15, 2014 at 6:00 PM, jpm miao miao...@gmail.com wrote:
 Hi,
I could not find a nice lag operator on zoo object. Perhaps there is,
 but I just couldn't find it. Basically I want the operator to return the
 lagged zoo object (with one or more variables ) with the original date. For
 example, if I write lag(x, -3), then I got the lagged series, but the first
 three observations are deleted. My code could work, but is not polished.
 Someone helps or comments?


 lagzoo-function(x, lag_n)
 {

   if(is.zoo(x)==FALSE)
   {
 stop(zoo objects for lagzoo, please)
   }
   if(ncol(x)==1)
   {
 y-x
   t-time(x)
   n-length(t)

   y[(lag_n+1):n]-x[1:(n-lag_n)]
   y[1:lag_n]-NA
   return(y)
   }
   else
   {
 y-x
 n-nrow(x)
 y[(lag_n+1):n,]-x[1:(n-lag_n),]
 y[1:lag_n,]-NA
 return(y)
   }
 }

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



-- 
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan

__
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] Assignment Operator in mclapply

2013-02-25 Thread Jeff Newmiller
Why do you equate using - with returning multiple items from a loop? There 
are valid reasons to use -, but the people who want to use it practically 
never have them.

Just return a list of the items you want to return from within the function.

squares - mclapply(1:10, function(x){result - list(); result$x - x; 
result$x2 - x^2; result})

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

Dario Strbenac d.strbe...@garvan.org.au wrote:

I sometimes need to return multiple items from a loop. Is it possible
to have the - operator work the same for mclapply as for lapply ?

 extra - list()

 squares - mclapply(1:10, function(x){extra[[x]] - x; x^2;})
 extra
list()

 squares - lapply(1:10, function(x){extra[[x]] - x; x^2;})
 extra
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

[[7]]
[1] 7

[[8]]
[1] 8

[[9]]
[1] 9

[[10]]
[1] 10

My question is like that of
http://tolstoy.newcastle.edu.au/R/e6/help/09/03/8329.html which is not
answered.

--
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia
__
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] Logical operator and lists

2013-01-08 Thread Gerrit Eichner

Hello, Dominic,

untested:

data - lapply( data, function( x) x[ x == ] - NA

 Hth  --  Gerrit


On Tue, 8 Jan 2013, Dominic Roye wrote:


Hello R-Helpers,

I have a slight problem with the expresion data[data==] - NA which works
well for a data.frame. But now i must use the same for a list of
data.frames.

My idea is data[[]][data==] but it don´t work.

Thanks!!

Dominic__
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] Logical operator and lists

2013-01-08 Thread arun
HI,
This should also work:

 set.seed(5)
 list1-lapply(1:3,function(i) 
data.frame(col1=sample(c(1:5,),10,replace=TRUE), 
value=rnorm(10),stringsAsFactors=FALSE))

 lapply(list1,function(x) {x[x==]-NA;x})
A.K.

- Original Message -
From: Dominic Roye dominic.r...@gmail.com
To: R help r-help@r-project.org
Cc: 
Sent: Tuesday, January 8, 2013 7:16 AM
Subject: [R] Logical operator and lists

Hello R-Helpers,

I have a slight problem with the expresion data[data==] - NA which works
well for a data.frame. But now i must use the same for a list of
data.frames.

My idea is data[[]][data==] but it don´t work.

Thanks!!

Dominic

    [[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] Logical operator and lists

2013-01-08 Thread arun
Hi,
Try this:

 set.seed(5)
 list1-lapply(1:3,function(i) 
data.frame(col1=sample(c(1:5,),10,replace=TRUE), 
value=rnorm(10),stringsAsFactors=FALSE))
 res-lapply(list1,function(x) {x[apply(x,2,function(y) y==)]-NA;x})
res[[1]]
#   col1  value
#1 2 -0.6029080
#2 5 -0.4721664
#3  NA -0.6353713
#4 2 -0.2857736
#5 1  0.1381082
#6 5  1.2276303
#7 4 -0.8017795
#8 5 -1.0803926
#9  NA -0.1575344
#10    1 -1.0717600
A.K.



- Original Message -
From: Dominic Roye dominic.r...@gmail.com
To: R help r-help@r-project.org
Cc: 
Sent: Tuesday, January 8, 2013 7:16 AM
Subject: [R] Logical operator and lists

Hello R-Helpers,

I have a slight problem with the expresion data[data==] - NA which works
well for a data.frame. But now i must use the same for a list of
data.frames.

My idea is data[[]][data==] but it don´t work.

Thanks!!

Dominic

    [[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] Logical operator and lists

2013-01-08 Thread arun
Hi,

If you don't return(x) or x at the end,
 set.seed(5)
 list1-lapply(1:3,function(i) 
data.frame(col1=sample(c(1:5,),10,replace=TRUE), 
value=rnorm(10),stringsAsFactors=FALSE))
 lapply(list1,function(x) x[x==])
#[[1]]
#[1]  

#[[2]]
#character(0)

#[[3]]
#[1]  


 lapply(list1,function(x) x[x==]-NA)
#[[1]]
#[1] NA
#
#[[2]]
#[1] NA
#
#[[3]]
#[1] NA
 lapply(list1,function(x) x[x==]-rep(NA,length(x[x==])))
#[[1]]
#[1] NA NA
#
#[[2]]
#logical(0)
#
#[[3]]
#[1] NA NA NA NA NA NA
 lapply(list1,function(x) {x[x==]-NA;return(x)})
#or
lapply(list1,function(x) {x[x==]-NA;x})
#or
 lapply(list1,function(x) {x[x==]-rep(NA,length(x[x==]));x})
[[1]]
#   col1  value
#1 2 -0.6029080
#2 5 -0.4721664
#3  NA -0.6353713
#4 2 -0.2857736
#5 1  0.1381082
#6 5  1.2276303
#7 4 -0.8017795
#8 5 -1.0803926
#9  NA -0.1575344
#10    1 -1.0717600
--

A.K.




From: Dominic Roye dominic.r...@gmail.com
To: arun smartpink...@yahoo.com 
Sent: Tuesday, January 8, 2013 1:06 PM
Subject: Re: [R] Logical operator and lists


hi, 

Can you explain me why without ;x at the end, i get only NA?

 c
[[1]]
[1] NA

[[2]]
[1] NA


2013/1/8 arun smartpink...@yahoo.com

HI,
This should also work:


 set.seed(5)
 list1-lapply(1:3,function(i) 
data.frame(col1=sample(c(1:5,),10,replace=TRUE), 
value=rnorm(10),stringsAsFactors=FALSE))

 lapply(list1,function(x) {x[x==]-NA;x})

A.K.

- Original Message -
From: Dominic Roye dominic.r...@gmail.com
To: R help r-help@r-project.org
Cc:
Sent: Tuesday, January 8, 2013 7:16 AM
Subject: [R] Logical operator and lists


Hello R-Helpers,

I have a slight problem with the expresion data[data==] - NA which works
well for a data.frame. But now i must use the same for a list of
data.frames.

My idea is data[[]][data==] but it don´t work.

Thanks!!

Dominic


    [[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] assigment operator question

2012-04-08 Thread Thomas Lumley
On Sun, Apr 8, 2012 at 11:57 AM, Duncan Murdoch
murdoch.dun...@gmail.com wrote:
 On 12-04-07 4:51 PM, Henrik Bengtsson wrote:

 On Sat, Apr 7, 2012 at 1:30 PM, Mark Heckmannmark.heckm...@gmx.de
  wrote:

 Hello,

 using the- assignment operator I do not understand why the following
 does not work.

 l- list()
 l
 list()
 l$arg1- test
 error in l$arg1- test : Objekt 'l' not found

 ?- says:  The operators- and -  cause a search to made through
 the environment for an existing definition of the variable being assigned.
 If such a variable is found (and its binding is not locked) then its value
 is redefined, otherwise assignment takes place in the global environment. 

 Still I do noch understand why the above does not work. The object l is
 in the global environment. Can someone explain it to me?


 Yes, the object 'l' is in the global environment, but 'l$arg1' is not,
 cf. exists(l$arg1) and exists(l).  Instead, this works:


 I don't think that is the problem:  - normally works with complex
 assignments.  I think the problems are

  - Mark was working at top level in the console
  - The documentation isn't clear about what happens in that case.
  - R is inconsistent between simple and complex assignments with -.

 The - operator is designed to be used in a function, and it makes an
 assignment in the environment of the function, not in the local evaluation
 frame.  If it can't find a variable there, it assigns into the global
 environment.  So if the l$arg1 - test was a line in a function,
 everything would have worked as expected.

 When you use it at the top level, it tries looking in the parent of the
 global environment, its parent, and so on, back to the empty environment at
 the root of all the namespaces.  If you happened to use a name like mean
 which exists, it will try to assign to that object (and fail, because that
 binding is locked).

 If you pick a name that doesn't exist, then R is inconsistent.  If you're
 doing a simple assignment like

 l - 5

 it will be done in the global environment, creating a new variable if
 necessary.  If you're doing a complex assignment like

 l$arg1 - test

 it will just fail, there's no global environment fallback.

 I don't think this inconsistency is really worth fixing, since - is
 designed for use in functions.  There really isn't any use for it at the top
 level.

 But maybe all of this should be documented.


There's quite a bit of detail in section 3.4 of the R Language
Definition, though it's focused on subscripting superassignments.   It
even covers pathological cases such as  x[is.na(x)] - 0

  -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland

__
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] assigment operator question

2012-04-07 Thread Henrik Bengtsson
On Sat, Apr 7, 2012 at 1:30 PM, Mark Heckmann mark.heckm...@gmx.de wrote:
 Hello,

 using the - assignment operator I do not understand why the following does 
 not work.

 l - list()
 l
 list()
 l$arg1 - test
 error in l$arg1 - test : Objekt 'l' not found

 ?- says:  The operators - and - cause a search to made through the 
 environment for an existing definition of the variable being assigned. If 
 such a variable is found (and its binding is not locked) then its value is 
 redefined, otherwise assignment takes place in the global environment. 

 Still I do noch understand why the above does not work. The object l is in 
 the global environment. Can someone explain it to me?

Yes, the object 'l' is in the global environment, but 'l$arg1' is not,
cf. exists(l$arg1) and exists(l).  Instead, this works:

l - list();
l$args1 - test;

but much much much much much much much much much much much much much
much much much much much much much much much much much much much much
much much much much much much much much much much much better, do NOT
use - (or assign()/get()) unless you fully understand what you're
doing and have a very good reason for doing it, and that reason should
be able pass the embarrassment-test on public R mailing lists.  Got
the point?  Don't use it - there is another way to do what you want to
achieve - you just have to ask/find out how.

/H


 Thanks
 --Mark
 
 Mark Heckmann
 Blog: www.markheckmann.de
 R-Blog: http://ryouready.wordpress.com

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

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


Re: [R] assigment operator question

2012-04-07 Thread Mark Heckmann
Thanks! I'll try to stick to that advice!
Maybe there is a better way... Here is what I want:

I want to save some default settings for a package.
The user can change these using a function similar to par().
I do not want to use options() here as it will be quite a lot of parameters.
I was thinking about an invisible object in the global environment and using 
- or assign() to change the settings...

What other roads are there to go?

TIA
--Mark


Am 07.04.2012 um 22:51 schrieb Henrik Bengtsson:

 On Sat, Apr 7, 2012 at 1:30 PM, Mark Heckmann mark.heckm...@gmx.de wrote:
 Hello,
 
 using the - assignment operator I do not understand why the following does 
 not work.
 
 l - list()
 l
 list()
 l$arg1 - test
 error in l$arg1 - test : Objekt 'l' not found
 
 ?- says:  The operators - and - cause a search to made through the 
 environment for an existing definition of the variable being assigned. If 
 such a variable is found (and its binding is not locked) then its value is 
 redefined, otherwise assignment takes place in the global environment. 
 
 Still I do noch understand why the above does not work. The object l is in 
 the global environment. Can someone explain it to me?
 
 Yes, the object 'l' is in the global environment, but 'l$arg1' is not,
 cf. exists(l$arg1) and exists(l).  Instead, this works:
 
 l - list();
 l$args1 - test;
 
 but much much much much much much much much much much much much much
 much much much much much much much much much much much much much much
 much much much much much much much much much much much better, do NOT
 use - (or assign()/get()) unless you fully understand what you're
 doing and have a very good reason for doing it, and that reason should
 be able pass the embarrassment-test on public R mailing lists.  Got
 the point?  Don't use it - there is another way to do what you want to
 achieve - you just have to ask/find out how.
 
 /H
 
 
 Thanks
 --Mark
 
 Mark Heckmann
 Blog: www.markheckmann.de
 R-Blog: http://ryouready.wordpress.com
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


Mark Heckmann
Blog: www.markheckmann.de
R-Blog: http://ryouready.wordpress.com











[[alternative HTML version deleted]]

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


Re: [R] assigment operator question

2012-04-07 Thread Henrik Bengtsson
On Sat, Apr 7, 2012 at 2:01 PM, Mark Heckmann mark.heckm...@gmx.de wrote:
 Thanks! I'll try to stick to that advice!
 Maybe there is a better way... Here is what I want:

 I want to save some default settings for a package.
 The user can change these using a function similar to par().
 I do not want to use options() here as it will be quite a lot of parameters.
 I was thinking about an invisible object in the global environment and using 
 - or assign() to change the settings...

 What other roads are there to go?

It sounds like you have a use case where using a global variable is
acceptable, however options() is indeed a good alternative to host
your settings.  I'm not sure why you don't want to leverage the
options() functionality that is already in place?  Is it because you
don't want to clutter up the options() output?  You can always keep
all of your package's setting in a single entry, e.g.
options(myPkgOpts=myPkgOpts) where myPkgOpts is a list/tree (e.g. your
'l' below).  Then you can have your own wrapper functions for settings
and getting these options.  The advantage with this approach is that
the settings will sustain any attempts of cleaning up the global
environment, e.g. rm(list=ls(all.names=TRUE)).

FYI, this updated topic may be better suited for the R-devel list.

/Henrik


 TIA
 --Mark


 Am 07.04.2012 um 22:51 schrieb Henrik Bengtsson:

 On Sat, Apr 7, 2012 at 1:30 PM, Mark Heckmann mark.heckm...@gmx.de wrote:
 Hello,

 using the - assignment operator I do not understand why the following 
 does not work.

 l - list()
 l
 list()
 l$arg1 - test
 error in l$arg1 - test : Objekt 'l' not found

 ?- says:  The operators - and - cause a search to made through the 
 environment for an existing definition of the variable being assigned. If 
 such a variable is found (and its binding is not locked) then its value is 
 redefined, otherwise assignment takes place in the global environment. 

 Still I do noch understand why the above does not work. The object l is in 
 the global environment. Can someone explain it to me?

 Yes, the object 'l' is in the global environment, but 'l$arg1' is not,
 cf. exists(l$arg1) and exists(l).  Instead, this works:

 l - list();
 l$args1 - test;

 but much much much much much much much much much much much much much
 much much much much much much much much much much much much much much
 much much much much much much much much much much much better, do NOT
 use - (or assign()/get()) unless you fully understand what you're
 doing and have a very good reason for doing it, and that reason should
 be able pass the embarrassment-test on public R mailing lists.  Got
 the point?  Don't use it - there is another way to do what you want to
 achieve - you just have to ask/find out how.

 /H


 Thanks
 --Mark
 末末
 Mark Heckmann
 Blog: www.markheckmann.de
 R-Blog: http://ryouready.wordpress.com

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

 末末
 Mark Heckmann
 Blog: www.markheckmann.de
 R-Blog: http://ryouready.wordpress.com











        [[alternative HTML version deleted]]


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


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


Re: [R] assigment operator question

2012-04-07 Thread Gabor Grothendieck
On Sat, Apr 7, 2012 at 5:01 PM, Mark Heckmann mark.heckm...@gmx.de wrote:
 Thanks! I'll try to stick to that advice!
 Maybe there is a better way... Here is what I want:

 I want to save some default settings for a package.
 The user can change these using a function similar to par().
 I do not want to use options() here as it will be quite a lot of parameters.
 I was thinking about an invisible object in the global environment and using 
 - or assign() to change the settings...

 What other roads are there to go?


Another possibility is to store the global information in an
environment in your package's namespace.  See .LatticeEnv in the
lattice package's source code for an example.

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

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


Re: [R] assigment operator question

2012-04-07 Thread Duncan Murdoch

On 12-04-07 4:51 PM, Henrik Bengtsson wrote:

On Sat, Apr 7, 2012 at 1:30 PM, Mark Heckmannmark.heckm...@gmx.de  wrote:

Hello,

using the- assignment operator I do not understand why the following does not 
work.

l- list()
l
list()
l$arg1- test
error in l$arg1- test : Objekt 'l' not found

?- says:  The operators- and -  cause a search to made through the 
environment for an existing definition of the variable being assigned. If such a variable is found (and its binding 
is not locked) then its value is redefined, otherwise assignment takes place in the global environment. 

Still I do noch understand why the above does not work. The object l is in the 
global environment. Can someone explain it to me?


Yes, the object 'l' is in the global environment, but 'l$arg1' is not,
cf. exists(l$arg1) and exists(l).  Instead, this works:


I don't think that is the problem:  - normally works with complex 
assignments.  I think the problems are


 - Mark was working at top level in the console
 - The documentation isn't clear about what happens in that case.
 - R is inconsistent between simple and complex assignments with -.

The - operator is designed to be used in a function, and it makes an 
assignment in the environment of the function, not in the local 
evaluation frame.  If it can't find a variable there, it assigns into 
the global environment.  So if the l$arg1 - test was a line in a 
function, everything would have worked as expected.


When you use it at the top level, it tries looking in the parent of the 
global environment, its parent, and so on, back to the empty environment 
at the root of all the namespaces.  If you happened to use a name like 
mean which exists, it will try to assign to that object (and fail, 
because that binding is locked).


If you pick a name that doesn't exist, then R is inconsistent.  If 
you're doing a simple assignment like


l - 5

it will be done in the global environment, creating a new variable if 
necessary.  If you're doing a complex assignment like


l$arg1 - test

it will just fail, there's no global environment fallback.

I don't think this inconsistency is really worth fixing, since - is 
designed for use in functions.  There really isn't any use for it at the 
top level.


But maybe all of this should be documented.

Duncan Murdoch



l- list();
l$args1- test;

but much much much much much much much much much much much much much
much much much much much much much much much much much much much much
much much much much much much much much much much much better, do NOT
use - (or assign()/get()) unless you fully understand what you're
doing and have a very good reason for doing it, and that reason should
be able pass the embarrassment-test on public R mailing lists.  Got
the point?  Don't use it - there is another way to do what you want to
achieve - you just have to ask/find out how.

/H



Thanks
--Mark

Mark Heckmann
Blog: www.markheckmann.de
R-Blog: http://ryouready.wordpress.com

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


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


__
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] logical operator for different dimensions

2012-02-13 Thread Petr Savicky
On Mon, Feb 13, 2012 at 02:08:52AM -0800, uday wrote:
 
 I have some dataset 
 
 
  sci.pre - 0.300.380.500.650.801.031.331.72   
 2.22
   2.873.815.066.759.00   11.97   14.15   16.34   19.04
   22.27   25.49   29.72   34.67   40.47   47.29   55.29   64.67   75.6
88.50  103.50  121.10  141.70  165.80  194.00  227.00  265.00  308.00
 356.50  411.00  441.60  472.20  506.35  540.50  578.55  616.60  637.75
   658.90  680.05  701.20  724.65  748.10  771.55  795.00  820.95  846.90
   872.85  898.80  927.35  955.90  984.45 1013.00  ( length is 60)
 
 sci.avgkernal  - 0.300.380.500.650.801.031.33   
 1.722.22
 2.873.815.066.759.00   11.97   14.15   16.34   19.04
  22.27   25.49   29.72   34.67   40.47   47.29   55.29   64.67   75.65
  88.50  103.50  121.10  141.70  165.80  194.00  227.00  265.00  308.00
 356.50  411.00  441.60  472.20  506.35  540.50  578.55  616.60  637.75
 658.90  680.05  701.20  724.65  748.10  771.55  795.00  820.95  846.90
   872.85  898.80  927.35  955.90  984.45 1013.00 ( length is 60)
 
 pres.interptimes -  [,1]  [,2] [,3] [,4] [,5]
 [,6] [,7] [,8]
 [1,] 1016.1267 1005.9741 989.9127 970.0237 945.6067 880.5082 790.4647
 675.8315
 [2,]  875.6320  866.8767 853.0258 835.8741 814.8176 758.6784 681.0275
 582.1712
 [3,]  996.0351  986.0758 970.3201 950.8098 926.8576 862.9984 774.6692
 662.2184
 [4,]  996.0353  986.0760 970.3203 950.8100 926.8578 862.9987 774.6694
 662.2187
 [5,] 1008.0222  997.9431 981.9978 962.2527 938.0123 873.3847 783.9926
 670.1888
 [6,]  999.8343  989.8371 974.0214 954.4367 930.3932 866.2906 777.6247
 664.7453
  [,9][,10][,11][,12][,13][,14][,15][,16]
 [1,] 544.3248 410.3611 289.4130 237.2794 191.5622 152.3020 119.1842 91.68080
 [2,] 468.7636 353.2371 248.9350 203.9765 164.5513 130.6945 102.1346 78.41645
 [3,] 533.2153 401.8020 283.1566 232.0154 187.1686 148.6559 116.1686 89.18875
 [4,] 533.2155 401.8022 283.1568 232.0157 187.1688 148.6561 116.1688 89.18898
 [5,] 539.6334 406.6388 286.5657 234.8092 189.4227 150.4466 117.5683 90.26387
 [6,] 535.2505 403.3363 284.2386 232.9026 187.8848 149.2253 116.6141 89.53148
 [,17][,18][,19] [,20]
 [1,] 69.12170 50.81654 24.30808 0.8657024
 [2,] 58.96213 43.17629 20.31615 0.1001228
 [3,] 67.05913 49.10247 23.09867 0.1025881
 [4,] 67.05936 49.10270 23.09890 0.1028196
 [5,] 67.86797 49.69524 23.37854 0.1057444
 [6,] 67.31751 49.29240 23.18949 0.1057560 ( dim is 6 20) 
 
 sci.prediff   - diff(sci.pre) 
 sci.prediff   - c(sci.pre[1],sci.prediff) 
 sum(sci.avgkernal*sci.prediff )/sum(sci.prediff )
 pres.interptimes  - pres.interptime[,-20]#skip last level
 tm3.avgkernal -array(NA,c(length(1:nobs),19))
 for (k in 1:nobs){
for (h in 1:19){
 sel- sci.pre = pres.interptimes[k,h]  sci.pre 
 pres.interptimes[k,h+1]
 tm3.avgkernal[k,h] - sum((sci.avgkernal * sci.prediff)[sel]) /
 sum(sci.prediff[sel])
   }
}
 
 after running code I get error 
 
 Error: subscript out of bounds
 
 How to fix this error ?

I suspect that the error is generated at

  pres.interptimes[k,h+1]

since h+1 goes up to 20 and pres.interptimes was restricted to 19
columns in 

  pres.interptimes  - pres.interptime[,-20]#skip last level

(assuming that pres.interptime is a typo and is pres.interptimes
in fact).

Hope this helps.

Petr Savicky.

__
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] logical operator for different dimensions

2012-02-13 Thread uday
Hi  Petr,

You were correct ,  thats was the mistake . I am sorry for last reply. now
its working. 


Cheers 
Uday  

--
View this message in context: 
http://r.789695.n4.nabble.com/logical-operator-for-different-dimensions-tp4383316p4384143.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] overloading + operator for chars

2011-11-02 Thread Martin Morgan

On 11/02/2011 06:52 AM, Albert-Jan Roskam wrote:

Hello,

I would like to overload the + operator so that it can be used to concatenate two strings, e.g 
John + Doe = JohnDoe.
How can I 'unseal' the + method?

setMethod(+, signature(e1=character, e2=character), function(e1, e2) paste(e1, e2, 
sep=) )

Error in setMethod(+, signature(e1 = character, e2 = character),  :
   the method for function + and signature e1=character, e2=character is 
sealed and cannot be re-defined






Hi -- I think the two issues are that + is part of the Arith group 
generic (?Methods, ?Arith) and that `+` (actually, members of the Ops 
group) for primitive types dispatches directly without doing method 
look-up. Personally I might


setClass(Character, contains=character)

Character - function(...) new(Character, ...)

setMethod(Arith, c(Character, Character), function(e1, e2) {
switch(.Generic,
   +=Character(paste(e1, e2, sep=)),
   stop(unhandled 'Arith' operator ', .Generic, '))
})

and then

 Character(c(foo, bar)) + Character(baz)
[1] foobaz barbaz

Some might point to

 `%+%` - function(e1, e2) paste(e1, e2, sep=)
 foo %+% bar
[1] foobar

Martin


Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?
~~
[[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.



--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

__
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] assignment operator - does not work in a function...

2011-10-31 Thread R. Michael Weylandt
It's a matter of environments:

your function makes veri for purposes of the function but then
throws it away when the function is done. If you want it to be
accessible you need to assign it to the global environment with
something like

assign(veri, read.transactions(c:/RVerileri/BitirmeVeri.csv,sep=;,col=1)
, envir = .GlobalEnv)

Michael

On Mon, Oct 31, 2011 at 9:30 AM, Levent TERLEMEZ
lterle...@anadolu.edu.tr wrote:
 Dear Users,

 I have a little problem with assignment operator. It's working from command 
 prompt but does not work same as in the command prompt in the function. Am I 
 missing or forgetting something? The function is (tried on both 2.13.1 and 
 2.13.2) as below using arules and arulseViz packages:

kuralfonk
 function()
 {
        require(arules)
        require(arulesViz)
        veri-read.transactions(c:/RVerileri/BitirmeVeri.csv,sep=;,col=1)  
  #This statement does not add veri object to working area
        #itemFrequencyPlot(veri,type=absolute)
        #bringToTop(-1)
        summary(veri) #But this statement gives the right answer
        itemFrequency(veri,type=absolute) #And this statement does nothing, 
 even the graph window does not open.
 }

 Thanks for your tips and corrections,
 Levent.

        [[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] assignment operator - does not work in a function...

2011-10-31 Thread Mehmet Suzen
Are you running the function with Rscript or R CMD? If yes, try it 
interactively. Or, try to run them separate functions.


-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org]
On Behalf Of Levent TERLEMEZ
Sent: 31 October 2011 13:31
To: r-help@r-project.org
Subject: [R] assignment operator - does not work in a function...

Dear Users,

I have a little problem with assignment operator. It's working from
command prompt but does not work same as in the command prompt in the
function. Am I missing or forgetting something? The function is (tried
on both 2.13.1 and 2.13.2) as below using arules and arulseViz
packages:

kuralfonk
function()
{
require(arules)
require(arulesViz)
veri-
read.transactions(c:/RVerileri/BitirmeVeri.csv,sep=;,col=1)   #This
statement does not add veri object to working area
#itemFrequencyPlot(veri,type=absolute)
#bringToTop(-1)
summary(veri) #But this statement gives the right answer
itemFrequency(veri,type=absolute) #And this statement does
nothing, even the graph window does not open.
}

Thanks for your tips and corrections,
Levent.

   [[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.
LEGAL NOTICE
This message is intended for the use o...{{dropped:10}}

__
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] assignment operator - does not work in a function...

2011-10-31 Thread Bert Gunter
 assignment operator - does not work in a function...

Of course not! Read the docs (e.g. An Introduction to R, especially the
part on writing functions)

Also:

?-

-- Bert

On Mon, Oct 31, 2011 at 9:22 AM, Mehmet Suzen msu...@mango-solutions.comwrote:

 Are you running the function with Rscript or R CMD? If yes, try it
 interactively. Or, try to run them separate functions.


 -Original Message-
 From: r-help-boun...@r-project.org
 [mailto:r-help-boun...@r-project.org]
 On Behalf Of Levent TERLEMEZ
 Sent: 31 October 2011 13:31
 To: r-help@r-project.org
 Subject: [R] assignment operator - does not work in a function...
 
 Dear Users,
 
 I have a little problem with assignment operator. It's working from
 command prompt but does not work same as in the command prompt in the
 function. Am I missing or forgetting something? The function is (tried
 on both 2.13.1 and 2.13.2) as below using arules and arulseViz
 packages:
 
 kuralfonk
 function()
 {
 require(arules)
 require(arulesViz)
 veri-
 read.transactions(c:/RVerileri/BitirmeVeri.csv,sep=;,col=1)   #This
 statement does not add veri object to working area
 #itemFrequencyPlot(veri,type=absolute)
 #bringToTop(-1)
 summary(veri) #But this statement gives the right answer
 itemFrequency(veri,type=absolute) #And this statement does
 nothing, even the graph window does not open.
 }
 
 Thanks for your tips and corrections,
 Levent.
 
[[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.
 LEGAL NOTICE
 This message is intended for the use o...{{dropped:10}}

 __
 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

[[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] %in% operator - NOT IN

2011-05-09 Thread Karl Ove Hufthammer
Dan Abner wrote:

 I am attempting to use the %in% operator with the ! to produce a NOT IN
 type of operation.

Just use the ‘%nin‰’ operator in the ‘Hmisc’ package. :-)

-- 
Karl Ove Hufthammer

__
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] %in% operator - NOT IN

2011-05-08 Thread Berwin A Turlach
G'day Dan,

On Sun, 8 May 2011 05:06:27 -0400
Dan Abner dan.abne...@gmail.com wrote:

 Hello everyone,
 
 I am attempting to use the %in% operator with the ! to produce a NOT
 IN type of operation. Why does this not work? Suggestions?
 
  data2[data1$char1 %in% c(string1,string2),1]-min(data1$x1)
  data2[data1$char1 ! %in%
  c(string1,string2),1]-max(data1$x1)+1000
 
 Error: unexpected '!' in data2[data1$char1 !

Try (untested)

R data2[!(data1$char1 %in% c(string1,string2)),1]-max(data1$x1)+1000 

HTH.

Cheers,

Berwin

__
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] %in% operator - NOT IN

2011-05-08 Thread baptiste auguie
Hi,

On 8 May 2011 21:18, Berwin A Turlach berwin.turl...@gmail.com wrote:
 G'day Dan,

 On Sun, 8 May 2011 05:06:27 -0400
 Dan Abner dan.abne...@gmail.com wrote:

 Hello everyone,

 I am attempting to use the %in% operator with the ! to produce a NOT
 IN type of operation. Why does this not work? Suggestions?

Alternatively,

example(`%in%`)

or

`%ni%` = Negate(`%in%`)

HTH,

baptiste


  data2[data1$char1 %in% c(string1,string2),1]-min(data1$x1)
  data2[data1$char1 ! %in%
  c(string1,string2),1]-max(data1$x1)+1000

 Error: unexpected '!' in data2[data1$char1 !

 Try (untested)

 R data2[!(data1$char1 %in% c(string1,string2)),1]-max(data1$x1)+1000

 HTH.

 Cheers,

        Berwin

 __
 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] %in% operator - NOT IN

2011-05-08 Thread Ted Harding
On 08-May-11 09:18:55, Berwin A Turlach wrote:
 G'day Dan,
 
 On Sun, 8 May 2011 05:06:27 -0400
 Dan Abner dan.abne...@gmail.com wrote:
 
 Hello everyone,
 I am attempting to use the %in% operator with the ! to produce
 a NOT IN type of operation. Why does this not work? Suggestions?
 
  data2[data1$char1 %in% c(string1,string2),1]-min(data1$x1)
  data2[data1$char1 ! %in%
  c(string1,string2),1]-max(data1$x1)+1000
 
 Error: unexpected '!' in data2[data1$char1 !
 
 Try (untested)
 
 R data2[!(data1$char1 %in%
 c(string1,string2)),1]-max(data1$x1)+1000 
 
 HTH.
 Cheers,
   Berwin

Berwin's suggestion should work -- it is the general way to
negate the result of an %in.

As to Why does this not work?, the point to note is that
%in% is a binary operator. If you enter
  ?%in%
you will be taken to the help page for match, where it is
pointed out that:

  ?%in%? is a more intuitive interface as a binary operator,
  which returns a logical vector indicating if there is a
  match or not for its left operand.

Specifically, therefore, the syntax of %in% requires

  X %in% Y

where X and Y are objects to which the functional definition
of %in% applies (see the same help page):

  '%in%' is currently defined as
  '%in% - function(x, table) match(x, table, nomatch = 0)  0'

In your expression (effectively X ! %in% Y) the item which
immediately precedes %in% is the !, and this is not a
valid item!

Based on the above functional definition, you could define
your own binary operator %!in% as

%!in% - function(x,table) match(x,table, nomatch = 0) == 0

or similar -- I have not tested this so cannot guarantee it!
However, it is the way to proceed if you want a NOT IN.
Then the usage could be:

data2[data1$char1 %!in% c(string1,string2),1]-max(data1$x1)+1000

Hoping ths helps,
Ted.


E-Mail: (Ted Harding) ted.hard...@wlandres.net
Fax-to-email: +44 (0)870 094 0861
Date: 08-May-11   Time: 10:35:05
-- XFMail --

__
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] %in% operator - NOT IN

2011-05-08 Thread Duncan Mackay


At 19:06 08/05/2011, you wrote:

Hello everyone,

I am attempting to use the %in% operator with the ! to produce a NOT IN type
of operation. Why does this not work? Suggestions?

 data2[data1$char1 %in% c(string1,string2),1]-min(data1$x1)
 data2[data1$char1 ! %in% c(string1,string2),1]-max(data1$x1)+1000

Error: unexpected '!' in data2[data1$char1 !


Thanks!

Dan



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


Hi Dan

See the last of the examples for ?match

%w/o% - function(x, y) x[!x %in% y] #--  x without y
(1:10) %w/o% c(3,7,12)

I think it was Peter Dalgaard who pointed this out some years ago

HTH

Regards


Duncan Mackay
Department of Agronomy and Soil Science
University of New England
ARMIDALE NSW 2351
Email: home mac...@northnet.com.au

__
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] assignment operator saving factor level as number

2010-11-05 Thread Erik Iverson

Could you give a small reproducible example please?
It is not clear to me what your looping structure is
doing, or what your goal here is.

There may be a much simpler method than introducing
subscripts.

--Erik

Wade Wall wrote:

Hi all,

I have a dataframe (df1) that I am trying to select values from to a second
dataframe that at the current time is only for the selected items from df1
(df2).  The values that I am trying to save from df1 are factors with
alphanumeric names

df1 looks like this:

'data.frame':   3014 obs. of  13 variables:
 $ Num : int  1 1 1 2 2 2 3 3 3 4 ...
 $ Tag_Num : int  1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ...
 $ Site: Factor w/ 25 levels PYBR002A,PYBR003B,..: 1 1 1 1 1 1 1
1 1 1 ...
 $ Site_IndNum : Factor w/ 1044 levels PYBR002A_001,..: 1 1 1 2 2 2 3 3 3
4 ...
  ...
 $ Area: num  463.3 29.5 101.8 152.9 34.6 ...

However, whenever I try to assign values, like this

df2[j,1]-df2$Site[i]

the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g.
1).

Does anyone know why this is happening and how I can assign the actual
values from df1 to df2?

Thanks in advance,

Wade

[[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] assignment operator saving factor level as number

2010-11-05 Thread jim holtman
Your example looks like you are assigning back to the first column of
df2 (Num).  Is this what you are really doing in your code?

You need to follow the posting guide:

PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall wade.w...@gmail.com wrote:
 Hi all,

 I have a dataframe (df1) that I am trying to select values from to a second
 dataframe that at the current time is only for the selected items from df1
 (df2).  The values that I am trying to save from df1 are factors with
 alphanumeric names

 df1 looks like this:

 'data.frame':   3014 obs. of  13 variables:
  $ Num         : int  1 1 1 2 2 2 3 3 3 4 ...
  $ Tag_Num     : int  1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ...
  $ Site        : Factor w/ 25 levels PYBR002A,PYBR003B,..: 1 1 1 1 1 1 1
 1 1 1 ...
  $ Site_IndNum : Factor w/ 1044 levels PYBR002A_001,..: 1 1 1 2 2 2 3 3 3
 4 ...
   ...
  $ Area        : num  463.3 29.5 101.8 152.9 34.6 ...

 However, whenever I try to assign values, like this

 df2[j,1]-df2$Site[i]

 the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g.
 1).

 Does anyone know why this is happening and how I can assign the actual
 values from df1 to df2?

 Thanks in advance,

 Wade

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




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] assignment operator saving factor level as number

2010-11-05 Thread Jeffrey Spies
Perhaps this will help:

 test1 - test2 - data.frame(col1=factor(c(1:3), labels=c(a, b, c)))
 test3 - data.frame(col1 = 1:3)

Now:

 test2[2,1] - test1$col1[1]
 test2$col1
[1] a a c
Levels: a b c

vs

 test3[2,1] - test1$col1[1]
 test3$col1
[1] 1 1 3

Because test3's first column, col1, is a vector of numeric, and each
element of a vector must have the same data type (numeric, factor,
etc), it will coerce the data coming in to have the same data type (if
it can).  In this case, the data type is numeric.  Had it been a
character coming in, because it can't coerce a character to a numeric,
it would have made the entire vector a vector of characters:

 test3[2,1] - 'b'
 test3$col1
[1] 1 b 3

Hope that demonstrates what's probably going on,

Jeff.

On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall wade.w...@gmail.com wrote:
 Hi all,

 I have a dataframe (df1) that I am trying to select values from to a second
 dataframe that at the current time is only for the selected items from df1
 (df2).  The values that I am trying to save from df1 are factors with
 alphanumeric names

 df1 looks like this:

 'data.frame':   3014 obs. of  13 variables:
  $ Num         : int  1 1 1 2 2 2 3 3 3 4 ...
  $ Tag_Num     : int  1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ...
  $ Site        : Factor w/ 25 levels PYBR002A,PYBR003B,..: 1 1 1 1 1 1 1
 1 1 1 ...
  $ Site_IndNum : Factor w/ 1044 levels PYBR002A_001,..: 1 1 1 2 2 2 3 3 3
 4 ...
   ...
  $ Area        : num  463.3 29.5 101.8 152.9 34.6 ...

 However, whenever I try to assign values, like this

 df2[j,1]-df2$Site[i]

 the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g.
 1).

 Does anyone know why this is happening and how I can assign the actual
 values from df1 to df2?

 Thanks in advance,

 Wade

        [[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] assignment operator saving factor level as number

2010-11-05 Thread Jorge Ivan Velez
Hi Wade,

Try (untested):

df2[j,1] - as.character(f2$Site)[i]

If that does not work, which is very likely, could you please provide
commented, minimal, self-contained, reproducible code?

HTH,
Jorge


On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall  wrote:

 Hi all,

 I have a dataframe (df1) that I am trying to select values from to a second
 dataframe that at the current time is only for the selected items from df1
 (df2).  The values that I am trying to save from df1 are factors with
 alphanumeric names

 df1 looks like this:

 'data.frame':   3014 obs. of  13 variables:
  $ Num : int  1 1 1 2 2 2 3 3 3 4 ...
  $ Tag_Num : int  1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ...
  $ Site: Factor w/ 25 levels PYBR002A,PYBR003B,..: 1 1 1 1 1 1
 1
 1 1 1 ...
  $ Site_IndNum : Factor w/ 1044 levels PYBR002A_001,..: 1 1 1 2 2 2 3 3 3
 4 ...
   ...
  $ Area: num  463.3 29.5 101.8 152.9 34.6 ...

 However, whenever I try to assign values, like this

 df2[j,1]-df2$Site[i]

 the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g.
 1).

 Does anyone know why this is happening and how I can assign the actual
 values from df1 to df2?

 Thanks in advance,

 Wade

[[alternative HTML version deleted]]

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


[[alternative HTML version deleted]]

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


Re: [R] assignment operator saving factor level as number

2010-11-05 Thread Wade Wall
Hi all,

Thanks for the help.  Jeffrey was right; my initial dataframe did not have
the columns defined for factors.  I solved it using Jorge's example of using
as.character.

Sorry for not being more clear before.

Wade

On Fri, Nov 5, 2010 at 4:12 PM, Jeffrey Spies jsp...@virginia.edu wrote:

 Perhaps this will help:

  test1 - test2 - data.frame(col1=factor(c(1:3), labels=c(a, b,
 c)))
  test3 - data.frame(col1 = 1:3)

 Now:

  test2[2,1] - test1$col1[1]
  test2$col1
 [1] a a c
 Levels: a b c

 vs

  test3[2,1] - test1$col1[1]
  test3$col1
 [1] 1 1 3

 Because test3's first column, col1, is a vector of numeric, and each
 element of a vector must have the same data type (numeric, factor,
 etc), it will coerce the data coming in to have the same data type (if
 it can).  In this case, the data type is numeric.  Had it been a
 character coming in, because it can't coerce a character to a numeric,
 it would have made the entire vector a vector of characters:

  test3[2,1] - 'b'
  test3$col1
 [1] 1 b 3

 Hope that demonstrates what's probably going on,

 Jeff.

 On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall wade.w...@gmail.com wrote:
  Hi all,
 
  I have a dataframe (df1) that I am trying to select values from to a
 second
  dataframe that at the current time is only for the selected items from
 df1
  (df2).  The values that I am trying to save from df1 are factors with
  alphanumeric names
 
  df1 looks like this:
 
  'data.frame':   3014 obs. of  13 variables:
   $ Num : int  1 1 1 2 2 2 3 3 3 4 ...
   $ Tag_Num : int  1195 1195 1195 1162 1162 1162 1106 1106 1106 1173
 ...
   $ Site: Factor w/ 25 levels PYBR002A,PYBR003B,..: 1 1 1 1 1
 1 1
  1 1 1 ...
   $ Site_IndNum : Factor w/ 1044 levels PYBR002A_001,..: 1 1 1 2 2 2 3 3
 3
  4 ...
    ...
   $ Area: num  463.3 29.5 101.8 152.9 34.6 ...
 
  However, whenever I try to assign values, like this
 
  df2[j,1]-df2$Site[i]
 
  the values are changed from alphanumeric (e.g. PYBR003A) to numerals
 (e.g.
  1).
 
  Does anyone know why this is happening and how I can assign the actual
  values from df1 to df2?
 
  Thanks in advance,
 
  Wade
 
 [[alternative HTML version deleted]]
 
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
 


[[alternative HTML version deleted]]

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


Re: [R] assignment operator saving factor level as number

2010-11-05 Thread Jeffrey Spies
Glad you figured it out, but just be aware that if you set one value
of the column to be a character, it will make the whole vector
characters.  This could cause issues for analysis if you need numerics
or factors.  If the column is supposed to be a factor to begin with,
set it to be so; if you have two data frames, one with a column of
factors (dat2) and one with what should be a column of factors (dat1),
you can use something like this:

dat1$columnThatShouldBeFactors - as.factor(
dat1$columnThatShouldBeFactors,
levels=levels(dat2$columnThatIsAlreadyFactors)
)

Cheers,

Jeff.

On Fri, Nov 5, 2010 at 6:03 PM, Wade Wall wade.w...@gmail.com wrote:
 Hi all,

 Thanks for the help.  Jeffrey was right; my initial dataframe did not have
 the columns defined for factors.  I solved it using Jorge's example of using
 as.character.

 Sorry for not being more clear before.

 Wade

 On Fri, Nov 5, 2010 at 4:12 PM, Jeffrey Spies jsp...@virginia.edu wrote:

 Perhaps this will help:

  test1 - test2 - data.frame(col1=factor(c(1:3), labels=c(a, b,
  c)))
  test3 - data.frame(col1 = 1:3)

 Now:

  test2[2,1] - test1$col1[1]
  test2$col1
 [1] a a c
 Levels: a b c

 vs

  test3[2,1] - test1$col1[1]
  test3$col1
 [1] 1 1 3

 Because test3's first column, col1, is a vector of numeric, and each
 element of a vector must have the same data type (numeric, factor,
 etc), it will coerce the data coming in to have the same data type (if
 it can).  In this case, the data type is numeric.  Had it been a
 character coming in, because it can't coerce a character to a numeric,
 it would have made the entire vector a vector of characters:

  test3[2,1] - 'b'
  test3$col1
 [1] 1 b 3

 Hope that demonstrates what's probably going on,

 Jeff.

 On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall wade.w...@gmail.com wrote:
  Hi all,
 
  I have a dataframe (df1) that I am trying to select values from to a
  second
  dataframe that at the current time is only for the selected items from
  df1
  (df2).  The values that I am trying to save from df1 are factors with
  alphanumeric names
 
  df1 looks like this:
 
  'data.frame':   3014 obs. of  13 variables:
   $ Num         : int  1 1 1 2 2 2 3 3 3 4 ...
   $ Tag_Num     : int  1195 1195 1195 1162 1162 1162 1106 1106 1106 1173
  ...
   $ Site        : Factor w/ 25 levels PYBR002A,PYBR003B,..: 1 1 1 1 1
  1 1
  1 1 1 ...
   $ Site_IndNum : Factor w/ 1044 levels PYBR002A_001,..: 1 1 1 2 2 2 3
  3 3
  4 ...
    ...
   $ Area        : num  463.3 29.5 101.8 152.9 34.6 ...
 
  However, whenever I try to assign values, like this
 
  df2[j,1]-df2$Site[i]
 
  the values are changed from alphanumeric (e.g. PYBR003A) to numerals
  (e.g.
  1).
 
  Does anyone know why this is happening and how I can assign the actual
  values from df1 to df2?
 
  Thanks in advance,
 
  Wade
 
         [[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] Logical operator fails to recognize vector element

2010-10-06 Thread jim holtman
FAQ 7.31

On Wed, Oct 6, 2010 at 12:02 PM, D. Alain dialva...@yahoo.de wrote:
 Hi experts,

 I have encountered a strange phenomenon after loading spreadsheet data via

example-read.csv(M:\\...\\mydata.csv,header=T)

 My data consist of two variables x and y which I have combined to a single 
 vector using

z-rowSums(cbind(example$x,example$y))
z
 [1]  9.79  9.79 17.54 12.59  2.18  9.79         #vector z with 6 elements

 Now I want to identify all elements equal to 9.79, so I enter

which(z==9.79)

 And I get the result

 [1] 2 6                                                                 #R 
 failes to identify the fist element!

 Taking a futher look at the elements I get the following:

 mode(z)
 [1] numeric
 mode(z[1])
 [1] numeric
 z[1]+z[2]                                                             #I can 
 add first and second element
 [1] 19.58

 But if I try to apply logical operators I get:

 z[1]==9.79
 [1] FALSE
 z[2]==9.79
 [1] TRUE
 z[1]==z[2]
 [1] FALSE

 When I reenter the first element via
 z[1]-9.79
 z[1]==9.79
 [1] TRUE

 What is wrong with my data? Can anyone help?

 version
               _
 platform         i386-pc-mingw32
 arch              i386
 os                 mingw32
 system          i386, mingw32
 status
 major             2
 minor            11.1
 year              2010
 month           05
 day               31
 svn rev           52157
 language        R
 version.string  R version 2.11.1 (2010-05-31)

 Thanks
 D. Alain



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




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] Logical operator fails to recognize vector element

2010-10-06 Thread Duncan Murdoch

 See FAQ 7.31.

Duncan Murdoch

On 06/10/2010 12:02 PM, D. Alain wrote:

Hi experts,

I have encountered a strange phenomenon after loading spreadsheet data via

example-read.csv(M:\\...\\mydata.csv,header=T)

My data consist of two variables x and y which I have combined to a single 
vector using

z-rowSums(cbind(example$x,example$y))
z
[1]  9.79  9.79 17.54 12.59  2.18  9.79 #vector z with 6 elements

Now I want to identify all elements equal to 9.79, so I enter

which(z==9.79)

And I get the result

[1] 2 6 #R 
failes to identify the fist element!

Taking a futher look at the elements I get the following:

  mode(z)
[1] numeric
  mode(z[1])
[1] numeric
  z[1]+z[2] #I can add 
first and second element
[1] 19.58

But if I try to apply logical operators I get:

  z[1]==9.79
[1] FALSE
  z[2]==9.79
[1] TRUE
  z[1]==z[2]
[1] FALSE

When I reenter the first element via
  z[1]-9.79
  z[1]==9.79
[1] TRUE

What is wrong with my data? Can anyone help?

  version
_
platform i386-pc-mingw32
arch  i386
os mingw32
system  i386, mingw32
status
major 2
minor11.1
year  2010
month   05
day   31
svn rev   52157
languageR
version.string  R version 2.11.1 (2010-05-31)

Thanks
D. Alain



__
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] Logical operator fails to recognize vector element

2010-10-06 Thread Phil Spector

Here's a simple example that might be instructive:


x = 9.790001
y = 9.79
x

[1] 9.79

y

[1] 9.79

x == y

[1] FALSE

abs(x - y)  1e-8

[1] TRUE

all.equal(x,y)

[1] TRUE

And as others have said, FAQ 7.31 .

- Phil Spector
 Statistical Computing Facility
 Department of Statistics
 UC Berkeley
 spec...@stat.berkeley.edu




On Wed, Oct 6, 2010 at 12:02 PM, D. Alain dialva...@yahoo.de wrote:

Hi experts,

I have encountered a strange phenomenon after loading spreadsheet data via


example-read.csv(M:\\...\\mydata.csv,header=T)


My data consist of two variables x and y which I have combined to a single 
vector using


z-rowSums(cbind(example$x,example$y))
z

[1]  9.79  9.79 17.54 12.59  2.18  9.79         #vector z with 6 elements

Now I want to identify all elements equal to 9.79, so I enter


which(z==9.79)


And I get the result

[1] 2 6                                                                 #R 
failes to identify the fist element!

Taking a futher look at the elements I get the following:


mode(z)

[1] numeric

mode(z[1])

[1] numeric

z[1]+z[2]                                                             #I can 
add first and second element

[1] 19.58

But if I try to apply logical operators I get:


z[1]==9.79

[1] FALSE

z[2]==9.79

[1] TRUE

z[1]==z[2]

[1] FALSE

When I reenter the first element via

z[1]-9.79
z[1]==9.79

[1] TRUE

What is wrong with my data? Can anyone help?


version

              _
platform         i386-pc-mingw32
arch              i386
os                 mingw32
system          i386, mingw32
status
major             2
minor            11.1
year              2010
month           05
day               31
svn rev           52157
language        R
version.string  R version 2.11.1 (2010-05-31)

Thanks
D. Alain



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





--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] = vs - operator

2010-07-26 Thread jim holtman
Check the mail archieve on this;  there has been a long discussion.

To avoid trouble in the future, use - as the assignment operator.

On Mon, Jul 26, 2010 at 9:51 AM, Alaios ala...@yahoo.com wrote:
 Hello
 I notice that in Linux the = operator works like the - operator
 So a=3 is similar to a-3.
 Could you please verify me that is correct? I would like to use = operator. 
 Do
 you think that might be a problem in the future?

 Best Regards
 Alex

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




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] = vs - operator

2010-07-26 Thread Joshua Wiley
Hi,

Just as an example, here are three threads that discuss it.

http://www.mail-archive.com/r-help@r-project.org/msg16881.html

http://r.789695.n4.nabble.com/advice-opinion-on-vs-in-teaching-R-td1014502.html#a1014502

http://www.mail-archive.com/r-help@r-project.org/msg100034.html

Cheers,

Josh

On Mon, Jul 26, 2010 at 6:51 AM, Alaios ala...@yahoo.com wrote:
 Hello
 I notice that in Linux the = operator works like the - operator
 So a=3 is similar to a-3.
 Could you please verify me that is correct? I would like to use = operator. 
 Do
 you think that might be a problem in the future?

 Best Regards
 Alex

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




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

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


Re: [R] wildcard operator

2010-05-27 Thread Dennis Murphy
Hi:

On the off chance that your 9860 explanatory variables do not completely
take up the 'non-y'
part of your data frame, you can always write a formula string to pass to
the model
function. For example,
form - paste('y ~ ', paste('x', 1:10, sep = '', collapse = ' + '))
 form
[1] y ~  x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10
m - lm(form, data = df)

HTH,
Dennis


On Wed, May 26, 2010 at 9:58 PM, Eric Fail e...@it.dk wrote:

 Hi Ruser

 As so usual I'm trying to replicate some SAS code. I wold like to know if
 there is a wildcard operators, as   :  in SAS, in R?

 When running:

 lm(y ~ x1 + x2 + x3 + x4 + x5 + x6  x9860, data=mydata)

 I would like to be able to get around it by just writing something like
 this:

 lm(y ~ x1:x9860, data=mydata)

 Anyone?

 Sorry for no including a working example, but I figured that it wasn't
 necessary.

 Thanks

 Eric


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


[[alternative HTML version deleted]]

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


Re: [R] wildcard operator

2010-05-26 Thread Jorge Ivan Velez
Hi Eric,

Try

lm(y ~ . , data = mydata)

HTH,
Jorge


On Thu, May 27, 2010 at 12:58 AM, Eric Fail  wrote:

 Hi Ruser

 As so usual I'm trying to replicate some SAS code. I wold like to know if
 there is a wildcard operators, as   :  in SAS, in R?

 When running:

 lm(y ~ x1 + x2 + x3 + x4 + x5 + x6  x9860, data=mydata)

 I would like to be able to get around it by just writing something like
 this:

 lm(y ~ x1:x9860, data=mydata)

 Anyone?

 Sorry for no including a working example, but I figured that it wasn't
 necessary.

 Thanks

 Eric

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


[[alternative HTML version deleted]]

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


Re: [R] wildcard operator

2010-05-26 Thread Erik Iverson

Eric Fail wrote:

Hi Ruser

As so usual I'm trying to replicate some SAS code. I wold like to know 
if there is a wildcard operators, as   :  in SAS, in R?


When running:

lm(y ~ x1 + x2 + x3 + x4 + x5 + x6  x9860, data=mydata)

I would like to be able to get around it by just writing something like 
this:


lm(y ~ x1:x9860, data=mydata)



See ?formula, specifically:

 There are two special interpretations of ‘.’ in a formula.  The
 usual one is in the context of a ‘data’ argument of model fitting
 functions and means ‘all columns not otherwise in the formula’:
 see ‘terms.formula’.  In the context of ‘update.formula’, *only*,
 it means ‘what was previously in this part of the formula’.


So assuming all those RHS variables + y make up your data.frame

lm(y ~ ., data = mydata) would be fine.  You can easily create such a data.frame 
if you don't already have it using regexs.


--Erik

__
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] Odp: ^ operator

2009-11-16 Thread carol white
but with complex, I get complex numbers for the first and last elements:

 (as.complex(tmp))^(1/3)
[1] 0.01969170+0.03410703i 0.03478442+0.i 0.03285672+0.i
[4] 0.08950802+0.i 0.05848363+0.10129661i

whereas for the first element, we get the followings.

Moreover, 
 -6.108576e-05^(1/3)
  [1] -0.03938341
and
 -(6.108576e-05^(1/3))
[1] -0.03938341
and
 -((6.108576e-05)^(1/3))
[1] -0.03938341

give the same results.

so using () doesn't preserve any thing

--- On Mon, 11/16/09, Petr PIKAL petr.pi...@precheza.cz wrote:

 From: Petr PIKAL petr.pi...@precheza.cz
 Subject: Odp: [R] ^ operator
 To: carol white wht_...@yahoo.com
 Cc: r-h...@stat.math.ethz.ch
 Date: Monday, November 16, 2009, 3:40 AM
 Hi
 
 AFAIK, this is issue of the preference of operators. 
 
 r-help-boun...@r-project.org
 napsal dne 16.11.2009 11:24:59:
 
  Hi,
  I want to apply ^ operator to a vector but it is
 applied to some of the 
  elements correctly and to some others, it generates
 NaN. Why is it not 
 able to
  calculate -6.108576e-05^(1/3) even though it exists?
  
  
   tmp
  [1] -6.108576e-05  4.208762e-05 
 3.547092e-05  7.171101e-04 
 -1.600269e-03
   tmp^(1/3)
  [1]        NaN 0.03478442
 0.03285672 0.08950802        NaN
 
 This computes (-a)^(1/3) which is not possible in real
 numbers. You have 
 to use as.complex(tmp)^(1/3) to get a result.
 
   -6.108576e-05^(1/3)
  [1] -0.03938341
 
 this is actually
 -(6.108576e-05^(1/3))
 
 Regards
 Petr
 
 
  
  __
  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] Odp: ^ operator

2009-11-16 Thread Ted Harding

On 16-Nov-09 11:40:29, Petr PIKAL wrote:
 Hi
 AFAIK, this is issue of the preference of operators. 
 
 r-help-boun...@r-project.org napsal dne 16.11.2009 11:24:59:
 

Not in this case (see below), though of course in general - takes
precedence over ^, so, for example, in the expression

  -2^(1/3)

the - is applied first, giving (-2); and then ^ is applied
next, giving (-2)^(1/3). There is a work-round (see below).

 Hi,
 I want to apply ^ operator to a vector but it is applied to
 some of the elements correctly and to some others, it generates
 NaN. Why is it not able to calculate -6.108576e-05^(1/3) even
 though it exists?

It only exists (in the real domain) if ^ takes precedence over -
which (in R) it does not!

  tmp
 [1] -6.108576e-05  4.208762e-05  3.547092e-05  7.171101e-04 
 -1.600269e-03
  tmp^(1/3)
 [1]NaN 0.03478442 0.03285672 0.08950802NaN
 
 This computes (-a)^(1/3) which is not possible in real numbers.

In this example, that is not accurate. tmp has already been
defined, and contains numbers which are already stored as negative
numbers, so - is no longer on the scene as an operator, before
^ is applied; the issue of precedence of - over ^ is no
longer present. The NaN arises from x^(1/3) where x is negative.

 You have to use as.complex(tmp)^(1/3) to get a result.
 
  -6.108576e-05^(1/3)
 [1] -0.03938341

This is not the result I get:

  as.complex(tmp)^(1/3)
# [1] 0.01969171+0.03410703i 0.03478442+0.i
# [3] 0.03285672+0.i 0.08950802+0.i
# [5] 0.05848363+0.10129662i

 this is actually
 -(6.108576e-05^(1/3))
 
 Regards
 Petr

It is possible to work round the problem without using as.complex
which can introduce complications -- see above, and also:

  x - (-1)
  x^(1/3)
  # [1] NaN
  as.complex(x)^(1/3)
  # [1] 0.5+0.8660254i

  as.complex(-1)^(1/2)
  # [1] 0+1i

which you would not want if you are working throughout in real
numbers (you would want the result -1 instead). Although, in the
mathematics of complex numbers, (-1)^(1/3) has three values, one
of which is -1, R only returns a single value.

However, you would have to define a new operator, called say %^%:

  %^%-function(X,x){sign(X)*(abs(X)^x)}

  tmp - c(-6.108576e-05, 4.208762e-05, 3.547092e-05,
7.171101e-04, -1.600269e-03)
  tmp%^%(1/3)
  # [1] -0.03938341  0.03478442  0.03285672  0.08950802 -0.11696726

The definition of %^% forces ^ to take precedence over -,
by in effect removing - from the scene until ^ has done its
work. But, if you hope to rely on this, note that if you apply
to 'tmp' any function in which the ordinary ^ will be used on
a negative number, you will still have the same problem.

Note: Trying to redefine ^ will not work, since invoking the
result initiates an infinite recursion:

  ^- function(X,x){sign(X)*(abs(X)^x)}
  ## (This definition will be accepted by R)
  tmp%^%(1/3)
  # Error: evaluation nested too deeply: infinite recursion /
  #   options(expressions=)?

It's not a clean situatio, but I hope the above helps!
Ted.


E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
Fax-to-email: +44 (0)870 094 0861
Date: 16-Nov-09   Time: 12:55:25
-- XFMail --

__
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] Odp: ^ operator

2009-11-16 Thread Liviu Andronic
On 11/16/09, Ted Harding ted.hard...@manchester.ac.uk wrote:
 Not in this case (see below), though of course in general - takes
  precedence over ^, so, for example, in the expression

   -2^(1/3)

  the - is applied first, giving (-2); and then ^ is applied
  next, giving (-2)^(1/3). There is a work-round (see below).

Hmm.. I may be doing something wrong, but from here it looks to be the
opposite.
 -2^(1/3); -(2)^(1/3); -(2^(1/3));
[1] -1.2599
[1] -1.2599
[1] -1.2599
 (-2)^(1/3)
[1] NaN

The results don't change when switching from the unary minus.
 0-2^(1/3); 0-(2)^(1/3); 0-(2^(1/3));
[1] -1.2599
[1] -1.2599
[1] -1.2599

It seems to me that in this example ^ is applied first, and -
second. There is also this fortune entry.
 fortune(unary)

Thomas Lumley: The precedence of ^ is higher than that of unary minus.
It may be surprising,
[...]
Hervé Pagès: No, it's not surprising. At least to me... In the country
where I grew up, I've
been teached that -x^2 means -(x^2) not (-x)^2.
   -- Thomas Lumley and Hervé Pagès (both explaining that operator
precedence is working
  perfectly well)
  R-devel (January 2006)


Liviu

__
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] Odp: ^ operator

2009-11-16 Thread Alain Guillet

Hi,

You forgot to put the parenthesis in the way Petr told you : 
(-6.108576e-05)^(1/3)  and the result is NaN. What do you want to preserve?



Alain




carol white wrote:

but with complex, I get complex numbers for the first and last elements:

  

(as.complex(tmp))^(1/3)


[1] 0.01969170+0.03410703i 0.03478442+0.i 0.03285672+0.i
[4] 0.08950802+0.i 0.05848363+0.10129661i

whereas for the first element, we get the followings.

Moreover, 
  

-6.108576e-05^(1/3)
 [1] -0.03938341


and
  

-(6.108576e-05^(1/3))


[1] -0.03938341
and
 -((6.108576e-05)^(1/3))
[1] -0.03938341

give the same results.

so using () doesn't preserve any thing

--- On Mon, 11/16/09, Petr PIKAL petr.pi...@precheza.cz wrote:

  

From: Petr PIKAL petr.pi...@precheza.cz
Subject: Odp: [R] ^ operator
To: carol white wht_...@yahoo.com
Cc: r-h...@stat.math.ethz.ch
Date: Monday, November 16, 2009, 3:40 AM
Hi

AFAIK, this is issue of the preference of operators. 


r-help-boun...@r-project.org
napsal dne 16.11.2009 11:24:59:



Hi,
I want to apply ^ operator to a vector but it is
  
applied to some of the 


elements correctly and to some others, it generates
  
NaN. Why is it not 
able to


calculate -6.108576e-05^(1/3) even though it exists?


  tmp
[1] -6.108576e-05  4.208762e-05 
  
3.547092e-05  7.171101e-04 
-1.600269e-03


tmp^(1/3)


[1]NaN 0.03478442
  

0.03285672 0.08950802NaN

This computes (-a)^(1/3) which is not possible in real
numbers. You have 
to use as.complex(tmp)^(1/3) to get a result.




-6.108576e-05^(1/3)


[1] -0.03938341
  

this is actually
-(6.108576e-05^(1/3))

Regards
Petr




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

  


--
Alain Guillet
Statistician and Computer Scientist

SMCS - Institut de statistique - Université catholique de Louvain
Bureau c.316
Voie du Roman Pays, 20
B-1348 Louvain-la-Neuve
Belgium

tel: +32 10 47 30 50

__
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] Odp: ^ operator

2009-11-16 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 16.11.2009 13:27:03:

 but with complex, I get complex numbers for the first and last elements:
 
  (as.complex(tmp))^(1/3)
 [1] 0.01969170+0.03410703i 0.03478442+0.i 0.03285672+0.i
 [4] 0.08950802+0.i 0.05848363+0.10129661i

And that is a right answer

 
 whereas for the first element, we get the followings.
 
 Moreover, 
  -6.108576e-05^(1/3)
   [1] -0.03938341
 and
  -(6.108576e-05^(1/3))
 [1] -0.03938341
 and
  -((6.108576e-05)^(1/3))
 [1] -0.03938341
 

No. With all constructions like above you compute a cube root of 
***positive*** number and then you put *-* sign before the result, hence 
the same result.

Try instead to make a cube root of negative number.

 (-6.108576e-05)^(1/3)
[1] NaN

This is what you exactly do by the first call. Beware also that 1/3 is not 
exactly representable in binary arithmetic and so you actually do not 
compute cube root but some root which is quite near to cube root.

 (1000^(1/3))-10
[1] -1.776357e-15

If you want cube root and have negative numbers you need probably 
something like

sign(tmp) * abs(tmp)^(1/3)


 give the same results.
 
 so using () doesn't preserve any thing

You need to use parentheses on correct places. To see what is the 
precedence of operators see

?Syntax 

Regards
Petr


 
 --- On Mon, 11/16/09, Petr PIKAL petr.pi...@precheza.cz wrote:
 
  From: Petr PIKAL petr.pi...@precheza.cz
  Subject: Odp: [R] ^ operator
  To: carol white wht_...@yahoo.com
  Cc: r-h...@stat.math.ethz.ch
  Date: Monday, November 16, 2009, 3:40 AM
  Hi
  
  AFAIK, this is issue of the preference of operators. 
  
  r-help-boun...@r-project.org
  napsal dne 16.11.2009 11:24:59:
  
   Hi,
   I want to apply ^ operator to a vector but it is
  applied to some of the 
   elements correctly and to some others, it generates
  NaN. Why is it not 
  able to
   calculate -6.108576e-05^(1/3) even though it exists?
   
   
tmp
   [1] -6.108576e-05  4.208762e-05 
  3.547092e-05  7.171101e-04 
  -1.600269e-03
tmp^(1/3)
   [1]NaN 0.03478442
  0.03285672 0.08950802NaN
  
  This computes (-a)^(1/3) which is not possible in real
  numbers. You have 
  to use as.complex(tmp)^(1/3) to get a result.
  
-6.108576e-05^(1/3)
   [1] -0.03938341
  
  this is actually
  -(6.108576e-05^(1/3))
  
  Regards
  Petr
  
  
   
   __
   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] Odp: ^ operator

2009-11-16 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 16.11.2009 13:55:30:

 
 On 16-Nov-09 11:40:29, Petr PIKAL wrote:
  Hi
  AFAIK, this is issue of the preference of operators. 
  
  r-help-boun...@r-project.org napsal dne 16.11.2009 11:24:59:
  
 
 Not in this case (see below), though of course in general - takes
 precedence over ^, so, for example, in the expression
 
   -2^(1/3)
 
 the - is applied first, giving (-2); and then ^ is applied
 next, giving (-2)^(1/3). There is a work-round (see below).

Are you sure?

I get

 -2^(1/3)
[1] -1.259921

 (-2)^(1/3)
[1] NaN

 2^(1/3)
[1] 1.259921

So ^ is applied first and then the result is negated.


See ?Syntax.

I agree with what you write below, though.

Regards
Petr




 
  Hi,
  I want to apply ^ operator to a vector but it is applied to
  some of the elements correctly and to some others, it generates
  NaN. Why is it not able to calculate -6.108576e-05^(1/3) even
  though it exists?
 
 It only exists (in the real domain) if ^ takes precedence over -
 which (in R) it does not!
 
   tmp
  [1] -6.108576e-05  4.208762e-05  3.547092e-05  7.171101e-04 
  -1.600269e-03
   tmp^(1/3)
  [1]NaN 0.03478442 0.03285672 0.08950802NaN
  
  This computes (-a)^(1/3) which is not possible in real numbers.
 
 In this example, that is not accurate. tmp has already been
 defined, and contains numbers which are already stored as negative
 numbers, so - is no longer on the scene as an operator, before
 ^ is applied; the issue of precedence of - over ^ is no
 longer present. The NaN arises from x^(1/3) where x is negative.
 
  You have to use as.complex(tmp)^(1/3) to get a result.
  
   -6.108576e-05^(1/3)
  [1] -0.03938341
 
 This is not the result I get:
 
   as.complex(tmp)^(1/3)
 # [1] 0.01969171+0.03410703i 0.03478442+0.i
 # [3] 0.03285672+0.i 0.08950802+0.i
 # [5] 0.05848363+0.10129662i
 
  this is actually
  -(6.108576e-05^(1/3))
  
  Regards
  Petr
 
 It is possible to work round the problem without using as.complex
 which can introduce complications -- see above, and also:
 
   x - (-1)
   x^(1/3)
   # [1] NaN
   as.complex(x)^(1/3)
   # [1] 0.5+0.8660254i
 
   as.complex(-1)^(1/2)
   # [1] 0+1i
 
 which you would not want if you are working throughout in real
 numbers (you would want the result -1 instead). Although, in the
 mathematics of complex numbers, (-1)^(1/3) has three values, one
 of which is -1, R only returns a single value.
 
 However, you would have to define a new operator, called say %^%:
 
   %^%-function(X,x){sign(X)*(abs(X)^x)}
 
   tmp - c(-6.108576e-05, 4.208762e-05, 3.547092e-05,
 7.171101e-04, -1.600269e-03)
   tmp%^%(1/3)
   # [1] -0.03938341  0.03478442  0.03285672  0.08950802 -0.11696726
 
 The definition of %^% forces ^ to take precedence over -,
 by in effect removing - from the scene until ^ has done its
 work. But, if you hope to rely on this, note that if you apply
 to 'tmp' any function in which the ordinary ^ will be used on
 a negative number, you will still have the same problem.
 
 Note: Trying to redefine ^ will not work, since invoking the
 result initiates an infinite recursion:
 
   ^- function(X,x){sign(X)*(abs(X)^x)}
   ## (This definition will be accepted by R)
   tmp%^%(1/3)
   # Error: evaluation nested too deeply: infinite recursion /
   #   options(expressions=)?
 
 It's not a clean situatio, but I hope the above helps!
 Ted.
 
 
 E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
 Fax-to-email: +44 (0)870 094 0861
 Date: 16-Nov-09   Time: 12:55:25
 -- XFMail --
 
 __
 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] Odp: ^ operator

2009-11-16 Thread Ted Harding
On 16-Nov-09 13:13:27, Liviu Andronic wrote:
 On 11/16/09, Ted Harding ted.hard...@manchester.ac.uk wrote:
 Not in this case (see below), though of course in general - takes
  precedence over ^, so, for example, in the expression

   -2^(1/3)

  the - is applied first, giving (-2); and then ^ is applied
  next, giving (-2)^(1/3). There is a work-round (see below).

 Hmm.. I may be doing something wrong, but from here it looks to be the
 opposite.
 -2^(1/3); -(2)^(1/3); -(2^(1/3));
 [1] -1.2599
 [1] -1.2599
 [1] -1.2599
 (-2)^(1/3)
 [1] NaN
 
 The results don't change when switching from the unary minus.
 0-2^(1/3); 0-(2)^(1/3); 0-(2^(1/3));
 [1] -1.2599
 [1] -1.2599
 [1] -1.2599

Correct!!! I was inadvertently put on the wrong foot by Pietr
Pikal's comment about precedence, and as a result what I wrote
about precedence of ^ relative to - was on the wrong foot
throughout, and should be ignored. My apologies for any confusion
this may have caused to anybody.

In any case, this is not relevant to Carol White's query about
taking the cube root (or indeed any fractional power) of a
negative number. This can only be done (as Carol intended it)
by using the form sign(x)*(abs(x)^power).

As I tried to point out, there is a distinction between an
expression which the user may enter as x - -1.234, and then
x^(1/3), expecting -(1.234^(1/3)), and the cube root of the
negative number x.

Ted.

 It seems to me that in this example ^ is applied first, and -
 second. There is also this fortune entry.
 fortune(unary)
 
 Thomas Lumley: The precedence of ^ is higher than that of unary minus.
 It may be surprising,
 [...]
 Hervé Pagès: No, it's not surprising. At least to me... In the
 country
 where I grew up, I've
 been teached that -x^2 means -(x^2) not (-x)^2.
-- Thomas Lumley and Hervé Pagès (both explaining that operator
 precedence is working
   perfectly well)
   R-devel (January 2006)
 
 
 Liviu
 
 __
 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.


E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk
Fax-to-email: +44 (0)870 094 0861
Date: 16-Nov-09   Time: 13:40:06
-- XFMail --

__
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] Weird operator behaviour

2009-11-03 Thread Erik Iverson
Argument recycling is coming into play here with !=.  I am guessing you want

! fish$Species %in% c(CRA, PHC)

?

Best Regards,
Erik Iverson 
 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
 On Behalf Of jimdare
 Sent: Tuesday, November 03, 2009 4:02 PM
 To: r-help@r-project.org
 Subject: [R] Weird operator behaviour
 
 
 Hi,
 
 I have a dataset called 'fish'.  fish$Species returns extract 1.  When I
 use
 fish$Species != c(CRA,PHC), i.e. I want all species except CRA and
 PHC, I get extract 2 which is blatantly wrong.  Can anyone see what I'm
 doing wrong?
 
 Regards,
 James
 
 
 EXTRACT 1
 
  fish$Species
   [1] ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ATO
 ATO
  [19] ATO ATO ATO ATO ATO ATO BIG BIG BIG BIG BIG BIG BIG BIG BIG BIG BIG
 BIG
  [37] BIG BIG BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS
 CRA
  [55] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
  [73] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
  [91] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [109] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [127] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [145] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [163] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [181] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [199] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [217] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [235] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [253] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [271] CRA CRA CRA CRA CRA CRA CRA CRA MAK MAK MAK MAK MAK MAK MAK MAK MAK
 MAK
 [289] MAK MAK MAK MAK MAK MOO MOO MOO MOO MOO MOO MOO MOO MOO MOO MOO MOO
 MOO
 [307] MOO MOO PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC
 PHC
 [325] PHC PHC PHC POS POS POS POS POS POS POS POS POS POS POS PTO PTO PTO
 PTO
 [343] PTO PTO PTO PTO PTO PTO PTO PTO PTO PTO PTO RBM RBM RBM RBM RBM RBM
 RBM
 [361] RBM RBM RBM RBM RBM RBM RBM RBM SKJ SKJ SKJ SKJ SKJ SKJ SKJ SKJ SKJ
 SKJ
 [379] SKJ SKJ SKJ SKJ SKJ STM STM STM STM STM STM STM STM STN STN STN STN
 STN
 [397] STN STN STN STN STN STN STN STN STN STN STN STN STN STN STN STN STN
 STN
 [415] SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO TOR
 TOR
 [433] TOR TOR TOR TOR TOR TOR YFN YFN YFN YFN YFN YFN YFN YFN YFN YFN YFN
 YFN
 [451] YFN YFN YFN YFN
 17 Levels: ALB ATO BIG BWS CRA MAK MOO PHC POS PTO RBM SKJ STM STN SWO ...
 YFN
 
 EXTRACT 2
 
  fish$Species != c(CRA,PHC)
   [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [37]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [61] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [73] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [85] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [97] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [109] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [121] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [133] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [145] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [157] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [169] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [181] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [193] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [205] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [217] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [229] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [241] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [253] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [265] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [277] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
 [289]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
 [301]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
 FALSE
 [313]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
 FALSE
 [325]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  

Re: [R] Weird operator behaviour

2009-11-03 Thread Peter Alspach
Tena koe James

Have you tried something like !fish$Species%in%c('CRA','PHC')?

HTH 

Peter Alspach 

 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of jimdare
 Sent: Wednesday, 4 November 2009 11:02 a.m.
 To: r-help@r-project.org
 Subject: [R] Weird operator behaviour
 
 
 Hi,
 
 I have a dataset called 'fish'.  fish$Species returns extract 
 1.  When I use fish$Species != c(CRA,PHC), i.e. I want 
 all species except CRA and PHC, I get extract 2 which is 
 blatantly wrong.  Can anyone see what I'm doing wrong? 
 
 Regards,
 James 
 
 
 EXTRACT 1
 
  fish$Species
   [1] ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB 
 ALB ALB ATO ATO  [19] ATO ATO ATO ATO ATO ATO BIG BIG BIG BIG 
 BIG BIG BIG BIG BIG BIG BIG BIG  [37] BIG BIG BWS BWS BWS BWS 
 BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS CRA  [55] CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA  [73] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA  [91] CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA [109] CRA CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA [127] CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA [145] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA [163] CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA [181] CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA [199] 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA CRA [217] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA [235] CRA CRA CRA CRA CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA [253] CRA CRA CRA 
 CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA 
 [271] CRA CRA CRA CRA CRA CRA CRA CRA MAK MAK MAK MAK MAK MAK 
 MAK MAK MAK MAK [289] MAK MAK MAK MAK MAK MOO MOO MOO MOO MOO 
 MOO MOO MOO MOO MOO MOO MOO MOO [307] MOO MOO PHC PHC PHC PHC 
 PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC [325] PHC PHC 
 PHC POS POS POS POS POS POS POS POS POS POS POS PTO PTO PTO 
 PTO [343] PTO PTO PTO PTO PTO PTO PTO PTO PTO PTO PTO RBM RBM 
 RBM RBM RBM RBM RBM [361] RBM RBM RBM RBM RBM RBM RBM RBM SKJ 
 SKJ SKJ SKJ SKJ SKJ SKJ SKJ SKJ SKJ [379] SKJ SKJ SKJ SKJ SKJ 
 STM STM STM STM STM STM STM STM STN STN STN STN STN [397] STN 
 STN STN STN STN STN STN STN STN STN STN STN STN STN STN STN 
 STN STN [415] SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO 
 SWO SWO SWO SWO TOR TOR [433] TOR TOR TOR TOR TOR TOR YFN YFN 
 YFN YFN YFN YFN YFN YFN YFN YFN YFN YFN [451] YFN YFN YFN YFN
 17 Levels: ALB ATO BIG BWS CRA MAK MOO PHC POS PTO RBM SKJ 
 STM STN SWO ...
 YFN
 
 EXTRACT 2
 
  fish$Species != c(CRA,PHC)
   [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  
 TRUE  TRUE TRUE  [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  
 TRUE  TRUE  TRUE  TRUE  TRUE TRUE  [25]  TRUE  TRUE  TRUE  
 TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE TRUE  [37]  
 TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  
 TRUE TRUE  [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  
 TRUE FALSE  TRUE FALSE TRUE  [61] FALSE  TRUE FALSE  TRUE 
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE  [73] FALSE  
 TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE 
 TRUE  [85] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE 
 FALSE  TRUE FALSE TRUE  [97] FALSE  TRUE FALSE  TRUE FALSE  
 TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE [109] FALSE  TRUE 
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE 
 [121] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  
 TRUE FALSE TRUE [133] FALSE  TRUE FALSE  TRUE FALSE  TRUE 
 FALSE  TRUE FALSE  TRUE FALSE TRUE [145] FALSE  TRUE FALSE  
 TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE [157] 
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE 
 FALSE TRUE [169] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  
 TRUE FALSE  TRUE FALSE TRUE [181] FALSE  TRUE FALSE  TRUE 
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE [193] FALSE  
 TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE 
 TRUE [205] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE 
 FALSE  TRUE FALSE TRUE [217] FALSE  TRUE FALSE  TRUE FALSE  
 TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE [229] FALSE  TRUE 
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE 
 [241] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  
 TRUE FALSE TRUE [253] FALSE  TRUE FALSE  TRUE FALSE  TRUE 
 FALSE  TRUE FALSE  TRUE FALSE TRUE [265] FALSE  TRUE FALSE  
 TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE TRUE [277] 
 FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  
 TRUE TRUE [289]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  
 TRUE  TRUE  TRUE  TRUE TRUE [301]  TRUE  TRUE  TRUE  TRUE  
 TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE [313]  TRUE 
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE 
 FALSE [325]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  

Re: [R] Weird operator behaviour

2009-11-03 Thread Jorge Ivan Velez
Hi Jim,

Take a look at ?%in%, especially its first example.

HTH,
Jorge


On Tue, Nov 3, 2009 at 5:02 PM, jimdare  wrote:


 Hi,

 I have a dataset called 'fish'.  fish$Species returns extract 1.  When I
 use
 fish$Species != c(CRA,PHC), i.e. I want all species except CRA and
 PHC, I get extract 2 which is blatantly wrong.  Can anyone see what I'm
 doing wrong?

 Regards,
 James


 EXTRACT 1

  fish$Species
  [1] ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ALB ATO
 ATO
  [19] ATO ATO ATO ATO ATO ATO BIG BIG BIG BIG BIG BIG BIG BIG BIG BIG BIG
 BIG
  [37] BIG BIG BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS BWS
 CRA
  [55] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
  [73] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
  [91] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [109] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [127] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [145] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [163] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [181] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [199] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [217] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [235] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [253] CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA CRA
 CRA
 [271] CRA CRA CRA CRA CRA CRA CRA CRA MAK MAK MAK MAK MAK MAK MAK MAK MAK
 MAK
 [289] MAK MAK MAK MAK MAK MOO MOO MOO MOO MOO MOO MOO MOO MOO MOO MOO MOO
 MOO
 [307] MOO MOO PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC PHC
 PHC
 [325] PHC PHC PHC POS POS POS POS POS POS POS POS POS POS POS PTO PTO PTO
 PTO
 [343] PTO PTO PTO PTO PTO PTO PTO PTO PTO PTO PTO RBM RBM RBM RBM RBM RBM
 RBM
 [361] RBM RBM RBM RBM RBM RBM RBM RBM SKJ SKJ SKJ SKJ SKJ SKJ SKJ SKJ SKJ
 SKJ
 [379] SKJ SKJ SKJ SKJ SKJ STM STM STM STM STM STM STM STM STN STN STN STN
 STN
 [397] STN STN STN STN STN STN STN STN STN STN STN STN STN STN STN STN STN
 STN
 [415] SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO SWO TOR
 TOR
 [433] TOR TOR TOR TOR TOR TOR YFN YFN YFN YFN YFN YFN YFN YFN YFN YFN YFN
 YFN
 [451] YFN YFN YFN YFN
 17 Levels: ALB ATO BIG BWS CRA MAK MOO PHC POS PTO RBM SKJ STM STN SWO ...
 YFN

 EXTRACT 2

  fish$Species != c(CRA,PHC)
  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [37]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
  [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [61] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [73] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [85] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
  [97] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [109] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [121] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [133] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [145] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [157] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [169] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [181] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [193] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [205] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [217] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [229] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [241] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [253] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [265] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 TRUE
 [277] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
 [289]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
 [301]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
 FALSE
 [313]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
 FALSE
 [325]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
 [337]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
 [349]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 TRUE
 [361]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  

Re: [R] VEC Operator in R

2008-10-23 Thread Gábor Csárdi
And what is this operator supposed to do? Maybe you just want
as.vector, but it is hard to say from this.

Gabor

On Thu, Oct 23, 2008 at 7:47 PM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context: 
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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




-- 
Gabor Csardi [EMAIL PROTECTED] UNIL DGM

__
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] VEC Operator in R

2008-10-23 Thread Henrique Dallazuanna
I think you can use as.vector

On Thu, Oct 23, 2008 at 3:47 PM, megh [EMAIL PROTECTED] wrote:


 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context:
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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] VEC Operator in R

2008-10-23 Thread Sarah Goslee
It might help if you told us what you expected VEC
and VECH to do.

On Thu, Oct 23, 2008 at 1:47 PM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --

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

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


Re: [R] VEC Operator in R

2008-10-23 Thread Gabor Grothendieck
Try:

RsiteSearch(vech)


On Thu, Oct 23, 2008 at 1:47 PM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context: 
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] VEC Operator in R

2008-10-23 Thread Patrick Burns

For 'VEC' you may not need to do anything -- in
a lot of circumstances it will just work.

For 'VECH' you can write a function using the
'lower.tri' function.

But it seems to me that the assumption behind your
question is that a simple function that someone else
has written is going to be better than a simple function
that you write.  Not true (barring bugs).

R is egalitarian -- your functions have the same standing
as official functions. A lot of times it is faster to write
a function of your own rather than search out someone
else's even if you know such functions exist.


Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and A Guide for the Unwilling S User)

megh wrote:

Can anyone please tell whether there is any R function to act as VEC and
VECH operator on Matrix? Yes of course, I can write a
user-defined-function for that or else, I can put dim(mat) - NULL. However
I am looking for some R function.

Your help will be highly appreciated.

Regards,



__
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] VEC Operator in R

2008-10-23 Thread Kingsford Jones
RSiteSearch('vec vech', restrict = 'fun') turns up several packages
with vec and vech functions.

E.g., matrixcalc, fUtilities, ks, ...


hth,

Kingsford Jones

On Thu, Oct 23, 2008 at 11:47 AM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context: 
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] VEC Operator in R

2008-10-23 Thread Kingsford Jones
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---
RSiteSearch('vec vech', restrict = 'fun') turns up several packages
with vec and vech functions.

E.g., matrixcalc, fUtilities, ks, ...


hth,

Kingsford Jones

On Thu, Oct 23, 2008 at 11:47 AM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context: 
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
---End Message---
__
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] VEC Operator in R

2008-10-23 Thread Kingsford Jones
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---
RSiteSearch('vec vech', restrict = 'fun') turns up several packages
with vec and vech functions.

E.g., matrixcalc, fUtilities, ks, ...


hth,

Kingsford Jones

On Thu, Oct 23, 2008 at 11:47 AM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context: 
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
---End Message---
__
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] VEC Operator in R

2008-10-23 Thread Patrick Burns
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---

For 'VEC' you may not need to do anything -- in
a lot of circumstances it will just work.

For 'VECH' you can write a function using the
'lower.tri' function.

But it seems to me that the assumption behind your
question is that a simple function that someone else
has written is going to be better than a simple function
that you write.  Not true (barring bugs).

R is egalitarian -- your functions have the same standing
as official functions. A lot of times it is faster to write
a function of your own rather than search out someone
else's even if you know such functions exist.


Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and A Guide for the Unwilling S User)

megh wrote:

Can anyone please tell whether there is any R function to act as VEC and
VECH operator on Matrix? Yes of course, I can write a
user-defined-function for that or else, I can put dim(mat) - NULL. However
I am looking for some R function.

Your help will be highly appreciated.

Regards,



__
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 Message---
__
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] VEC Operator in R

2008-10-23 Thread Patrick Burns
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---

For 'VEC' you may not need to do anything -- in
a lot of circumstances it will just work.

For 'VECH' you can write a function using the
'lower.tri' function.

But it seems to me that the assumption behind your
question is that a simple function that someone else
has written is going to be better than a simple function
that you write.  Not true (barring bugs).

R is egalitarian -- your functions have the same standing
as official functions. A lot of times it is faster to write
a function of your own rather than search out someone
else's even if you know such functions exist.


Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and A Guide for the Unwilling S User)

megh wrote:

Can anyone please tell whether there is any R function to act as VEC and
VECH operator on Matrix? Yes of course, I can write a
user-defined-function for that or else, I can put dim(mat) - NULL. However
I am looking for some R function.

Your help will be highly appreciated.

Regards,



__
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 Message---
__
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] VEC Operator in R

2008-10-23 Thread Gabor Grothendieck
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---
Try:

RsiteSearch(vech)


On Thu, Oct 23, 2008 at 1:47 PM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context: 
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
---End Message---
__
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] VEC Operator in R

2008-10-23 Thread Gabor Grothendieck
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---
Try:

RsiteSearch(vech)


On Thu, Oct 23, 2008 at 1:47 PM, megh [EMAIL PROTECTED] wrote:

 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context: 
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
---End Message---
__
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] VEC Operator in R

2008-10-23 Thread Henrique Dallazuanna
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---
I think you can use as.vector

On Thu, Oct 23, 2008 at 3:47 PM, megh [EMAIL PROTECTED] wrote:


 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context:
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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.
---End Message---
__
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] VEC Operator in R

2008-10-23 Thread Henrique Dallazuanna
Sender: [EMAIL PROTECTED]
On-Behalf-Of: [EMAIL PROTECTED]
Subject: Re: [R] VEC Operator in R
Message-Id: [EMAIL PROTECTED]
Recipient: [EMAIL PROTECTED]




This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Thank You.
---BeginMessage---
I think you can use as.vector

On Thu, Oct 23, 2008 at 3:47 PM, megh [EMAIL PROTECTED] wrote:


 Can anyone please tell whether there is any R function to act as VEC and
 VECH operator on Matrix? Yes of course, I can write a
 user-defined-function for that or else, I can put dim(mat) - NULL. However
 I am looking for some R function.

 Your help will be highly appreciated.

 Regards,
 --
 View this message in context:
 http://www.nabble.com/VEC-Operator-in-R-tp20136201p20136201.html
 Sent from the R help mailing list archive at Nabble.com.

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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.
---End Message---
__
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   >