gpt4 book ai didi

R:为什么我在将列转换为因子后没有得到类型或类 "factor"?

转载 作者:行者123 更新时间:2023-12-01 22:06:24 24 4
gpt4 key购买 nike

我有以下设置。

df <- data.frame(aa = rnorm(1000), bb = rnorm(1000))

apply(df, 2, typeof)
# aa bb
#"double" "double"

apply(df, 2, class)
# aa bb
#"numeric" "numeric"

然后我尝试将其中一列转换为“因子”。但是正如您在下面看到的,我没有得到任何“因素”类型或类别。我做错了什么吗?

df[, 1] <- as.factor(df[, 1])

apply(df, 2, typeof)
# aa bb
#"character" "character"

apply(df, 2, class)
# aa bb
#"character" "character"

最佳答案

抱歉,我觉得我原来的回答写得不好。为什么我把那个“因素矩阵”放在最开始?这是一个更好的尝试。

来自 ?apply:

 If ‘X’ is not an array but an object of a class with a non-null
‘dim’ value (such as a data frame), ‘apply’ attempts to coerce it
to an array via ‘as.matrix’ if it is two-dimensional (e.g., a data
frame) or via ‘as.array’.

因此,在按行或列应用 FUN 之前,通过 as.matrix 将数据框转换为矩阵。

来自 ?as.matrix:

 ‘as.matrix’ is a generic function.  The method for data frames
will return a character matrix if there is only atomic columns and
any non-(numeric/logical/complex) column, applying ‘as.vector’ to
factors and ‘format’ to other non-character columns. Otherwise,
the usual coercion hierarchy (logical < integer < double <
complex) will be used, e.g., all-logical data frames will be
coerced to a logical matrix, mixed logical-integer will give a
integer matrix, etc.

The default method for ‘as.matrix’ calls ‘as.vector(x)’, and hence
e.g. coerces factors to character vectors.

我的母语不是英语,我看不懂以下内容(这看起来很重要!)。有人可以澄清一下吗?

The method for data frames will return a character matrix if there is only atomic columns and any non-(numeric/logical/complex) column, applying ‘as.vector’ to factors and ‘format’ to other non-character columns.

来自 ?as.vector:

 Note that factors are _not_ vectors; ‘is.vector’ returns ‘FALSE’
and ‘as.vector’ converts a factor to a character vector for ‘mode
= "any"’.

简单地说,只要您在数据框中有一个因子列,as.matrix 就会为您提供一个字符矩阵。


我相信这个apply with data frame 问题已经被提出了很多次,上面只是添加了另一个重复的答案。真对不起。我没有仔细阅读 OP 的问题。首先让我感到震惊的是 R 无法构建真正的因子矩阵。

f <- factor(letters[1:4])

matrix(f, 2, 2)
# [,1] [,2]
#[1,] "a" "c"
#[2,] "b" "d"

## a sneaky way to get a matrix of factors by setting `dim` attribute
dim(f) <- c(2, 2)
# [,1] [,2]
#[1,] a c
#[2,] b d
#Levels: a b c d

is.matrix(f)
#[1] TRUE

class(f)
#[1] "factor" ## not a true matrix with "matrix" class

虽然这很有趣,但它应该与 OP 的问题不太相关。

再次抱歉把这里搞得一团糟。太糟糕了!!


So if I do sapply would it help? Because I have many columns that need to be converted to factor.

实际使用lapplysapply 会将结果简化为一个数组,在二维情况下是一个矩阵。这是一个例子:

dat <- head(trees)
sapply(dat, as.factor)
# Girth Height Volume
#[1,] "8.3" "70" "10.3"
#[2,] "8.6" "65" "10.3"
#[3,] "8.8" "63" "10.2"
#[4,] "10.5" "72" "16.4"
#[5,] "10.7" "81" "18.8"
#[6,] "10.8" "83" "19.7"

new_dat <- data.frame(lapply(dat, as.factor))
str(new_dat)
#'data.frame': 6 obs. of 3 variables:
# $ Girth : Factor w/ 6 levels "8.3","8.6","8.8",..: 1 2 3 4 5 6
# $ Height: Factor w/ 6 levels "63","65","70",..: 3 2 1 4 5 6
# $ Volume: Factor w/ 5 levels "10.2","10.3",..: 2 2 1 3 4 5

sapply(new_dat, class)
# Girth Height Volume
#"factor" "factor" "factor"

apply(new_dat, 2, class)
# Girth Height Volume
#"character" "character" "character"

关于typeof,因子实际上是以整数形式存储的。

sapply(new_dat, typeof)
# Girth Height Volume
#"integer" "integer" "integer"

当您输入一个因素时,您可以看到这一点。例如:

dput(new_dat[[1]])
#structure(1:6, .Label = c("8.3", "8.6", "8.8", "10.5", "10.7",
#"10.8"), class = "factor")

实际值为 1:6。角色等级只是一个属性。

关于R:为什么我在将列转换为因子后没有得到类型或类 "factor"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51582258/

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