What Does the Period Mean in R?

The R programming language is heavily tied to data science and analysis. And, as that implies, the language’s syntax is consistently linked to a logical syntax. There’s not much ambiguity to be found in either math or programming languages related to mathematics. However, R does have a few exceptions to the rule. And one of them is the period “.” character. Most elements of R have a consistent meaning no matter how they’re used. But the period’s meaning in R is a little more fluid than you might expect. But you’re about to see exactly what it means in various usage scenarios in R.

The Variability of R’s Period

One of the first things to keep in mind about R’s use of periods is that they sometimes, but not always, have a special meaning. This contrasts with many other languages that reserve a specific place for periods within the general syntax. For example, periods in Python denote interaction with an object. But periods in R have different meanings in different contexts. Likewise, you’ll note that other languages tend to refer to symbol names differently depending on their functionality. With R, a period is generally just referred to as a period since it doesn’t have the same level of specific functionality as found in some other languages.

A Basic Overview of the Period

The most basic use of a period is within variable names. Periods can typically be used just like any other alphanumeric character for variable names in R. For example, take a look at the following code.

our.variable <- 10
print(our.variable)

In this example, the period is used in a similar way to a “_” in many other languages. It’s just a way to increase the readability of variables. This is typically used as an alternative for camel notation. So instead of naming a variable “ourVariable” someone might name it “our.variable”. However, note that if a variable starts with a period then it must be followed by a letter rather than a number. A variable starting with a period redefines what would be considered a valid character for what follows. But only if the period is the first character of the variable. In a similar way, you can also use periods in the names of identifiers.

More Advanced Usage Scenarios

Variables are only the tip of the iceberg for periods in R. You can also use them in a wide variety of different ways that provide more dynamic functionality. For example, you can use periods for partial matches in function calls. Take a look at the following example.

seq(1, 10, b=.5)

The “b” might look like a variable, but it’s actually a function argument. The period acts as a partial match for seq’s “by”. Note that this syntax holds true for similar usage scenarios such as a dummy argument or within outer parameters.

The period can also be used as a formula notation. Take a look at the following example to see this concept in action.

library(datasets)
ourModel <- lm(mpg ~ ., data=mtcars)
summary(ourModel)

In this example you see the linear model function load the mtcars dataset. The period signifies that all variables should be loaded from mtcars. Periods can also provide special meaning to apply a wildcard value in other contexts. For example, take a look at this code to list files in a directory.

list.files(path=”.”)

However, note that this isn’t a true wild card use. The period is specifying everything in a sense. But it’s specifying that the path is the current directory. As such, all files in the current directory are returned by the function. This notation can also be used to exclude data. For example, with “~ . – wt” to include everything except a wt variable.

Periods are also used with regular expressions. And, as you’d expect, that holds true for any use of R with regular expressions. See the following code for a simple example of this concept.

gsub(“.”, “_”, “helloWorld”)

In this example, we use the period as a wildcard selector for replacement with the “_” character. Since everything is a match for wildcard values every letter in “helloWorld” is replaced with “_”.

It’s also important to note that specific R libraries and frameworks might modify how the period is used. You can see a good example of this in the purrr package. In purrr the period can act as a placeholder value to represent whatever element is being processed in a function.

To return to an earlier point, it’s important to keep in mind that periods in R don’t have the same significance found in many other programming languages. In a sense, it’s similar to any alphanumeric character in that it can be important. But that importance is largely situational. The period gains or loses importance based on how it’s being leveraged in your code, the libraries you’re using, etc. You can make a period a central part of your code. Or you could easily ignore it outside of strings.

Scroll to top
Privacy Policy