意外转换为字符而不是数据框和矩阵中的因子 [英] Unexpected conversion to chars instead of factors in data frames and matrices

查看:10
本文介绍了意外转换为字符而不是数据框和矩阵中的因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 R 的新手用户,但以下内容最令人困惑.

I am not a novice user of R, but the following is most confusing.

我有一个数据框(尽管问题同样存在于矩阵中)的分类变量采用值 +1/-1,我想将其转换为因子.

I have a data frame (although the problem is equally present for matrices) of categorical variables taking the values +1/-1, which I'd like to convert into factors.

mat <- matrix(sample(c(-1, +1), 16, replace = T), nrow = 4)
mat <- data.frame(mat)

然而,使用

mat <- apply(mat, 2, factor)

将整数转换为字符而不是因子:

turns integers into characters instead of factors:

> mat 
     [,1] [,2] [,3] [,4]
[1,] "-1" "1"  "-1" "1" 
[2,] "-1" "-1" "-1" "-1"
[3,] "-1" "1"  "1"  "1" 
[4,] "-1" "-1" "1"  "1" 

也许在同样的情况下(我的一些其他数据也遇到了这种问题)试图将矩阵和数据框中的字符名称转换为因子会导致更混乱的行为:

Perhaps in the same vein (and I had a problem of this sort with some of my other data) trying to convert character names in matrices and data frames into factors results in more confusing behaviour:

 mat2 <- matrix(sample(letters, 16, replace = T), nrow = 4)
 > mat2
     [,1] [,2] [,3] [,4]
 [1,] "x"  "m"  "r"  "e" 
 [2,] "u"  "r"  "b"  "p" 
 [3,] "j"  "p"  "h"  "j" 
 [4,] "k"  "s"  "e"  "x" 

mat2[,1] <- factor(mat2[,1])
> mat2
     [,1] [,2] [,3] [,4]
 [1,] "4"  "m"  "r"  "e" 
 [2,] "3"  "r"  "b"  "p" 
 [3,] "1"  "p"  "h"  "j" 
 [4,] "2"  "s"  "e"  "x" 

任何帮助或澄清将不胜感激.

any help or clarification would be appreciated.

推荐答案

永远记住数据框是列表,因此对列的操作就像迭代列表的元素一样.我想也许你打算做更多这样的事情:

Always remember that data frames are lists, and so operating on columns is just like iterating over elements of a list. I think maybe you intended to do something more like this:

mat[] <- lapply(mat,factor)

或者这个:

as.data.frame(lapply(mat,factor))

尽管在这里,请注意,每个因素的水平都不相同!

Although even here, note that the levels of each factor are not the same!

这篇关于意外转换为字符而不是数据框和矩阵中的因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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