简体   繁体   中英

How do I subset my data.frame by field type (e.g., numeric, character)?

I'd like to be able to subset my data.frame , DATA , into numeric fields and factor-type fields. My goal is to write generalized scripts to summarize my data, however, certain functions, such as hist or quantile , are not appropriate for non-numeric data. And it would make more sense to run table on the factor-type fields.

I tried using

types <- apply( DATA, 2, typeof)

to create a list of types for each field which I could then subset DATA by. However, this only caused errors. I'm sure there is a simply way of doing this but I've done a lot of searching and can't come up with anything.

Thanks.

[Since it worked, I'm posting my comment as an answer to this:]

Try lapply(DATA,class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM