Skip to content

Instantly share code, notes, and snippets.

@pH-7
Last active August 31, 2025 08:05
Show Gist options
  • Select an option

  • Save pH-7/7c2b7ec3317bf576e5f1052302a79e8d to your computer and use it in GitHub Desktop.

Select an option

Save pH-7/7c2b7ec3317bf576e5f1052302a79e8d to your computer and use it in GitHub Desktop.
Learn how variables are declared in programming languages such as Python/R. (These are my notes that I use when mentoring someone who is learning the basics of programming and data science with Python) https://pierrehenry.substack.com | https://pH7.me

Variables/Constants in Programming Languages Context

In contrast to variables, constants do not change during the execution of a program and are usually declared in all capitals (uppercase), which is a common convention in programming.

What does ~ tilde mean in R programming language?

In R, the tilde (~) symbol is commonly used in formula syntax to specify the relationship between variables in statistical models or data manipulation operations.

In the context of facet_grid() function in R, the tilde (~) followed by a dot (.) is used to create a faceting formula that represents all variables in the dataset except for the ones specified explicitly.

Here's an example to illustrate its usage:

library(ggplot2)

# Create a scatter plot with faceted grid
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  facet_grid(Species ~ .)

In the above example, we are using the facet_grid() function from the ggplot2 package to create a faceted grid plot. The formula Species ~ . is specified as the argument to facet_grid().

Here's what it means:

  • The left-hand side of the formula, Species, represents the variable that defines the rows in the grid. In this case, it's the "Species" variable from the iris dataset.

  • The right-hand side of the formula, . (dot), represents all other variables in the dataset (in this case, "Sepal.Length" and "Sepal.Width" from the iris dataset) that are not explicitly mentioned on the left-hand side.

Therefore, the resulting faceted grid plot will have rows for each unique value of "Species" (setosa, versicolor, and virginica in the case of the iris dataset), and the columns will be based on all other variables (Sepal.Length and Sepal.Width) present in the dataset.

This allows you to create a grid of plots where each plot represents a combination of levels from the specified variable(s) on the left-hand side and the remaining variables on the right-hand side.

Comments are disabled for this gist.