为什么使用 table() 后浮点数被视为整数? [英] Why after using table() floating numbers are considered as integers?

查看:25
本文介绍了为什么使用 table() 后浮点数被视为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 double 类型的浮点数向量:

I have a vector of float numbers of type double :

> typeof(globals$out$data$randrating)
[1] "double"

当我把它变成表格后,相同的值返回一个整数:

And after I turn it into a table, the same values return an integer:

> data_rating <- as.data.frame(table(globals$out$data$randrating))
> colnames(data_rating) <- c("rating", "freq")
> typeof(data_rating$rating)
[1] "integer"

在 data_rating 中查看数据的样子:

To see what the data look like in data_rating :

> data_rating

   | rating | freq
1  |      4 |  312
2  |    7.1 |  324
3  |      8 |  340
4  |    8.5 |  962
5  |    8.7 | 1640

有什么想法吗?

推荐答案

原因是因为第一列是factor 而我们使用typeof看到的是整数表示.您可以使用 class 来查找实际的 class.

The reason is because the first column is factor and what we see by using typeof is the integer representation. You can use class to find the actual class.

如果我们查看 table 输出,names 部分是一个 character 向量.当我们使用 as.data.frame 将其转换为 data.frame 时,character 元素被转换为 factor对于 data.frame 调用,默认情况下 stringsAsFactors=TRUE.如果我们使用as.data.frame(...., stringsAsFactors=FALSE),它会改变typeof.

If we look at the table output, the names part is a character vector. When we convert it to data.frame using as.data.frame, the character elements gets converted to factor as by default stringsAsFactors=TRUE for the data.frame call. If we use as.data.frame(...., stringsAsFactors=FALSE), it would change the typeof.

作为一个可重复的例子,

As a reproducible example,

 set.seed(24)
 v1 <- sample(c(1:20, 14.5, 18.2), 200, replace=TRUE)
 tbl <- table(v1)
 str(tbl)
 # 'table' int [1:22(1d)] 7 9 10 11 12 10 7 10 13 6 ...
 #- attr(*, "dimnames")=List of 1
 # ..$ v1: chr [1:22] "1" "2" "3" "4" ...

 d1 <- as.data.frame(tbl)
 str(d1)
 # 'data.frame':   22 obs. of  2 variables:
 # $ v1  : Factor w/ 22 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
 # $ Freq: int  7 9 10 11 12 10 7 10 13 6 ...

 class(d1$v1)
 #[1] "factor"
 typeof(d1$v1)
 #[1] "integer"
 mode(d1$v1)
 #[1] "numeric"

使用 stringsAsFactors=FALSE

 d2 <- as.data.frame(tbl, stringsAsFactors=FALSE)
 class(d2$v1)
 #[1] "character"
 typeof(d2$v1)
 #[1] "character"
 mode(d2$v1)
 #[1] "character"

存储mode是前一个的numeric,这个是character.

The storage mode is numeric for the previous one and it is character for this.

这篇关于为什么使用 table() 后浮点数被视为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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