在 R 中将矩阵转换为数据框时出现问题(R 认为所有数字类型都是因子)

发布于 2024-09-11 16:00:29 字数 535 浏览 1 评论 0原文

我通过 COM 接口将数据从 C# 传递到 R。当数据到达 R 时,它被存储在“矩阵”中。我使用的一些函数要求数据位于“DataFrame”内。我使用数据表达到 R 来转换数据结构

newDataFrame <- as.data.frame(oldMatrix)

就好了,但是一旦我转换为 DataFrame,它就会假设我的所有数字数据都是因子!

所以它变成: {34, 46, 90, 54, 69, 54} 变成 {1, 2, 3, 4, 5, 4}

我的数据表确实有因子,所以我不能强迫整个东西是数字的。有什么办法解决这个问题吗?注意:我无法将数据以 CSV 格式导出到文件系统并手动将其读入 R。

顺便说一句,我使用的需要 DataFrame 的函数是“Hmisc”包,使用

hist.data.frame(dataFrame)

它为 DataFram 中的每一列数据生成频率直方图,并将它们全部排列在网格模式中(非常漂亮)!

谢谢! -戴夫

I am passing data from C# to R over a COM interface. When the data arrives in R it is housed in a 'Matrix'. Some of the functions that I use require that the data be inside a 'DataFrame' instead. I convert the data structure using

newDataFrame <- as.data.frame(oldMatrix)

The table of data reaches R just fine, once I make the conversion to the DataFrame however, it assumes all of my numeric data are factors!

So it turns: {34, 46, 90, 54, 69, 54} into {1, 2, 3, 4, 5, 4}

My data table DOES have factors in it though, so I just can't force the whole thing to be numeric. Is there any way around this? Note: I can't export the data as a CSV onto the filesystem and read it into R manually.

On a side note, the function I am using that requires a DataFrame is the 'Hmisc' package using

hist.data.frame(dataFrame)

this produces a frequency histogram for every column of data in the DataFram and arranges them in all in a grid pattern (quite nifty)!

Thanks!
-Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳钉梦 2024-09-18 16:00:29

我认为您错误地诊断了问题 - 矩阵中的所有列必须属于同一类型,因此这可能是问题出现的地方,而不是转换为数据框的地方。

I think you have mis-diagnosed the problem - all columns in a matrix must be of the same type, so this is likely to be where the problem arises, not the conversion to a data frame.

我要还你自由 2024-09-18 16:00:29

我以前也遇到过这个问题。读取数据时需要设置stringsAsFactors=F。

现在,您可以将各个变量/列转换为因子(即使用 as.numeric() 等),而不必担心如何处理数字。

I've had this problem before. You need to set stringsAsFactors=F when you read the data.

Now, you can convert individual variables/columns to factors (ie, with as.numeric() and the like), without worrying about how the numbers are treated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文