This question already has an answer here:
What does %>% mean in R [duplicate]
(1 answer)
Closed 8 years ago.
I am an entry level R user.May be this question sound like easy but it will be great if some one can help .
what is the meaning of this symbol in R-coding ...
%>%
Thank you
%>% is most commonly used as an operator for the popular dplyr package
It can be used to chain code together. It is very useful when you are performing several operations on data, and don’t want to save the output at each intermediate step.
%>% means whatever you want it to mean, in Base R anyway:
> %>%
Error: unexpected SPECIAL in "%>%"
(which means that symbol is not defined.)
Binary operators are ones that have an input from the left and from the right of the operator, just like *, + etc. You use them as you would mathematically like a * b, which R turns into the call '*'(a, b). R allows you to add your own binary operators via the %foo% syntax, with foo replace by whatever you want, as long as it hasn't already been used by R, which includes %*% and %/% for example.
`%foo%` <- function(x, y) paste("foo", x, "and foo", y)
> 1 %foo% 2
[1] "foo 1 and foo 2"
%>% takes on a specific and well-defined meaning once you load the magrittr R package for example, where it is used as a pipe operator might be in a Unix shell to chain together a series of function calls.
Related
This question already has answers here:
What are the differences between "=" and "<-" assignment operators?
(9 answers)
Closed 3 years ago.
I was wondering if there is a technical difference between the assignment operators "=" and "<-" in R. So, does it make any difference if I use:
Example 1: a = 1 or a <- 1
Example 2: a = c(1:20) or a <- c(1:20)
Thanks for your help
Sven
Yes there is. This is what the help page of '=' says:
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.
With "can be used" the help file means assigning an object here. In a function call you can't assign an object with = because = means assigning arguments there.
Basically, if you use <- then you assign a variable that you will be able to use in your current environment. For example, consider:
matrix(1,nrow=2)
This just makes a 2 row matrix. Now consider:
matrix(1,nrow<-2)
This also gives you a two row matrix, but now we also have an object called nrow which evaluates to 2! What happened is that in the second use we didn't assign the argument nrow 2, we assigned an object nrow 2 and send that to the second argument of matrix, which happens to be nrow.
Edit:
As for the edited questions. Both are the same. The use of = or <- can cause a lot of discussion as to which one is best. Many style guides advocate <- and I agree with that, but do keep spaces around <- assignments or they can become quite hard to interpret. If you don't use spaces (you should, except on twitter), I prefer =, and never use ->!
But really it doesn't matter what you use as long as you are consistent in your choice. Using = on one line and <- on the next results in very ugly code.
This question already has answers here:
How to reverse complement a DNA strand using R
(1 answer)
Reverse complementary Base
(2 answers)
Closed 1 year ago.
I have been tasked with writing an R function capable of taking a DNA string (s) as input and returning the complementary string on the reverse strand (e.g. "ACGT" returns "TGCA"). The result should look something like this:
> s <- "CCCTTAG"
> reverse.dna(s)
[1] "CTAAGGG"
I currently have the following functions for converting a string to a vector and vice versa, however any attempts I have made to use the replace() or switch() commands to substitute the complementary bases into either the string or vector have been unsuccessful.
string.to.vec <- function(s) {
strsplit(s,"") [[1]]
vec.to.string <- function(v) {
paste(v,collapse="")
As I have very limited experience using R I was wondering if anyone would be able to help me by suggesting the simplest method to implement this functionality into my function. Thanks!
We may use chartr in combination with stri_reverse
library(stringi)
chartr("ACGT", "TGCA", stri_reverse(s))
[1] "CTAAGGG"
The Bioconductor Biostrings package has functions for DNA strings. We convert s from the question to a DNAString object, run reverseComplement and then convert back to character class. Omit the last conversion if you want to keep it as a DNAString.
library(Biostrings)
s |> DNAString() |> reverseComplement() |> as.character()
## [1] "CTAAGGG"
This question already has answers here:
Making a string concatenation operator in R
(5 answers)
Closed 2 years ago.
I'm a big fan of the + operator for string concatenation in Python. I would like to extend/customize the + operator to do the same thing in R.
Here's what I have so far:
`+` <- function(a, b){
if(is.numeric(a)){
sum(a, b)
}else{
paste0(a, b)
}
This works pretty well, but in some speed tests, performs poorly compared to the original/primitive +. So, how can I refer to the primitive + instead of sum() in the second line of the function? If I just use +, of course R gives me a node stack overflow from infinite recursion.
(The answer offered in the duplicate question is another alternative, cleaner perhaps in that it does not add another function.)
Save the primitive as another function. Here I'll use a "special" function %plus% (so that it can be inlined), but it could be simply plus if you'd prefer.
`%plus%` <- `+`
`+` <- function(e1, e2) if (is.numeric(e1)) `%plus%`(e1, e2) else paste0(e1, e2)
1+2
# [1] 3
'a'+'b'
# [1] "ab"
This question already has answers here:
How to use `package::function()` inside a package when `function` is an infix operator?
(2 answers)
Closed 3 years ago.
Suppose I have a binary operator such as magrittr %>%. I can invoke it like
library(magrittr)
a %>% b
How can I use a fully qualified expression instead of library, as from this example:
a magrittr::%>% b
I tried various combinations but I could not get it to work.
First define %>%:
`%>%` <- magrittr::`%>%`
# test
3 %>% sum(1)
## [1] 4
This question already has answers here:
What are the differences between "=" and "<-" assignment operators?
(9 answers)
Closed 6 years ago.
It seems output is same when I use any of the two. Is there any difference between them?
x <- "hello"
x <- 'hello'
x = "hello"
x = 'hello'
It seems all are giving same output. Is there difference between them? and when to use them?
Thanks in advance!
In your examples, the answer is yes. But see notes below:
https://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html
Single and double quotes delimit character constants. They can be used
interchangeably but double quotes are preferred (and character
constants are printed using double quotes), so single quotes are
normally only used to delimit character constants containing double
quotes.
http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html
A little history before we continue: when the R language (and S before
it) was first created, <- was the only choice of assignment operator.
This is a hangover from the language APL, where the arrow notation was
used to distinguish assignment (assign the value 3 to x) from equality
(is x equal to 3?). (Professor Ripley reminds me that on APL keyboards
there was an actual key on the keyboard with the arrow symbol on it,
so the arrow was a single keystroke back then. The same was true of
the AT&T terminals first used for the predecessors of S as described
in the Blue Book.) However many modern languages (such as C, for
example) use = for assignment, so beginners using R often found the
arrow notation cumbersome, and were prone to use = by mistake. But R
uses = for yet another purpose: associating function arguments with
values (as in pnorm(1, sd=2), to set the standard deviation to 2). To
make things easier for new users familiar with languages like C, R
added the capability in 2001 to also allow = be used as an assignment
operator, on the basis that the intent (assignment or association) is
usually clear by context. So, x = 3
clearly means "assign 3 to x", whereas
f(x = 3)
clearly means "call function f, setting the argument x to 3".