File Input and Output with R

data.frame function is similar like structure in C language, which can contain different types of data as a union, helping us to manage data more easily.


1. Reading a column from a data frame.

> treasure <- data.frame(weights, prices, types)
> treasure[[2]]
> treasure[["weights"]]
> treasure$prices


2. Loading a dataset from a package

> data(meuse, package = "sp")

3. Loading a data frame from files

First of all, we can use list.files() to show all the docs in current working folder.

Secondly, read.csv() could load a file into workspace.

> list.files()
[1] "__MACOSX"                    "Groundwater_Temperature.csv"
[3] "Groundwater_Temperature.xls" "gstat.pdf"                  
[5] "gstat_1.1-1"                 "gstat_1.1-1.zip"            
> read.csv("Groundwater_Temperature.csv")
        Name X.Koordinate Y.Koordinate Oberfl盲che   Datum Temperatur
1    ABP9922      4459725      5340978      497.50 7/25/09        9.9
2    ABP9926      4460005      5341246      498.90 7/25/09       12.0
3      BP 25      4464282      5331784      534.17 7/26/09        9.9

4. Formatting a txt file

Original of "infantry.txt":
Port         Infantry
Porto Bello  700
Cartagena    500
Panama City  1500
Havana       2000

> read.table("infantry.txt", sep = "\t", header = TRUE)
         Port Infantry
1 Porto Bello      700
2   Cartagena      500
3 Panama City     1500
4      Havana     2000

5. Printing

sprintf("the coordinate of 30th record is %g %f", meuse[30,1], meuse[30,2] )

%f -- float

%e -- Scientific notation

%g -- integer


print(paste0("Current working dir: ", wd))
paste()/paste0() -- convert vector into character





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值