熊荣川
六盘水师范学院生物信息学实验室 **********************
http://blog.sciencenet.cn/u/Bearjazz
平台的开放性使得R语言具有了丰富的运算功能,使得一些表格数据不能在Excel中实现的运算(或是较为繁琐的运算)可以在导入R语言之后得到快速而容易的实现。然后,R语言平台本身对于表格的交互性查看和编辑都不是很方便。因此,倘若把两者结合起来就完美至极了,其它的博文我们大致提了一下如何从表格中导入数据,例如“怎样用R语言处理表格导入数据中的缺省值” http://bbs.sciencenet.cn/home.php?mod=space&uid=508298&do=blog&id=548225 所以,本文只是简单的介绍怎样将在R语言平台上生成的或是编辑过得表格数据保存为Excel文件。
实例一,将R平台上生成的数据保存为Excel文件 > nx <- c(rnorm(10)) 随机生成一个包含10个正态分布数据的向量(一维表格) > nx 查看向量 [1] 0.436219296 -0.003864687 1.666923704 查看结果 -0.755768282 1.070840200 [6] 0.943247037 0.861156081 -1.083567875 -1.137469924 0.303574238 write.csv(nx, file=\"D:/bear.csv\") 将向量保存到表格bear.csv中 结果 > 实例二,将编辑、运算过后的数据导出为Excel表格文件
实例一是一个简单的输出操作,旨在让读者掌握输出操作的精髓所在。下面我通过一个稍稍复杂的例子来演示在R语言平台和Excel表格之间自由交流数据的魅力所在。
> data<-read.csv(\"D:\\\\ziliao\\\\zhuanye\\\\R bear\\\\bearf.csv\") 读入表格,存在data向量中 > data 查看向量 F1.Hz. F2.Hz. F3.Hz. F4.Hz. F5.Hz. 二维向量的查看结果, 6列、42F6.Hz. 行 1 3431.654 4596.179 9642.441 12348.066 3838.552 7498.416 2 3461.062 4524.386 10666.409 11647.196 4754.872 7498.416 3 3411.314 4415.518 10666.409 11166.294 4773.052 7498.416 4 3605.767 3838.552 10666.409 10008.806 4676.657 7498.416 5 3445.936 4754.872 10666.409 9935.201 4813.661 7498.416 6 3500.930 4773.052 10666.409 10051.817 4686.464 7498.416 7 3518.311 4676.657 10666.409 10441.247 4792.689 9506.292 8 3536.907 4813.661 7736.680 11894.051 4739.644 8709.927 9 3545.723 4686.464 8599.928 12063.672 4861.783 7913.562 10 3553.186 4792.689 10824.886 11938.194 4813.760 7117.197 11 3552.418 4739.644 10015.512 11809.212 4819.385 8208.955 12 3553.105 4861.783 11534.382 12513.075 4688.018 8278.993 13 3554.377 4813.760 9760.246 11382.993 4666.107 8349.031 14 3539.068 4819.385 7407.902 11420.825 4788.276 8419.069 15 3514.625 4688.018 7117.997 11035.042 4794.959 8489.106 16 3534.030 4666.107 8525.595 11722.272 4794.926 8559.144 17 3521.814 4788.276 8525.595 10939.196 4819.187 8629.182 18 3588.896 4794.959 8525.595 10921.920 4789.445 8699.220 19 3615.696 4794.926 8525.595 11512.921 4786.146 8769.257 20 3595.440 4819.187 7539.253 11756.123 4787.794 8839.295 21 3595.121 4789.445 6696.898 12120.326 4810.097 8909.333 22 3596.052 4786.146 6662.958 12316.042 4784.533 8979.371 23 3603.943 4787.794 7415.122 12355.483 4882.977 9049.409 24 3614.603 4810.097 6935.103 11976.211 4896.201 9119.446 25 3605.269 4784.533 7498.416 11756.809 4909.424 9189.484 26 3594.244 4801.659 7498.416 11148.864 4922.647 9259.522 27 3557.889 4777.278 7498.416 11530.219 4935.870 9329.560 28 3533.653 4769.104 7498.416 11038.150 4949.094 9399.597 29 3524.953 4801.545 7498.416 10680.637 4962.317 9469.635 30 3583.689 4746.466 7498.416 10477.168 4975.540 9539.673 31 3601.696 4735.630 7498.416 10999.146 4988.764 9609.711 32 3583.889 4735.707 7498.416 10841.006 5001.987 9679.749 33 3531.654 4684.883 7498.416 10726.529 5015.210 9749.786 34 3524.498 4719.818 7498.416 11226.458 5028.433 9819.824 35 3590.700 4794.351 9506.292 11493.798 5041.657 9889.862 36 3533.200 4741.003 8709.927 12484.489 5054.880 9959.900 37 3508.570 4735.610 7913.562 11674.365 5068.103 10029.938 38 3544.007 4699.263 7117.197 11521.220 5081.327 10099.975 39 3652.210 4622.555 6320.832 12220.909 5094.550 10170.013 40 3657.347 4356.328 9531.433 13190.689 5107.773 10240.051 41 3707.543 4240.136 10560.521 13190.689 5120.996 10310.089 42 3553.006 4480.197 11589.609 10394.072 5134.220 10380.126 > data <- data/100+50 对表格中所有的单元格数据都除以100之后加上50 > > data 查看运算后data向量 F1.Hz. F2.Hz. F3.Hz. F4.Hz. F5.Hz. 看见了吧,这比在Excel表格中F6.Hz. 的繁琐的“公式输入”、“拖曳”、1 84.31654 95.96179 146.4244 173.4807 88.38552 新建表格等操作容易多了 124.9842 2 84.61062 95.24386 156.6641 166.4720 97.54872 124.9842 3 84.11314 94.15518 156.6641 161.6629 97.73052 124.9842 4 86.05767 88.38552 156.6641 150.0881 96.76657 124.9842 5 84.45936 97.54872 156.6641 149.3520 98.13661 124.9842 6 85.00930 97.73052 156.6641 150.5182 96.86464 124.9842 7 85.18311 96.76657 156.6641 154.4125 97.92689 145.0629 8 85.36907 98.13661 127.3668 168.9405 97.39644 137.0993 9 85.45723 96.86464 135.9993 170.6367 98.61783 129.1356 10 85.53186 97.92689 158.2489 169.3819 98.13760 121.1720 11 85.52418 97.39644 150.1551 168.0921 98.19385 132.0896 12 85.53105 98.61783 165.3438 175.1308 96.88018 132.7899 13 85.54377 98.13760 147.6025 163.8299 96.66107 133.4903 14 85.39068 98.19385 124.0790 164.2083 97.88276 134.1907 15 85.14625 96.88018 121.1800 160.3504 97.94959 134.8911 16 85.34030 96.66107 135.2559 167.2227 97.94926 135.5914 17 85.21814 97.88276 135.2559 159.3920 98.19187 136.2918 18 85.88896 97.94959 135.2559 159.2192 97.89445 136.9922 19 86.15696 97.94926 135.2559 165.1292 97.86146 137.6926 20 85.95440 98.19187 125.3925 167.5612 97.87794 138.3930 21 85.95121 139.0933 22 85.96052 139.7937 23 86.03943 140.4941 24 86.14603 141.1945 25 86.05269 141.8948 26 85.94244 142.5952 27 85.57889 143.2956 28 85.33653 143.9960 29 85.24953 144.6964 30 85.83689 145.3967 31 86.01696 146.0971 32 85.83889 146.7975 33 85.31654 147.4979 34 85.24498 148.1982 35 85.90700 148.8986 36 85.33200 149.5990 37 85.08570 150.2994 38 85.44007 150.9998 39 86.52210 151.7001 40 86.57347 152.4005 41 87.07543 153.1009 42 85.53006 153.8013 97.89445 116.9690 171.2033 98.10097 97.86146 116.6296 173.1604 97.84533 97.87794 124.1512 173.5548 98.82977 98.10097 119.3510 169.7621 98.96201 97.84533 124.9842 167.5681 99.09424 98.01659 124.9842 161.4886 99.22647 97.77278 124.9842 165.3022 99.35870 97.69104 124.9842 160.3815 99.49094 98.01545 124.9842 156.8064 99.62317 97.46466 124.9842 154.7717 99.75540 97.35630 124.9842 159.9915 99.88764 97.35707 124.9842 158.4101 100.01987 96.84883 124.9842 157.2653 100.15210 97.19818 124.9842 162.2646 100.28433 97.94351 145.0629 164.9380 100.41657 97.41003 137.0993 174.8449 100.54880 97.35610 129.1356 166.7437 100.68103 96.99263 121.1720 165.2122 100.81327 96.22555 113.2083 172.2091 100.94550 93.56328 145.3143 181.9069 101.07773 92.40136 155.6052 181.9069 101.20996 94.80197 165.8961 153.9407 101.34220 > write.csv(data, file=\"D:/bearf2.csv\") 将向量保存到表格bearf2.csv中(下图) 看到了这里有什么启发吗?呵呵,祝你科研愉快。
因篇幅问题不能全部显示,请点此查看更多更全内容