首页 > 代码库 > R语言读取csv中的内容

R语言读取csv中的内容

       1992年,R语言诞生。R语言是PC和Linux时代的产物,R语言和贝尔实验室开发的S语言类似,R支持一系列分析技术,包括统计分析、预测建模、数据可视化。在CRAN上可以找到众多的扩张包。

       R软件 的首选界面是命令行界面,通过编写脚本来调用相应的功能函数。同时,它也支持图形界面。

       R语言可以用来做数据挖掘,下面我们就用它来读取birth2.csv 中的内容。

//birth2.csv

ALGERIA       ,36.4,14.6
CONGO         ,37.3,8
EGYPT         ,42.1,15.3
GHANA         ,55.8,25.6
IVORY COAST   ,56.1,33.1
MALAGASY      ,41.8,15.8
MOROCCO       ,46.1,18.7
TUNISIA       ,41.7,10.1
CAMBODIA      ,41.4,19.7
CEYLON        ,35.8,8.5
CHINA         ,34,11
TAIWAN        ,36.3,6.1
HONG KONG     ,32.1,5.5
INDIA         ,20.9,8.8
INDONESIA     ,27.7,10.2
IRAQ          ,20.5,3.9
FRANCE        ,25,6.2
JAPAN         ,17.3,7
JORDAN        ,46.3,6.4
KOREA         ,14.8,5.7
MALAYSIA      ,33.5,6.4
MONGOLIA      ,39.2,11.2
PHILLLIPINES  ,28.4,7.1
SYRIA         ,26.2,4.3
THAILAND      ,34.8,7.9
VIETNAM       ,23.4,5.1
CANADA        ,24.8,7.8
COSTA RICA    ,49.9,8.5
DOMINICAN R   ,33,8.4
GUATEMALA     ,47.7,17.3
HONDURAS      ,46.6,9.7
MEXICO        ,45.1,10.5
NICARAGUA     ,42.9,7.1
PANAMA        ,40.1,8
UNITED STATES ,21.7,9.6
ARGENTINA     ,21.8,8.1
BOLIVIA       ,17.4,5.8
BRAZIL        ,45,13.5
CHILE         ,33.6,11.8
COLOMBIA      ,44,11.7
ECUADOR       ,44.2,13.5
PERU          ,27.7,8.2
URUGUAY       ,22.5,7.8
VENEZUELA     ,42.8,6.7
AUSTRIA       ,18.8,12.8
BElGIUM       ,17.1,12.7
BRITAIN       ,18.2,12.2
BULGARIA      ,16.4,8.2
CZECHOSLOVAKIA ,16.9,9.5
DENMARK       ,17.6,19.8
FINLAND       ,18.1,9.2
FRANCE        ,18.2,11.7
E.GERMANY     ,17.5,13.7
W.GERMANY     ,18.5,11.4
GREECE        ,17.4,7.8
HUNGARY       ,13.1,9.9
IRELAND       ,22.3,11.9
ITALY         ,19,10.2
NETHERLANDS   ,20.9,8
NORWAY        ,17.5,10
POLAND        ,19,7.5
PORTUGAL      ,23.5,10.8
ROMANIA       ,15.7,8.3
SPAIN         ,21.5,9.1
SWEDEN        ,14.8,10.1
SWITZERLAND   ,18.9,9.6
U.S.S.R.      ,21.2,7.2
YUGOSLAVIA    ,21.4,8.9
AUSTRALIA     ,21.6,8.7
NEW ZEALAND   ,25.5,8.8
   

    加载相应的程序包,这些程序包里有R的内置函数,加载语法为: library(X)  ; X表示程序包

//lib2.R

library(MASS)
library(lpSolve)
library(sampling)
library(RODBC)
library(survival)
library(splines)
library(Hmisc)
library(timeDate)
library(timeSeries)
library(norm)
library(fBasics)
library(Matrix)
library(rattle)
library(ellipse)

library(lattice)
library(nnet)
library(mice)
library(arules)

library(colorspace)
library(TSP)
library(gclus)
library(scatterplot3d)
library(grid)
library(vcd)
library(seriation)
library(cluster)


library(mvtnorm)
library(stats4)
library(modeltools)
library(multcomp)
library(mclust)
library(flexmix)
library(fpc)

    在R软件的命令行窗口,输入语句
setwd("D://R_work//data") #设置工作区间
data4 = read.table("birth2.csv",header=F, sep=",")  #读取csv文件
data4    #输出向量data4中的内容

效果如下:

技术分享

图(1)用R语言读取.csv文件中的内容

 



R语言读取csv中的内容