简体   繁体   English

像在R中使用readxl一样读取Excel数据

[英]Read excel data as is using readxl in R

I have to read an excel file in R. The excelfile has a column with values such as 50%,20%... and another column with dates in the format "12-December-2017" but R converts both the column datas. 我必须在R中读取一个excel文件。excelfile中有一列的值例如50%,20%...,另一列的日期格式为“ 12-December-2017”,但是R会转换这两个列数据。

I am using readxl package and i specified in col_types parameter all the columns to be read as text but when i check the dataframe all the column types are characters but the percentage data and date changes to decimals and numbers respectively. 我正在使用readxl包,并且我在col_types参数中指定了所有要读取为文本的列,但是当我检查数据帧时,所有列类型都是字符,但百分比数据和日期分别变为小数和数字。

 excelfile2<-read_excel(filePath,col_types=rep("text",8))

I want to read the excel file as is.Any help will be appreciated. 我想按原样读取Excel文件。任何帮助将不胜感激。

This is because what you visualize inside the Excel is not what actually is stored. 这是因为您在Excel中可视化的不是实际存储的内容。
For example, if in excel you visualize "12-December-2017", what is stored in reality is the number of days since 1-1-1899. 例如,如果在excel中您将“ 12-December-2017”可视化,则实际存储的是从1-1-1899开始的天数。

My suggestion is to open the Excel file with the TextReader so you have a grasp what really you are reading in R. 我的建议是使用TextReader打开Excel文件,这样您就可以真正掌握R中正在阅读的内容。

Then, you can either define everything as text in excel or you can apply some transformations in R in order to convert the days since 1-1-1899 into a POSIXct format. 然后,您可以在excel中将所有内容定义为文本,也可以在R中应用一些转换,以将1-1-1899年以来的日期转换为POSIXct格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM