Home > Software design >  Unexpected behavior of ls(pattern="")
Unexpected behavior of ls(pattern="")

Time:11-05

As I want to bind several dataframes together to retrieve one large frame, I use mget(ls(pattern="")) to bind rows by pattern. This works usually well, except for the following example:

library(dplyr)

`Accuracy 1` <- data.frame (Product  = "10001",
                            Bias = 0.4,
                            Variance = 0.1,
                            Disturbance = 0.5)

`Accuracy 2` <-  data.frame (Product  = "20001",
                               Bias = 0.1,
                               Variance = 0.1,
                               Disturbance = 0.8)

`Accuracy_3` <-  data.frame (Product  = "30001",
                             x = "test",
                             y = 0.1,
                             z = 0.2,
                             w = "why")

acc_output <-  mget(ls(pattern="Accuracy *")) %>%
  bind_rows()

As you can see, one of the dataframes (Accuracy_3) displays a different name pattern. I was hoping as I defined the pattern with a space after the word Accuracy, that this frame would be ignored and only the first two frames would be bound together,

However, this is not what's happening here. Even if I define the pattern with a space, the binding gets done for the one with underscore as well. Why does this happen and how can I avoid this?

CodePudding user response:

The pattern in ls is a regular expression (see e.g. https://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html).

In your pattern, Accuracy *, the * is interpreted as zero or more occurences of the previous character, i.e. the space. As Accuracy_ has zero spaces, it matches the pattern.

You could instead use Accuracy or Accuracy [1-2].

  • Related