R将dataframe列写入具有前导零的csv [英] R write dataframe column to csv having leading zeroes

查看:178
本文介绍了R将dataframe列写入具有前导零的csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,用于存储不同长度的前缀. 表格片段(ClusterTable)

I have a table that stores prefixes of different lengths.. snippet of table(ClusterTable)

ClusterTable [ClusterTable $ FeatureIndex =="Prefix2",'FeatureIndex', 'FeatureValue')]

ClusterTable[ClusterTable$FeatureIndex == "Prefix2",'FeatureIndex', 'FeatureValue')]

   FeatureIndex FeatureValue
80      Prefix2           80
81      Prefix2           81
30      Prefix2           30
70      Prefix2           70
51      Prefix2           51
84      Prefix2           84
01      Prefix2           01
63      Prefix2           63
28      Prefix2           28
26      Prefix2           26
65      Prefix2           65
75      Prefix2           75

然后我使用以下命令写入csv文件:

and I write to csv file using following:

write.csv(ClusterTable, file = "My_Clusters.csv")

特征值01失去了它的前导零.

The Feature Value 01 loses it leading zero.

我首先尝试将列转换为字符

I tried first converting the column to characters

ClusterTable$FeatureValue <- as.character(ClusterTable$FeatureValue)

,并且还尝试将其附加到一个空字符串以在写入文件之前将其转换为字符串.

and also tried to append it to an empty string to convert it to string before writing to file.

ClusterTable$FeatureValue <- paste("",ClusterTable$FeatureValue)

此外,我在此表中具有各种长度的前缀,因此我不能使用固定长度的简单格式说明符.即该表还具有值001(Prefix3),0001(Prefix4)等. 谢谢

Also, I have in this table prefixes of various lengths, so I cant use simple format specifier of a fixed length. i.e the table also has Value 001(Prefix3),0001(Prefix4),etc. Thanks

推荐答案

在处理前导零时,如果导出到excel,则需要谨慎. Excel趋向于超越自身并自动修剪前导零.否则,您可以编写代码,并且在任何其他文本编辑器中打开文件都应显示零.

When dealing with leading zeros you need to be cautious if exporting to excel. Excel has a tendency to outsmart itself and automatically trim leading zeros. You code is fine otherwise and opening the file in any other text editor should show the zeros.

这篇关于R将dataframe列写入具有前导零的csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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