R write dataframe column to csv having leading zeroes

后端 未结 7 1106
故里飘歌
故里飘歌 2020-12-21 04:21

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

ClusterTable[ClusterTable$FeatureIndex == \"Prefix2\",\'Feat

7条回答
  •  情话喂你
    2020-12-21 04:55

    I know this is an old question, but I happened upon a solution for keeping the lead zeroes when opening .csv output in excel. Before writing your .csv in R, add an apostrophe at the front of each value like so:

    vector <- sapply(vector, function(x) paste0("'", x))
    

    When you open the output in excel, the apostrophe will tell excel to keep all the characters and not drop lead zeroes. At this point you can format the column as "text" and then do a find and replace to remove the apostrophes (maybe make a macro for this).

提交回复
热议问题