我尝试在保留行名的同时将矩阵写入 csv (参见r 中的导出矩阵)。
但是,当我使用 write.table() 执行此操作时,所有列都会向左移动(因此第一个数据列标题出现在 rownames 列上方)。
"PC1","PC2","PC3","PC4"
"Murder",0.0417043206282872,-0.04482165626967,0.0798906594208106,-0.994921731246978
"Assault",0.995221281426497,-0.0587600278572231,-0.0675697350838042,0.03893829763516
"UrbanPop",0.0463357461197111,0.976857479909889,-0.200546287353865,-0.0581691430589317
"Rape",0.0751555005855469,0.200718066450337,0.974080592182492,0.0723250196376096
我尝试了以下方法(手动添加额外的列):
merged.pca <- prcomp(USArrests)
write.table(merged.pca$rotation, file = "rotation.csv", sep = ",", col.names = c("rowname",colnames(merged.pca$rotation)))
不幸的是,这失败了:
Error in write.table(merged.pca$rotation, file = "rotation.csv", sep = ",", :
invalid 'col.names' specification
TBH 我不知道这个错误是什么意思。参数是列表而不是向量吗?