gpt4 book ai didi

r - 将 data.frame 列格式从字符转换为因子

转载 作者:行者123 更新时间:2023-12-03 04:27:01 27 4
gpt4 key购买 nike

我想将 data.frame 对象 (mydf) 的某些列的格式(类)从 charactor 更改为 factor .

当我通过 read.table() 函数读取文本文件时,我不想这样做。

如有任何帮助,我们将不胜感激。

最佳答案

欢迎来到 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

这也适用于字符、日期、整数和其他类

由于您是 R 新手,我建议您查看这两个网站:

R 引用手册: http://cran.r-project.org/manuals.html

R 引用卡: http://cran.r-project.org/doc/contrib/Short-refcard.pdf

关于r - 将 data.frame 列格式从字符转换为因子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com