What Is the Meaning of the Dollar Sign "$" in R Function()

What is the meaning of the dollar sign $ in R function()?

The $ allows you extract elements by name from a named list. For example

x <- list(a=1, b=2, c=3)
x$b
# [1] 2

You can find the names of a list using names()

names(x)
# [1] "a" "b" "c"

This is a basic extraction operator. You can view the corresponding help page by typing ?Extract in R.

Dollar sign before a variable

To access a column, use:

my_data[ , cond]

or

my_data[[cond]]

The ith row can be accessed with:

my_data[i, ]

Combine both to obtain the desired value:

my_data[i, cond]

or

my_data[[cond]][i]

What is the difference between . (dot) and $ (dollar sign)?

The $ operator is for avoiding parentheses. Anything appearing after it will take precedence over anything that comes before.

For example, let's say you've got a line that reads:

putStrLn (show (1 + 1))

If you want to get rid of those parentheses, any of the following lines would also do the same thing:

putStrLn (show $ 1 + 1)
putStrLn $ show (1 + 1)
putStrLn $ show $ 1 + 1

The primary purpose of the . operator is not to avoid parentheses, but to chain functions. It lets you tie the output of whatever appears on the right to the input of whatever appears on the left. This usually also results in fewer parentheses, but works differently.

Going back to the same example:

putStrLn (show (1 + 1))
  1. (1 + 1) doesn't have an input, and therefore cannot be used with the . operator.
  2. show can take an Int and return a String.
  3. putStrLn can take a String and return an IO ().

You can chain show to putStrLn like this:

(putStrLn . show) (1 + 1)

If that's too many parentheses for your liking, get rid of them with the $ operator:

putStrLn . show $ 1 + 1

Why do you use '$ (the dollar sign) while adding multiple conditions in an R code?

Most of the tidyverse (and tidyverse includes dplyr) functions use Data masking. To quote from package authors-

.. provides data masking, which blurs the distinction between two types of variables:

  1. env-variables are "programming" variables and live in an environment. They are usually created with <-. Env-variables can be any type of R object.

  1. data-variables are "statistical" variables and live in a data frame. Data-variables live inside data frames, so must be vectors.

Now filter function comes from dplyr which uses data masking. So market_segment variable which live inside hotel_bookings can be called directly inside dplyr functions. This may not be the case always with base R functions.

hotel_bookings[hotel_bookings$hotel=="City Hotel" & 
hotel_bookings$market_segment=="Online TA", ]

The following will not work here

hotel_bookings[hotel=="City Hotel" & 
market_segment=="Online TA", ]

Thus inside all dplyr functions, variable names can be called by themselves (without using $).

For further reading/reference please see this page.

What are the special dollar sign shell variables?


  • $1, $2, $3, ... are the positional parameters.
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
  • "$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
  • $# is the number of positional parameters.
  • $- current options set for the shell.
  • $$ pid of the current shell (not subshell).
  • $_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
  • $IFS is the (input) field separator.
  • $? is the most recent foreground pipeline exit status.
  • $! is the PID of the most recent background command.
  • $0 is the name of the shell or shell script.

Most of the above can be found under Special Parameters in the Bash Reference Manual. There are all the environment variables set by the shell.

For a comprehensive index, please see the Reference Manual Variable Index.

R: removing the '$' symbols


 dat <- gsub('[$]','',dat)
dat <- as.numeric(gsub(',','',dat))
> dat
[1] 129900 139900 254000 260000 290000 295000

In one step

 gsub('[$]([0-9]+)[,]([0-9]+)','\\1\\2',dat)
[1] "129900" "139900" "254000" "260000" "290000" "295000"

Removing parenthesis & dollar sign using gsub() in R?

I think the easiest way is to step through what is replaced. I'm inferring that you do not want to lose the negative-ness suggested by the parens, so we'll do two steps:

s <- c("($2,345)", "$3,500", "$5,600", "($3,234)")
gsub("[$),]", "", s)
# [1] "(2345" "3500" "5600" "(3234"

This removes most of the junk (that we did not want/need to keep), now let's deal with the leading left-paren:

gsub("^\\s*[(]", "-", gsub("[$),]", "", s))
# [1] "-2345" "3500" "5600" "-3234"

From here, if needed, you can convert to numeric:

as.numeric(gsub("^\\s*[(]", "-", gsub("[$),]", "", s)))
# [1] -2345 3500 5600 -3234

This is not very robust to mal-formed strings. For instance, though I look for (and remove) leading space for the left-paren, anything else there will be a problem.

When should I use the dollar symbol ($) in a variable name?

From the Java Language Specification on identifiers:

The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

Meaning of $ in a string?

To Python, those dollar signs mean nothing at all. Just like the 'D' or 'a' that follow, the dollar sign is merely a character in a string.

To your source-code control system, the dollar signs indicate a substitution command. When you check out a new copy of your source code, that string is replaced with the timestamp of the last committed change to the file.

Reference:

  • http://svnbook.red-bean.com/en/1.6/svn.advanced.props.special.keywords.html
  • http://www.badgertronics.com/writings/cvs/keywords.html


Related Topics



Leave a reply



Submit