I have a dataframe with different variables. For example:
x10 <- c(1, 2, 3)
x11 <- c(3, 2, 1)
x12 <- c(1, 2, 3)
y05_p <- c(5, 6, 7)
y06_p <- c(4, 5, 6)
y07_p <- c(3, 4, 5)
dat <- data.frame(x10, x11, x12, y05_p, y06_p, y07_p)
> dat
x10 x11 x12 y05_p y06_p y07_p
1 1 3 1 5 4 3
2 2 2 2 6 5 4
3 3 1 3 7 6 5
Now i would like to drop some variables, but with specific conditions: For example, all variables called "x", no matter what number following. In other words: I want to use a "placeholder", to drop every variable, that includes "x" in the name.
Using subset, this could look like:
dat <- subset(dat, select = -c(x*))
Here, the "*" is the placeholder.
Or just with "select":
dat <- select(dat, -x*)
The result should look like:
dat <- select(dat, -x*)
> dat
y05_p y06_p y07_p
1 5 4 3
2 6 5 4
3 7 6 5
Or to work with another example:
dat <- select(dat, -y*_p)
> dat
x10 x11 x12
1 1 3 1
2 2 2 2
3 3 1 3
I am grateful for any help.