楼主: 耕耘使者
6589 5

[问答] read.table能否读入2007或2003excel文件? [推广有奖]

贵宾

已卖:5006份资源

学术权威

39%

还不是VIP/贵宾

-

威望
4
论坛币
1811021 个
通用积分
171.7911
学术水平
109 点
热心指数
173 点
信用等级
87 点
经验
93428 点
帖子
4549
精华
0
在线时间
2848 小时
注册时间
2006-4-6
最后登录
2025-7-12

楼主
耕耘使者 发表于 2014-2-5 19:15:45 |AI写论文
100论坛币
shangzheng.xlsx (73.91 KB)
上面是数据文件。
代码:
  1. read.table("shangzheng.xlsx",header=T)
复制代码
结果:
TT截图未命名.jpg


我尝试另存为2003excel文件,打开时,提示:
Error in make.names(col.names, unique = TRUE) :
  invalid multibyte string 1
In addition: Warning message:
In read.table("shangzheng.xls", header = T) :
  incomplete final line found by readTableHeader on 'shangzheng.xls'

最佳答案

kaifengedu 查看完整内容

建议使用xlsx包。输出为:
关键词:Table EXCEL ABLE READ exce excel

沙发
kaifengedu 发表于 2014-2-5 19:15:46
建议使用xlsx包。
  1. library(xlsx)
  2. Data <- read.xlsx(
  3.   "shangzheng.xlsx",
  4.   sheetIndex = 1,
  5.   startRow = 1,
  6. )
  7. head(Data)
复制代码
输出为:
  1. > head(Data)
  2.         time      kp     max     min      sp
  3. 1 2012-07-11 2160.02 2176.49 2156.60 2175.38
  4. 2 2012-07-12 2172.06 2197.04 2152.67 2185.49
  5. 3 2012-07-13 2180.17 2198.66 2177.92 2185.90
  6. 4 2012-07-16 2187.89 2188.06 2146.15 2147.96
  7. 5 2012-07-17 2142.43 2165.33 2141.48 2161.19
  8. 6 2012-07-18 2159.86 2170.30 2138.79 2169.10
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
耕耘使者 + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

藤椅
dxystata 发表于 2014-2-5 20:45:06
1. Save Excel files into text
Saving Excel files into CSV can be done directly from Excel or through some external tools that allows batch operations. Native R functions for text data import can so be used.
  1. df = read.table("myfile.csv", header = TRUE)
复制代码
2. Copy and paste from Excel to R
This is a fast solutions, but it has one main drawbacks: it requires to open Excel file, select data and copy. By the way, this is the best compromise when you're in a hurry.
  1. df = read.table("clipboard")
复制代码
3. ODBC connection
  1. require(RODBC)
  2. conn = odbcConnectExcel("myfile.xlsx") # open a connection to the Excel file
  3. sqlTables(conn)$TABLE_NAME # show all sheets
  4. df = sqlFetch(conn, "Sheet1") # read a sheet
  5. df = sqlQuery(conn, "select * from [Sheet1 $]") # read a sheet (alternative SQL sintax)
  6. close(conn) # close the connection to the file
复制代码
4. gdata package
  1. require(gdata)
  2. df = read.xls ("myfile.xlsx"), sheet = 1, header = TRUE)
复制代码
5. xlsReadWrite package
  1. require(xlsReadWrite)
  2. xls.getshlib()
  3. df = read.xls("myfile.xls", sheet = 1)
复制代码
6. XLConnect package
  1. require(XLConnect)
  2. wb = loadWorkbook("myfile.xlsx")
  3. df = readWorksheet(wb, sheet = "Sheet1", header = TRUE)
复制代码
7.xlsx package
  1. require(xlsx)
  2. read.xlsx("myfile.xlsx", sheetName = "Sheet1")
  3. read.xlsx2("myfile.xlsx", sheetName = "Sheet1")
复制代码
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
求证1加1 + 1 + 1 + 1 热心帮助其他会员
耕耘使者 + 2 + 2 + 1 精彩帖子

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 2   查看全部评分

板凳
耕耘使者 发表于 2014-2-5 20:54:22
kaifengedu 发表于 2014-2-5 19:15
建议使用xlsx包。输出为:
非常感谢!

报纸
kaifengedu 发表于 2014-2-5 21:17:50
耕耘使者 发表于 2014-2-5 20:54
非常感谢!

地板
耕耘使者 发表于 2014-2-5 23:25:58
dxystata 发表于 2014-2-5 20:45
1. Save Excel files into text
Saving Excel files into CSV can be done directly from Excel or throug ...
精彩!
多谢兄弟!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 03:44