This question already has answers here:
What are the differences between "=" and "<-" assignment operators?
(9 answers)
Closed 3 years ago.
I'm using R 2.8.1 and it is possible to use both = and <- as variable assignment operators. What's the difference between them? Which one should I use?
From here:
The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.
Reading from "Introducing Monte Carlo Methods with R", by Robert and Casella:
"The assignment operator is =, not to be confused with ==, which is the Boolean operator for equality. An older assignment operator is <- and, for compatibility reasons, it still remains functional, but it should be ignored to ensure cleaner programming.
(As pointed out by Spector, P. (2009). 'Data Manipulation with R' - Section 8.7., an exception is when using system.time, since = is then used to identify keywords)
A misleading feature of the assignment operator <- is found in Boolean
expressions such as
> if (x[1]<-2) ...
which is supposed to test whether or not x[1] is less than -2 but ends
up allocating 2 to x[1], erasing its current value! Note also that using
> if (x[1]=-2) ...
mistakenly instead of (x[1]==-2) has the same consequence."
Related
My question is where does the piping operator of magrittr package %>% come in the order of operations?
I have a problem simmilar to the following:
set.seed(10)
df <- data.frame(a=rnorm(3),b=rnorm(3),c=rnorm(3))
df/rowSums(df) %>% round(.,3)
This results in the following non rounded figures:
a b c
1 -0.0121966 0.119878 0.8922125
To get the rounded figures I need to put df/rowSums(df) between brackets.
I experimented with the +,-,*,/ and ^ and from the results I found the order of operation is as follow:
Exponents
Piping
Multiplication and division
Addition and subtraction
Is that right or there is something wrong with my understanding of the piping operator?
The help page you are looking for is ?Syntax. (Don't feel bad for not being able to find this, it took me about six guesses at search keywords.) I'm going to quote its entire operator precedence table here:
The following unary and binary operators are defined. They are
listed in precedence groups, from highest to lowest.
‘:: :::’ access variables in a namespace
‘$ #’ component / slot extraction
‘[ [[’ indexing
‘^’ exponentiation (right to left)
‘- +’ unary minus and plus
‘:’ sequence operator
‘%any%’ special operators (including ‘%%’ and ‘%/%’)
‘* /’ multiply, divide
‘+ -’ (binary) add, subtract
‘< > <= >= == !=’ ordering and comparison
‘!’ negation
‘& &&’ and
‘| ||’ or
‘~’ as in formulae
‘-> ->>’ rightwards assignment
‘<- <<-’ assignment (right to left)
‘=’ assignment (right to left)
‘?’ help (unary and binary)
So magrittr's pipe operators, like all the operators of the form %whatever%, do indeed have precedence greater than multiply and divide but lower than exponentiation, and this is guaranteed by the language specification.
Personally, I don't see the value in these operators. Why not just write
round(df/rowSums(df), 3)
which has the evaluation order you want, and is (IMNSHO) easier to read as well?
This question already has answers here:
Boolean operators && and ||
(4 answers)
Closed 9 years ago.
Why there are four logical operators:
&, &&
|, ||
What's the differences in usage?
Yes, I've checked the docs, yet I'm a little bit confused. The docs says:
‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector. Evaluation proceeds only until the result is determined.
The longer form is appropriate for programming control-flow and
typically preferred in ‘if’ clauses.
I think a piece of example will clearly demonstrate them. Thanks.
Key differences are as below...
Long form(&& or ||) short circuits, which means if it can identify the result by just validating just the first element. While doing &&, if the comparision of first two elements resulted in false, comparing next set of elements will also result in False. So, it returns false. While doing || if comparision resulted in true in first few elements, we can confidently say that any further validations will not change the result so it returns True.
Short forms continues to do for the entire vectors and creates a vector of results and returns it.
Hope this helps.
& and && indicate logical AND and | and || indicate logical OR. The
shorter form performs elementwise comparisons in much the same way as
arithmetic operators. The longer form evaluates left to right
examining only the first element of each vector. Evaluation proceeds
only until the result is determined. The longer form is appropriate
for programming control-flow and typically preferred in if clauses.
Source: http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html
My question is where does the piping operator of magrittr package %>% come in the order of operations?
I have a problem simmilar to the following:
set.seed(10)
df <- data.frame(a=rnorm(3),b=rnorm(3),c=rnorm(3))
df/rowSums(df) %>% round(.,3)
This results in the following non rounded figures:
a b c
1 -0.0121966 0.119878 0.8922125
To get the rounded figures I need to put df/rowSums(df) between brackets.
I experimented with the +,-,*,/ and ^ and from the results I found the order of operation is as follow:
Exponents
Piping
Multiplication and division
Addition and subtraction
Is that right or there is something wrong with my understanding of the piping operator?
The help page you are looking for is ?Syntax. (Don't feel bad for not being able to find this, it took me about six guesses at search keywords.) I'm going to quote its entire operator precedence table here:
The following unary and binary operators are defined. They are
listed in precedence groups, from highest to lowest.
‘:: :::’ access variables in a namespace
‘$ #’ component / slot extraction
‘[ [[’ indexing
‘^’ exponentiation (right to left)
‘- +’ unary minus and plus
‘:’ sequence operator
‘%any%’ special operators (including ‘%%’ and ‘%/%’)
‘* /’ multiply, divide
‘+ -’ (binary) add, subtract
‘< > <= >= == !=’ ordering and comparison
‘!’ negation
‘& &&’ and
‘| ||’ or
‘~’ as in formulae
‘-> ->>’ rightwards assignment
‘<- <<-’ assignment (right to left)
‘=’ assignment (right to left)
‘?’ help (unary and binary)
So magrittr's pipe operators, like all the operators of the form %whatever%, do indeed have precedence greater than multiply and divide but lower than exponentiation, and this is guaranteed by the language specification.
Personally, I don't see the value in these operators. Why not just write
round(df/rowSums(df), 3)
which has the evaluation order you want, and is (IMNSHO) easier to read as well?
This question already has answers here:
Boolean operators && and ||
(4 answers)
Closed 9 years ago.
Why there are four logical operators:
&, &&
|, ||
What's the differences in usage?
Yes, I've checked the docs, yet I'm a little bit confused. The docs says:
‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector. Evaluation proceeds only until the result is determined.
The longer form is appropriate for programming control-flow and
typically preferred in ‘if’ clauses.
I think a piece of example will clearly demonstrate them. Thanks.
Key differences are as below...
Long form(&& or ||) short circuits, which means if it can identify the result by just validating just the first element. While doing &&, if the comparision of first two elements resulted in false, comparing next set of elements will also result in False. So, it returns false. While doing || if comparision resulted in true in first few elements, we can confidently say that any further validations will not change the result so it returns True.
Short forms continues to do for the entire vectors and creates a vector of results and returns it.
Hope this helps.
& and && indicate logical AND and | and || indicate logical OR. The
shorter form performs elementwise comparisons in much the same way as
arithmetic operators. The longer form evaluates left to right
examining only the first element of each vector. Evaluation proceeds
only until the result is determined. The longer form is appropriate
for programming control-flow and typically preferred in if clauses.
Source: http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html
This question already has answers here:
What are the differences between "=" and "<-" assignment operators?
(9 answers)
Closed 3 years ago.
Is it just a style preference?
As far as I can tell, they are the same.
I see many people prefer the "longer" <- version and I can't tell why (perhaps keeping away from = and == confusions?)
No, they are not exactly the same: the = operator cannot be used everywhere that <- can.
The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.
There are also differences in scope. See this answer for more details.
Which is better depends on who you ask.
Reading from "Introducing Monte Carlo Methods with R", by Robert and Casella:
"The assignment operator is =, not to be confused with ==, which is the Boolean operator for equality. An older assignment operator is <- and, for compatibility reasons, it still remains functional, but it should be ignored to ensure cleaner programming. (As pointed out by Spector, P. (2009). 'Data Manipulation with R' - Section 8.7., an exception is when using system.time, since = is then used to identify keywords)
Source
On the other hand, Google's R style guide recommends using <-:
Assignment
Use <-, not =, for assignment.
GOOD:
x <- 5
BAD:
x = 5