John Seo
John Seo

Reputation: 25

Any way to create a list of dataframes, while retaining dataframe object type?

Is there anyway to create a list of dataframes while keeping the dataframe object type? The reason I am trying to do this is that I am trying to loop a function over the list of dataframes, and the function only works on objects of class data.frame. For instance:

> df<-data.frame("A"=c(1,1,1,1), "B"=c(2,2,2,2), "C"=c(3,3,3,3))
    A B C
  1 1 2 3
  2 1 2 3
  3 1 2 3
  4 1 2 3
> class(df)
    [1] "data.frame"

> lst<-list(df)
> class(lst[1])
    [1] "list"

Upvotes: 0

Views: 48

Answers (1)

Rorschach
Rorschach

Reputation: 32426

You could do

attr(lst, "class") <- "data.frame"
class(lst[1])
[1] "data.frame"

but, you probably just want class(lst[[1]]) to begin with.

Upvotes: 1

Related Questions