简体   繁体   English

如何读取数值作为R中的因子?

[英]How to read numeric values as factors in R?

I have a data frame A which has numeric column like: 我有一个数据框A,其数字列如下:

zip code
00601
00602
00607

and so on. 等等。

If I read this in R using read.csv, they are read as numeric entities. 如果我使用read.csv在R中读取它,它们将被读作数字实体。 I want them as factors. 我想要它们作为因素。

I tried converting them back to factor using 我尝试将它们转换回因子使用

A <- as.factor(A)

But this removes starting zeroes and make A like 但这会删除起始零并使A像

zip code
601
602
607

I do not want this. 我不想要这个。 I want to save zeroes. 我想保存零。

read.csv调用中使用colClasses以字符或因子读取它们: read.csv(*, colClasses="factor")

You may need to add leading zeros - as in this post . 您可能需要添加前导零 - 如本文所示 This first converts to a character class. 这首先转换为字符类。 Then, you can change this to a factor, which maintains the leading zeros. 然后,您可以将其更改为保持前导零的因子。

Example

A <- data.frame("zip code"=c(00601,00602,00607))
class(A$zip.code) #numeric
A$zip.code <- sprintf("%05d", A$zip.code)
class(A$zip.code) #character
A$zip.code <- as.factor(A$zip.code)
class(A$zip.code) #factor

Resulting in: 导致:

> A$zip.code
[1] 00601 00602 00607
Levels: 00601 00602 00607

Writing A as a .csv file A写为.csv文件

write.csv(A, "tmp.csv")

results in 结果是

"","zip.code"
"1","00601"
"2","00602"
"3","00607"

everything without any text qualifier is (attempted to be) read as numeric, so the issue is basically to know how your data (in case 00607 ) is stored on the flat text file. 没有任何文本限定符的所有内容都被(试图)读取为数字,因此问题基本上是知道您的数据(如果是00607 )是如何存储在平面文本文件中的。 If without text qualifier, you can either follow the suggestion of @Hong Ooi or use 如果没有文本限定符,您可以遵循@Hong Ooi的建议或使用

read.csv(*, colClasses="character")

and then convert each column accordingly (in case you don' want/need all of them to factor ). 然后相应地转换每一列(如果你不想要/需要所有这些factor )。 Once you have a character vector (a data.frame column) converting it to factor is just straightforward 一旦你有了一个字符向量(一个data.frame列),将它转换为factor只是简单明了

> zipCode <- c("00601", "00602", "00607")
> factor(zipCode)
[1] 00601 00602 00607
Levels: 00601 00602 00607

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM