erasmortg
erasmortg

Reputation: 3278

Subset data frames inside of a list based on column classes

I have a very large list comprised of data frames, every element of the list is a different data frame, where each column is comprised of different types of variables, and data frames of different lengths. I want to subset the data frames in this list, and keep only those columns have classes 'integer' or 'numeric', while keeping the data frame structure (so seemingly no 'lapply').

A MRE follows:

 x1 <- c(1,2,3,4)
y1 <- c(letters[1:4])
z1 <- as.integer(c(0, 1, 0, 1))
df1 <- data.frame(x1,y1,z1)
str(df1)

x2 <- c(0, 1, 2, 3,4 )
y2 <- as.integer(c(0, 1, 0, 1, 0))
z2 <- c(letters[1:5])
df2 <- data.frame(x2,y2,z2)
str(df2)

list12 <- list(df1, df2)
str(list12)

#the following have not worked or returned errors:
#list12<- sapply(list12, function (x) subset(x, select = class %in%        c('character', 'factor'), drop =FALSE))
#Error in match(x, table, nomatch = 0L) : 
#  'match' requires vector arguments 

#list12 <- list12[sapply(list12, function(x) subset(x, select x %in% class is.numeric(x) || is.integer(x))]
#unexpected symbol

#list12 <- list12[, sapply(list12, function(x) is.numeric(x) || is.integer(x))]
#  incorrect number of dimensions

#list12 <- sapply(list12, function(x) subset(x, select = class is.numeric(x) || is.integer(x))
#unexpected symbol

My expected result is a list of 2 data frames, with only columns that contain integers or numeric classes

Upvotes: 4

Views: 1299

Answers (3)

Brandon Bertelsen
Brandon Bertelsen

Reputation: 44658

I like David's answer (+1), but using sapply() feels more natural to me.

lapply(list12, function(x) x[sapply(x, is.numeric)])

Upvotes: 1

David Arenburg
David Arenburg

Reputation: 92292

Another option is to use Filter within lapply

lapply(list12, Filter, f = is.numeric)
# [[1]]
#   x1 z1
# 1  1  0
# 2  2  1
# 3  3  0
# 4  4  1
# 
# [[2]]
#   x2 y2
# 1  0  0
# 2  1  1
# 3  2  0
# 4  3  1
# 5  4  0

Upvotes: 3

nicola
nicola

Reputation: 24490

Just try:

lapply(list12,function(x) x[vapply(x,class,"") %in% c("integer","numeric")])

Upvotes: 1

Related Questions