buhtz
buhtz

Reputation: 12202

How to describe the data structure of a data frame in R?

I am looking for a way describe the data structure (not the data itself) of a data frame in R.

df = read.csv2('data.csv')
desc(df)

Output should be like

 df.columnname1
 df.columnname2 (type)
 ...

Upvotes: 1

Views: 3809

Answers (1)

Alex Ioannides
Alex Ioannides

Reputation: 1222

Try:

df_types <- data.frame("col_types" = unlist(lapply(df, typeof)))

I use lapply to loop over columns getting their type. This output is a named list, so I then use unlist to convert into a named character vector and cast this to a data.frame to get the printed output in the format that you're after.

Upvotes: 3

Related Questions