Hossein
Hossein

Reputation: 471

Removing columns of some data frames in R

I want to remove columns A and B from some data frames. But all my data frames do not have the same columns. For example, df1 has columns A, B and C, so:

df1 <- subset(df1 , select = -c(`A`,`B`))

But df2 has columns A and D and does not have column B, so I can do:

df2 <- subset(df2 , select = -c(`A`))

Since I have many data frames, is there a way to change the first code that is also usable for the second df without getting the error that column B is not in the second data frame?

Upvotes: 0

Views: 66

Answers (1)

akrun
akrun

Reputation: 887118

One way is to use setdiff

setdiff(names(df1), c('A', 'B'))

It can be passed within the subset 'select' argument

subset(df1, select = setdiff(names(df1), c('A', 'B')))

It can be wrapped in a function and used for multiple datasets

fsubset <- function(dat, colstoRemove){
    subset(dat, select = setdiff(names(dat), colstoRemove))
}


fsubset(df1, c("A", "B"))
fsubset(df2, c("A", "B"))

data

df1 <- data.frame(A = 1:5, B = 6:10, C = 11:15)
df2 <- data.frame(A = 1:6, D = 11:16)

Upvotes: 2

Related Questions