Brad
Brad

Reputation: 680

Subset dataframe on set of columns regardless if some columns are not present

I have a dataframe with 4 columns. C1, C2, C3, C4.

To subset 3 of these columns I use;

myvars <- c("C1", "C2", "C3")
DF <- DF[myvars]

If one of C1, C2 or C3 are missing, an error message is produced.

Error in [.data.frame(All, myvars) : undefined columns selected

I want the subsetting to occur even if some of the columns are not available. So it will atleast subset the columns which are available.

Upvotes: 0

Views: 32

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

We can use intersect to get common names and then subset based on it.

my_subset <- function(df, vars) {
  df[, intersect(names(df), vars)]
}

df1 <- data.frame(C1 = 1:4, C2 = 4:7, C3 = 10:13)
myvars <- c("C1", "C2", "C3", "C4")

my_subset(df1, myvars)

#  C1 C2 C3
#1  1  4 10
#2  2  5 11
#3  3  6 12
#4  4  7 13

Upvotes: 1

Related Questions