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

问题描述

我正在尝试使用 xlsxXLConnect 包将 Excel 2010 工作簿文件中的工作表读入 R.尽管将工作表中的单元格格式化为文本",但两者都在邮政编码列上删除了前导零.

I am trying to read a worksheet in an Excel 2010 workbook file into R using both the xlsx and XLConnect packages. Both are dropping leading zeroes on zip code columns despite formatting the cells in the worksheet as 'Text'.

wb <- loadWorkbook('c:/~/file1.xlsx')
sheetNames <- getSheets(wb)

for(i in 1:length(sheetNames)){  # i = 2
    #dat1 <- read.xlsx('c:/~/file1.xlsx', sheetNames[i], as.data.frame = T)
    dat1 <-  readWorksheetFromFile('c:/~/file1.xlsx', sheetNames[i])
}

有人对如何处理这个问题有建议吗?

Does anyone have suggestions for how to deal with this?

推荐答案

您可以将列类型指定为 readWorksheet()readWorksheetFromFile() 的参数,通过使用参数 colType = ....

You can specify the column types as an argument to readWorksheet() or readWorksheetFromFile(), by using the argument colType = ....

例如,要将所有列读取为字符,请使用:

For example, to read all columns as character, use:

readWorkSheet(..., colType="character")

这篇关于R/Excel:前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:58