导出数据框作为因素 [英] Export data frame as factors

查看:63
本文介绍了导出数据框作为因素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R的新手,第一次在这里发布-这可能确实很明显,但我必须缺少一些东西.

New to R and first post here - this might be really obvious but I must be missing something.

导入了一个与二进制相关的csv

Imported a csv with a binary dependent while

purchase=read.csv("../Desktop/purchase.csv", stringsASfactors=TRUE)

现在,我希望从R(在.csv中)导出相同的数据帧,但是将分类变量(最初是字符串)保留为因素,以有效地转换数据集.

Now I wish to export the same data frame from R (in .csv), but instead keep the categorical variables (which were originally strings) as factors, effectively converting the data set.

我尝试了

write.csv(purchase,'../Desktop/purchaseconverted.csv', stringsASfactors=TRUE) 

但是write函数不支持stringsASfactors.

but the write function doesn't support stringsASfactors.

欢迎您的帮助!

推荐答案

在将因素写入文件时,默认行为是将因素视为字符串.在下面的示例中使用 iris 数据集查看此数据集,该数据集包含因子列 Species :

The default behavior is going to treat factors as character strings when writing them to file. See this in the below example using the iris dataset, which contains a factor column Species:

write.csv(head(iris))
# "","Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
# "1",5.1,3.5,1.4,0.2,"setosa"
# "2",4.9,3,1.4,0.2,"setosa"
# "3",4.7,3.2,1.3,0.2,"setosa"
# "4",4.6,3.1,1.5,0.2,"setosa"
# "5",5,3.6,1.4,0.2,"setosa"
# "6",5.4,3.9,1.7,0.4,"setosa"

您可以通过将因子转换为数字来更改此设置,因此该文件包含数字值,而不是因子变量的因子标签:

You can change this by converting the factors to numeric, so the file contains the numeric values rather than the factor labels for the factor variable:

iris2 <- iris
iris2$Species <- as.numeric(iris2$Species)
> write.csv(head(iris2))
# "","Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
# "1",5.1,3.5,1.4,0.2,1
# "2",4.9,3,1.4,0.2,1
# "3",4.7,3.2,1.3,0.2,1
# "4",4.6,3.1,1.5,0.2,1
# "5",5,3.6,1.4,0.2,1
# "6",5.4,3.9,1.7,0.4,1

通过这种方式将因子值(而不是因子标签)写入CSV.

This way you write the factor values, not the factor labels, to the CSV.

这篇关于导出数据框作为因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆