如何将主要因素的数据框转换为数值矩阵;取消列表不起作用
我在将 data.frame
转换为矩阵格式时遇到问题。因为我想将主要包含因子变量的 data.frame 更改为数字矩阵,所以我使用了以下代码
UN2010frame <- data.matrix(lapply(UN2010, as.numeric))
,但是当我检查 UN2010frame
的模式时,它仍然显示为列表。由于我要运行的代码 (OrdRating) 不接受列表格式的数据,因此我使用 UN2010matrix <- unlist(UN2010frame)
取消列出我的矩阵。当我这样做时,我的第一行(以前是带有列名称的行)变成了 NA。这对我来说是一个问题,因为当我尝试使用此数据集运行序数 IRT 模型时,我收到以下错误消息。
> Error in 1:nrow(Y) : argument of
> length 0
我认为这是因为我第一行中的所有值现在都消失了。
如果您能在任何方面帮助我,我将不胜感激。 非常感谢!
海莉
I am having trouble turning my data.frame
into a matrix format. Because I wanted to change my data.frame with mostly factor variables into a numeric matrix, I used the following code
UN2010frame <- data.matrix(lapply(UN2010, as.numeric))
However when I checked the mode of the UN2010frame
, it still showed up as a list. Because the code I want to run (Ordrating) does not accept data in a list format, I used UN2010matrix <- unlist(UN2010frame)
to unlist my matrix. When I did this, my first row ( which was formerly a row with column names) turned into NAs. This was a problem for me because when I tried to run an ordinal IRT model using this data set, I got the following error message.
> Error in 1:nrow(Y) : argument of
> length 0
I think it is because all the values in my first row are now gone.
If you could help me on any front, It would be deeply appreciated.
Thank you very much!
Haillie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,data.matrix 的正确用法是:
因为它会自动转换为数字。代码中的
lapply
是您收到的错误的第一个来源。您将列表放入 data.matrix 函数中,而不是数据帧中。所以它返回一个矩阵列表,而不是一个矩阵。其次,
unlist
返回一个向量,而不是矩阵。所以很确定你不会找到“第一行带有 NA”,因为你有一个向量。这可能可以解释您的部分困惑。您可能在某处有一个字符列。将其转换为数字给出 NA。如果您不希望这样,请将它们排除在进一步分析之外。一种可能性是使用
plyr
包中的colwise()
仅转换因子:它返回仅包含因子的数据帧。这可以通过
data.matrix()
或as.matrix()
轻松转换。或者,您使用基本解决方案:它将返回一个矩阵,其中所有非字符列都转换为数字。如果您确实想保留包含所有原始列的数据框,您可以执行以下操作:
玩具示例代码:
First, the correct use of data.matrix is :
as it converts automatically to numeric. The
lapply
in your code is the first source for the error you get. You put a list in thedata.matrix
function, not a dataframe. So it returns a list of matrices, and not a matrix.Second,
unlist
returns a vector, not a matrix. So pretty sure you won't find a "first row with NA", as you have a vector. Which might explain part of your confusion.You probably have a character column somewhere. Converting this to numeric gives NA. If you don't want this, then exclude them from the further analysis. One possibility is to use
colwise()
from theplyr
package to convert only the factors:Which returns a dataframe with only the factors. This can be easily converted by
data.matrix()
oras.matrix()
. Alternatively you use the base solution :which will return you a matrix with all non-character columns converted to numeric.If you really want to keep the dataframe with all original columns, you can do :
Toy example code :
尝试使用
as.data.frame
而不是data.matrix
。Try
as.data.frame
instead ofdata.matrix
.