无法强制类typeOf是data.frame的两倍

时间:2014-08-12 18:47:26

标签: r statistics

我想将pcomp()函数的输出转换为问题所在的数据框:

使用as.data.frame时,我得到:

Error in as.data.frame.default(data.pca1$loadings) : 
  cannot coerce class ""loadings"" to a data.frame

我的问题是我不知道另一种将数据转换为dataframe的方式。

我的数据:

dput(data.pca1$loading)
structure(c(0.629999587107591, 0.544821342951502, 0.757214347947699, 
0.140799724239407, 0.195350889504226, 0.30782155605534, 0.113779470611058, 
0.25319481053313, 0.530575304369119, 0.161372414349993, 0.172071942829558, 
0.181110279027528, 0.32087938033537, 0.199923250086931, 0.112976439383677, 
0.435960093914842, 0.735714667273012, 0.546839296993964, 0.618721696774699, 
0.157746933602662, 0.196394937008026, 0.323352336171706, 0.312677801641149, 
0.0540081734469586, 0.236882360216423, 0.160496430572191, 0.126509131465792, 
0.160239501804437, 0.28141267308665, 0.286102918406781, 0.59838483667591, 
0.588492755323989, 0.597607970656895, -0.00300183993888212, 0.132863946525179, 
0.3091527351007, 0.967808869898446, 0.460034358552482, 0.0613549566320599, 
0.0823819988685729, 0.243877077120173, 0.0998230648040528, 0.0718231415302767, 
0.241248015517299), .Dim = c(11L, 4L), .Dimnames = list(c("Q1", 
"Q2", "Q3", "Q4", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", "Q12"
), c("Factor1", "Factor2", "Factor3", "Factor4")), class = "loadings")

我真的很感激任何想法!

1 个答案:

答案 0 :(得分:2)

解决方案很简单。

as.data.frame(unclass(data.pca1$loadings))

解释也很简单。默认情况下,类加载的对象无法转换为data.frame。因此,您可以使用unclass语句删除对象的类,并且它被强制转换为矩阵,您可以将其转换为data.frame。