简体   繁体   中英

How to read a data frame as factors?

I have a data frame stored in a file that looks like this:

             V1          V2      ...... V100
V1           0            1              0
V2           0            0              1
.
.
.
V1000

As you can see, the values in the dataframe can be only "0" or "1". When I do

myData<-read.table("data.txt")

R considers the columns to be of class "integers". I would like to modify the class of all my columns to become of class "factor". Is there an easy to do it?

我会建议:

df[] <- lapply(df, factor)

如乔兰建议,您可以使用

myData<-read.table("data.txt", colClasses="factor")

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