将data.frame列格式从字符转换为因子 [英] convert data.frame column format from character to factor
本文介绍了将data.frame列格式从字符转换为因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用R语言编程。我想将我的data.frame对象( mydf
)的某些列的格式(类)从 charactor 更改为 factor 。我不想这样做,当我通过 read.table()
函数读取文本文件。任何帮助将不胜感激。
I am programming in R language. I would like to change the format (class) of some columns of my data.frame object (mydf
) from charactor to factor. I don't want to do this when I'm reading the text file by read.table()
function. Any help would be appreciated.
推荐答案
欢迎来到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参考手册:
http://cran.r-project.org/manuals.html
R参考卡: http://cran.r-project .org / doc / contrib / Short-refcard.pdf
这篇关于将data.frame列格式从字符转换为因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文