将 data.frame 列格式从字符转换为因子 [英] Convert data.frame column format from character to factor
本文介绍了将 data.frame 列格式从字符转换为因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 data.frame 对象 (mydf
) 的某些列的格式(类)从 charactor 更改为 factor.
I would like to change the format (class) of some columns of my data.frame object (mydf
) from charactor to factor.
当我通过 read.table()
函数读取文本文件时,我不想这样做.
I don't want to do this when I'm reading the text file by read.table()
function.
任何帮助将不胜感激.
推荐答案
您好,欢迎来到 R 的世界.
Hi welcome to the world of R.
mtcars #look at this built in data set
str(mtcars) #allows you to see the classes of the variables (all numeric)
#one approach it to index with the $ sign and the as.factor function
mtcars$am <- as.factor(mtcars$am)
#another approach
mtcars[, 'cyl'] <- as.factor(mtcars[, 'cyl'])
str(mtcars) # now look at the classes
这也适用于字符、日期、整数和其他类
This also works for character, dates, integers and other classes
由于您是 R 的新手,我建议您查看以下两个网站:
R 参考手册:http://cran.r-project.org/manuals.html
R 参考卡: http://cran.r-project.org/doc/contrib/Short-refcard.pdf
这篇关于将 data.frame 列格式从字符转换为因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文