R语言作为一个专业的统计软件,具有很多从外部导入数据的方法,
下面具体学习总结和分享一下:
1、读取txt文件
data=read.table(文件,--------);后面有很多可选择的参数,就不一一解释,大家可以使用help(read.table)查看相应的帮助
data=read.table("C:/Users/Administrator/Desktop/spss/数据/SPSS练习数据/data02-02a.txt") data V1 V2 V3 V4 V5 1 6 0 10 1.46 38 2 15 0 10 1.48 39 3 4 0 11 1.52 42 4 3 0 11 1.55 44 5 11 1 11 1.55 55 6 18 1 11 1.56 48
2、读取csv文件
同理,data=read.csv(文件,------------);后面是可选参数
data1=read.csv("C:/Users/Administrator/Desktop/spss/数据/SPSS练习数据.csv") data1 X2008.1.7 X11.97 X12.14 X11.7 X11.9 X11.9.1 1 2008/1/8 11.90 12.75 11.80 12.21 12.21 2 2008/1/9 12.10 13.03 12.05 12.94 12.94 3 2008/1/10 13.04 13.32 12.71 13.10 13.10 4 2008/1/11 13.15 14.15 13.15 13.93 13.93 5 2008/1/14 13.96 14.45 13.60 14.03 14.03 6 2008/1/15 14.00 14.18 13.70 13.97 13.97 7 2008/1/16 13.80 13.85 13.11 13.32 13.32 8 2008/1/18 13.27 13.73 12.20 12.80 12.80 9 2008/1/21 12.75 13.34 12.35 13.22 13.22 10 2008/1/22 13.08 13.20 11.90 11.90 11.90 11 2008/1/23 11.00 11.40 10.71 10.71 10.71 12 2008/1/24 11.30 11.47 11.00 11.17 11.17 13 2008/1/7 11.97 12.14 11.70 11.90 11.90 14 2008/1/8 11.90 12.75 11.80 12.21 12.21 15 2008/1/9 12.10 13.03 12.05 12.94 12.94 16 2008/1/10 13.04 13.32 12.71 13.10 13.10 17 2008/1/11 13.15 14.15 13.15 13.93 13.93 18 2008/1/14 13.96 14.45 13.60 14.03 14.03 19 2008/1/15 14.00 14.18 13.70 13.97 13.97 20 2008/1/16 13.80 13.85 13.11 13.32 13.32 21 2008/1/18 13.27 13.73 12.20 12.80 12.80 22 2008/1/21 12.75 13.34 12.35 13.22 13.22 23 2008/1/22 13.08 13.20 11.90 11.90 11.90 24 2008/1/23 11.00 11.40 10.71 10.71 10.71 25 2008/1/24 11.30 11.47 11.00 11.17 11.17 26 2008/1/7 11.97 12.14 11.70 11.90 11.90 27 2008/1/8 11.90 12.75 11.80 12.21 12.21 28 2008/1/9 12.10 13.03 12.05 12.94 12.94 29 2008/1/10 13.04 13.32 12.71 13.10 13.10 30 2008/1/11 13.15 14.15 13.15 13.93 13.93 31 2008/1/14 13.96 14.45 13.60 14.03 14.03 32 2008/1/15 14.00 14.18 13.70 13.97 13.97 33 2008/1/16 13.80 13.85 13.11 13.32 13.32 34 2008/1/18 13.27 13.73 12.20 12.80 12.80 35 2008/1/21 12.75 13.34 12.35 13.22 13.22 36 2008/1/22 13.08 13.20 11.90 11.90 11.90 37 2008/1/23 11.00 11.40 10.71 10.71 10.71 38 2008/1/24 11.30 11.47 11.00 11.17 11.17 39 2008/1/7 11.97 12.14 11.70 11.90 11.90 40 2008/1/8 11.90 12.75 11.80 12.21 12.21 41 2008/1/9 12.10 13.03 12.05 12.94 12.94 42 2008/1/10 13.04 13.32 12.71 13.10 13.10 43 2008/1/11 13.15 14.15 13.15 13.93 13.93 44 2008/1/14 13.96 14.45 13.60 14.03 14.03 45 2008/1/15 14.00 14.18 13.70 13.97 13.97 46 2008/1/16 13.80 13.85 13.11 13.32 13.32 47 2008/1/18 13.27 13.73 12.20 12.80 12.80 48 2008/1/21 12.75 13.34 12.35 13.22 13.22 49 2008/1/22 13.08 13.20 11.90 11.90 11.90 50 2008/1/23 11.00 11.40 10.71 10.71 10.71 51 2008/1/24 11.30 11.47 11.00 11.17 11.17 52 2008/1/7 11.97 12.14 11.70 11.90 11.90 53 2008/1/8 11.90 12.75 11.80 12.21 12.21 54 2008/1/9 12.10 13.03 12.05 12.94 12.94 55 2008/1/10 13.04 13.32 12.71 13.10 13.10 56 2008/1/11 13.15 14.15 13.15 13.93 13.93 57 2008/1/14 13.96 14.45 13.60 14.03 14.03 58 2008/1/15 14.00 14.18 13.70 13.97 13.97 59 2008/1/16 13.80 13.85 13.11 13.32 13.32 60 2008/1/18 13.27 13.73 12.20 12.80 12.80 61 2008/1/21 12.75 13.34 12.35 13.22 13.22 62 2008/1/22 13.08 13.20 11.90 11.90 11.90 63 2008/1/23 11.00 11.40 10.71 10.71 10.71 64 2008/1/24 11.30 11.47 11.00 11.17 11.17 65 2008/1/7 11.97 12.14 11.70 11.90 11.90 66 2008/1/8 11.90 12.75 11.80 12.21 12.21 67 2008/1/9 12.10 13.03 12.05 12.94 12.94 68 2008/1/10 13.04 13.32 12.71 13.10 13.10 69 2008/1/11 13.15 14.15 13.15 13.93 13.93 70 2008/1/14 13.96 14.45 13.60 14.03 14.03 71 2008/1/15 14.00 14.18 13.70 13.97 13.97 72 2008/1/16 13.80 13.85 13.11 13.32 13.32 73 2008/1/18 13.27 13.73 12.20 12.80 12.80 74 2008/1/21 12.75 13.34 12.35 13.22 13.22 75 2008/1/22 13.08 13.20 11.90 11.90 11.90 76 2008/1/23 11.00 11.40 10.71 10.71 10.71 77 2008/1/24 11.30 11.47 11.00 11.17 11.17
3、读取excel文件(xls)
data2=odbcConnectExcel("C:/Users/Administrator/Desktop/spss/数据/SPSS练习数据.xls") #只能使用32位的windows系统;Error in data2=odbcConnectExcel2007("C:/Users/Administrator/Desktop/spss/数据/SPSS练习数据.xls") data2 df=sqlTables(data2) table_test <- sqlFetch(data2, df$TABLE_NAME[1]) table_test 2008 1 7 星期一 11#97 12#14 11#7 11#9 11#91 1 2008-01-08 11.90 12.75 11.80 12.21 12.21 2 2008-01-09 12.10 13.03 12.05 12.94 12.94 3 2008-01-10 13.04 13.32 12.71 13.10 13.10 4 2008-01-11 13.15 14.15 13.15 13.93 13.93 5 2008-01-14 13.96 14.45 13.60 14.03 14.03 6 2008-01-15 14.00 14.18 13.70 13.97 13.97 7 2008-01-16 13.80 13.85 13.11 13.32 13.32 8 2008-01-18 13.27 13.73 12.20 12.80 12.80 9 2008-01-21 12.75 13.34 12.35 13.22 13.22 10 2008-01-22 13.08 13.20 11.90 11.90 11.90 11 2008-01-23 11.00 11.40 10.71 10.71 10.71 12 2008-01-24 11.30 11.47 11.00 11.17 11.17 13 2008-01-07 11.97 12.14 11.70 11.90 11.90 14 2008-01-08 11.90 12.75 11.80 12.21 12.21 15 2008-01-09 12.10 13.03 12.05 12.94 12.94 16 2008-01-10 13.04 13.32 12.71 13.10 13.10 17 2008-01-11 13.15 14.15 13.15 13.93 13.93 18 2008-01-14 13.96 14.45 13.60 14.03 14.03 19 2008-01-15 14.00 14.18 13.70 13.97 13.97 20 2008-01-16 13.80 13.85 13.11 13.32 13.32 21 2008-01-18 13.27 13.73 12.20 12.80 12.80 22 2008-01-21 12.75 13.34 12.35 13.22 13.22 23 2008-01-22 13.08 13.20 11.90 11.90 11.90 24 2008-01-23 11.00 11.40 10.71 10.71 10.71 25 2008-01-24 11.30 11.47 11.00 11.17 11.17 26 2008-01-07 11.97 12.14 11.70 11.90 11.90 27 2008-01-08 11.90 12.75 11.80 12.21 12.21 28 2008-01-09 12.10 13.03 12.05 12.94 12.94 29 2008-01-10 13.04 13.32 12.71 13.10 13.10 30 2008-01-11 13.15 14.15 13.15 13.93 13.93 31 2008-01-14 13.96 14.45 13.60 14.03 14.03 32 2008-01-15 14.00 14.18 13.70 13.97 13.97 33 2008-01-16 13.80 13.85 13.11 13.32 13.32 34 2008-01-18 13.27 13.73 12.20 12.80 12.80 35 2008-01-21 12.75 13.34 12.35 13.22 13.22 36 2008-01-22 13.08 13.20 11.90 11.90 11.90 37 2008-01-23 11.00 11.40 10.71 10.71 10.71 38 2008-01-24 11.30 11.47 11.00 11.17 11.17 39 2008-01-07 11.97 12.14 11.70 11.90 11.90 40 2008-01-08 11.90 12.75 11.80 12.21 12.21 41 2008-01-09 12.10 13.03 12.05 12.94 12.94 42 2008-01-10 13.04 13.32 12.71 13.10 13.10 43 2008-01-11 13.15 14.15 13.15 13.93 13.93 44 2008-01-14 13.96 14.45 13.60 14.03 14.03 45 2008-01-15 14.00 14.18 13.70 13.97 13.97 46 2008-01-16 13.80 13.85 13.11 13.32 13.32 47 2008-01-18 13.27 13.73 12.20 12.80 12.80 48 2008-01-21 12.75 13.34 12.35 13.22 13.22 49 2008-01-22 13.08 13.20 11.90 11.90 11.90 50 2008-01-23 11.00 11.40 10.71 10.71 10.71 51 2008-01-24 11.30 11.47 11.00 11.17 11.17 52 2008-01-07 11.97 12.14 11.70 11.90 11.90 53 2008-01-08 11.90 12.75 11.80 12.21 12.21 54 2008-01-09 12.10 13.03 12.05 12.94 12.94 55 2008-01-10 13.04 13.32 12.71 13.10 13.10 56 2008-01-11 13.15 14.15 13.15 13.93 13.93 57 2008-01-14 13.96 14.45 13.60 14.03 14.03 58 2008-01-15 14.00 14.18 13.70 13.97 13.97 59 2008-01-16 13.80 13.85 13.11 13.32 13.32 60 2008-01-18 13.27 13.73 12.20 12.80 12.80 61 2008-01-21 12.75 13.34 12.35 13.22 13.22 62 2008-01-22 13.08 13.20 11.90 11.90 11.90 63 2008-01-23 11.00 11.40 10.71 10.71 10.71 64 2008-01-24 11.30 11.47 11.00 11.17 11.17 65 2008-01-07 11.97 12.14 11.70 11.90 11.90 66 2008-01-08 11.90 12.75 11.80 12.21 12.21 67 2008-01-09 12.10 13.03 12.05 12.94 12.94 68 2008-01-10 13.04 13.32 12.71 13.10 13.10 69 2008-01-11 13.15 14.15 13.15 13.93 13.93 70 2008-01-14 13.96 14.45 13.60 14.03 14.03 71 2008-01-15 14.00 14.18 13.70 13.97 13.97 72 2008-01-16 13.80 13.85 13.11 13.32 13.32 73 2008-01-18 13.27 13.73 12.20 12.80 12.80 74 2008-01-21 12.75 13.34 12.35 13.22 13.22 75 2008-01-22 13.08 13.20 11.90 11.90 11.90 76 2008-01-23 11.00 11.40 10.71 10.71 10.71 77 2008-01-24 11.30 11.47 11.00 11.17 11.17
4、读取excel文件(xlsx)
使用xlsx包
library('xlsx') data3=read.xlsx("C:/Users/Administrator/Desktop/spss/数据/SPSS练习数据.xls",1) data3 X39454 X11.97 X12.14 X11.7 X11.9 X11.9.1 1 2008-01-08 11.90 12.75 11.80 12.21 12.21 2 2008-01-09 12.10 13.03 12.05 12.94 12.94 3 2008-01-10 13.04 13.32 12.71 13.10 13.10 4 2008-01-11 13.15 14.15 13.15 13.93 13.93 5 2008-01-14 13.96 14.45 13.60 14.03 14.03 6 2008-01-15 14.00 14.18 13.70 13.97 13.97 7 2008-01-16 13.80 13.85 13.11 13.32 13.32 8 2008-01-18 13.27 13.73 12.20 12.80 12.80 9 2008-01-21 12.75 13.34 12.35 13.22 13.22 10 2008-01-22 13.08 13.20 11.90 11.90 11.90 11 2008-01-23 11.00 11.40 10.71 10.71 10.71 12 2008-01-24 11.30 11.47 11.00 11.17 11.17 13 2008-01-07 11.97 12.14 11.70 11.90 11.90 14 2008-01-08 11.90 12.75 11.80 12.21 12.21 15 2008-01-09 12.10 13.03 12.05 12.94 12.94 16 2008-01-10 13.04 13.32 12.71 13.10 13.10 17 2008-01-11 13.15 14.15 13.15 13.93 13.93 18 2008-01-14 13.96 14.45 13.60 14.03 14.03 19 2008-01-15 14.00 14.18 13.70 13.97 13.97 20 2008-01-16 13.80 13.85 13.11 13.32 13.32 21 2008-01-18 13.27 13.73 12.20 12.80 12.80 22 2008-01-21 12.75 13.34 12.35 13.22 13.22 23 2008-01-22 13.08 13.20 11.90 11.90 11.90 24 2008-01-23 11.00 11.40 10.71 10.71 10.71 25 2008-01-24 11.30 11.47 11.00 11.17 11.17 26 2008-01-07 11.97 12.14 11.70 11.90 11.90 27 2008-01-08 11.90 12.75 11.80 12.21 12.21 28 2008-01-09 12.10 13.03 12.05 12.94 12.94 29 2008-01-10 13.04 13.32 12.71 13.10 13.10 30 2008-01-11 13.15 14.15 13.15 13.93 13.93 31 2008-01-14 13.96 14.45 13.60 14.03 14.03 32 2008-01-15 14.00 14.18 13.70 13.97 13.97 33 2008-01-16 13.80 13.85 13.11 13.32 13.32 34 2008-01-18 13.27 13.73 12.20 12.80 12.80 35 2008-01-21 12.75 13.34 12.35 13.22 13.22 36 2008-01-22 13.08 13.20 11.90 11.90 11.90 37 2008-01-23 11.00 11.40 10.71 10.71 10.71 38 2008-01-24 11.30 11.47 11.00 11.17 11.17 39 2008-01-07 11.97 12.14 11.70 11.90 11.90 40 2008-01-08 11.90 12.75 11.80 12.21 12.21 41 2008-01-09 12.10 13.03 12.05 12.94 12.94 42 2008-01-10 13.04 13.32 12.71 13.10 13.10 43 2008-01-11 13.15 14.15 13.15 13.93 13.93 44 2008-01-14 13.96 14.45 13.60 14.03 14.03 45 2008-01-15 14.00 14.18 13.70 13.97 13.97 46 2008-01-16 13.80 13.85 13.11 13.32 13.32 47 2008-01-18 13.27 13.73 12.20 12.80 12.80 48 2008-01-21 12.75 13.34 12.35 13.22 13.22 49 2008-01-22 13.08 13.20 11.90 11.90 11.90 50 2008-01-23 11.00 11.40 10.71 10.71 10.71 51 2008-01-24 11.30 11.47 11.00 11.17 11.17 52 2008-01-07 11.97 12.14 11.70 11.90 11.90 53 2008-01-08 11.90 12.75 11.80 12.21 12.21 54 2008-01-09 12.10 13.03 12.05 12.94 12.94 55 2008-01-10 13.04 13.32 12.71 13.10 13.10 56 2008-01-11 13.15 14.15 13.15 13.93 13.93 57 2008-01-14 13.96 14.45 13.60 14.03 14.03 58 2008-01-15 14.00 14.18 13.70 13.97 13.97 59 2008-01-16 13.80 13.85 13.11 13.32 13.32 60 2008-01-18 13.27 13.73 12.20 12.80 12.80 61 2008-01-21 12.75 13.34 12.35 13.22 13.22 62 2008-01-22 13.08 13.20 11.90 11.90 11.90 63 2008-01-23 11.00 11.40 10.71 10.71 10.71 64 2008-01-24 11.30 11.47 11.00 11.17 11.17 65 2008-01-07 11.97 12.14 11.70 11.90 11.90 66 2008-01-08 11.90 12.75 11.80 12.21 12.21 67 2008-01-09 12.10 13.03 12.05 12.94 12.94 68 2008-01-10 13.04 13.32 12.71 13.10 13.10 69 2008-01-11 13.15 14.15 13.15 13.93 13.93 70 2008-01-14 13.96 14.45 13.60 14.03 14.03 71 2008-01-15 14.00 14.18 13.70 13.97 13.97 72 2008-01-16 13.80 13.85 13.11 13.32 13.32 73 2008-01-18 13.27 13.73 12.20 12.80 12.80 74 2008-01-21 12.75 13.34 12.35 13.22 13.22 75 2008-01-22 13.08 13.20 11.90 11.90 11.90 76 2008-01-23 11.00 11.40 10.71 10.71 10.71 77 2008-01-24 11.30 11.47 11.00 11.17 11.17
使用openxlsx包
data4=read.xlsx("C:/Users/Administrator/Desktop/spss/数据/SPSS练习数据.xlsx",1) data4 39454 11.97 12.14 11.7 11.9 11.9 1 39455 11.90 12.75 11.80 12.21 12.21 2 39456 12.10 13.03 12.05 12.94 12.94 3 39457 13.04 13.32 12.71 13.10 13.10 4 39458 13.15 14.15 13.15 13.93 13.93 5 39461 13.96 14.45 13.60 14.03 14.03 6 39462 14.00 14.18 13.70 13.97 13.97 7 39463 13.80 13.85 13.11 13.32 13.32 8 39465 13.27 13.73 12.20 12.80 12.80 9 39468 12.75 13.34 12.35 13.22 13.22 10 39469 13.08 13.20 11.90 11.90 11.90 11 39470 11.00 11.40 10.71 10.71 10.71 12 39471 11.30 11.47 11.00 11.17 11.17 13 39454 11.97 12.14 11.70 11.90 11.90 14 39455 11.90 12.75 11.80 12.21 12.21 15 39456 12.10 13.03 12.05 12.94 12.94 16 39457 13.04 13.32 12.71 13.10 13.10 17 39458 13.15 14.15 13.15 13.93 13.93 18 39461 13.96 14.45 13.60 14.03 14.03 19 39462 14.00 14.18 13.70 13.97 13.97 20 39463 13.80 13.85 13.11 13.32 13.32 21 39465 13.27 13.73 12.20 12.80 12.80 22 39468 12.75 13.34 12.35 13.22 13.22 23 39469 13.08 13.20 11.90 11.90 11.90 24 39470 11.00 11.40 10.71 10.71 10.71 25 39471 11.30 11.47 11.00 11.17 11.17 26 39454 11.97 12.14 11.70 11.90 11.90 27 39455 11.90 12.75 11.80 12.21 12.21 28 39456 12.10 13.03 12.05 12.94 12.94 29 39457 13.04 13.32 12.71 13.10 13.10 30 39458 13.15 14.15 13.15 13.93 13.93 31 39461 13.96 14.45 13.60 14.03 14.03 32 39462 14.00 14.18 13.70 13.97 13.97 33 39463 13.80 13.85 13.11 13.32 13.32 34 39465 13.27 13.73 12.20 12.80 12.80 35 39468 12.75 13.34 12.35 13.22 13.22 36 39469 13.08 13.20 11.90 11.90 11.90 37 39470 11.00 11.40 10.71 10.71 10.71 38 39471 11.30 11.47 11.00 11.17 11.17 39 39454 11.97 12.14 11.70 11.90 11.90 40 39455 11.90 12.75 11.80 12.21 12.21 41 39456 12.10 13.03 12.05 12.94 12.94 42 39457 13.04 13.32 12.71 13.10 13.10 43 39458 13.15 14.15 13.15 13.93 13.93 44 39461 13.96 14.45 13.60 14.03 14.03 45 39462 14.00 14.18 13.70 13.97 13.97 46 39463 13.80 13.85 13.11 13.32 13.32 47 39465 13.27 13.73 12.20 12.80 12.80 48 39468 12.75 13.34 12.35 13.22 13.22 49 39469 13.08 13.20 11.90 11.90 11.90 50 39470 11.00 11.40 10.71 10.71 10.71 51 39471 11.30 11.47 11.00 11.17 11.17 52 39454 11.97 12.14 11.70 11.90 11.90 53 39455 11.90 12.75 11.80 12.21 12.21 54 39456 12.10 13.03 12.05 12.94 12.94 55 39457 13.04 13.32 12.71 13.10 13.10 56 39458 13.15 14.15 13.15 13.93 13.93 57 39461 13.96 14.45 13.60 14.03 14.03 58 39462 14.00 14.18 13.70 13.97 13.97 59 39463 13.80 13.85 13.11 13.32 13.32 60 39465 13.27 13.73 12.20 12.80 12.80 61 39468 12.75 13.34 12.35 13.22 13.22 62 39469 13.08 13.20 11.90 11.90 11.90 63 39470 11.00 11.40 10.71 10.71 10.71 64 39471 11.30 11.47 11.00 11.17 11.17 65 39454 11.97 12.14 11.70 11.90 11.90 66 39455 11.90 12.75 11.80 12.21 12.21 67 39456 12.10 13.03 12.05 12.94 12.94 68 39457 13.04 13.32 12.71 13.10 13.10 69 39458 13.15 14.15 13.15 13.93 13.93 70 39461 13.96 14.45 13.60 14.03 14.03 71 39462 14.00 14.18 13.70 13.97 13.97 72 39463 13.80 13.85 13.11 13.32 13.32 73 39465 13.27 13.73 12.20 12.80 12.80 74 39468 12.75 13.34 12.35 13.22 13.22 75 39469 13.08 13.20 11.90 11.90 11.90 76 39470 11.00 11.40 10.71 10.71 10.71 77 39471 11.30 11.47 11.00 11.17 11.17
5、读取spss数据
library(foreign) data5=read.spss("C:/Users/Administrator/Desktop/spss/数据/SPSS练习数据/data02-01.sav") data5
注:还有很多,没有一一列举。掌握住几个方法就可以了。