R语言学习笔记

R语言学习笔记9-输入输出篇

2015-12-05  本文已影响375人  RudyHe
- standard io
    - z1.txt
        - 123
        - 4 5
        - 6
    - z2.txt
        - 123
        - 4.2 5
        - 6
    - z3.txt
        - abc
        - de f
        - g
    - z4.txt
        - abc
        - 123 6
        - y
    - scan("z1.txt")
        - 123 4 5 6
    - scan("z2.txt")
        - 123.0 4.2 5.0 6.0
    - scan("z3.txt")
        - ERROR
    - scan("z3.txt",what="")
        - "abc" "de" "f" "g"
    - scan("z4.txt",what="")
        - "abc" "123" "6" "y"
    - v<-scan("z1.txt")
    - x1<-scan("z3.txt",what="")    # "abc" "de" "f" "g"
    - x2<-scan("z3.txt",what="",sep="\n")    # "abc" "de f" "g"
    - v<-scan("")    # read from keyboard, empty line means end
    - w<-readline()    # equivalent to above
    - inits<-readline("type your initials: ")    # with prompt
        - type your initials: NM
    - inits
        - "NM"
    - prints(1:3)
        - [1] 1 2 3
    - cat(1:3)
        - 1 2 3    # without [1]
    - cat(x,"abc","de\n")
        - 1 2 3 abc de    # seperate with blankspace
    - cat(x,"abc","de\n",sep="")
        - 123abcde
    - x<-c(5,12,13,8,88)
    - cat(x,sep=c(".",".",".","\n","\n")
        - 5.12.13.8
        - 88
- file
    - z
        - name age
        - John 25
        - Mary 28
        - Jim 19
    - z<-read.table("z",header=TRUE)
    - x
        - 1 0 1
        - 1 1 1
        - 1 1 0
        - 1 1 0
        - 0 0 1
    - x<-matrix(scan("x"),nrow=5,byrow=TRUE)
    - read.matrix<-function(filename) {
        - as.matrix(read.table(filename))
    - }
    - z1<-readLines("z")
    - z1
        - "name age" "John 25" "Mary 28" "Jim 19"
    - c<-file("z1","r")
    - readLines(c,n=1)
        - "John 25"
    - readLines(c,n=1)
        - "Mary 28"
    - readLines(c,n=1)
        - "Jim 19"
    - readLines(c,n=1)
        - character(0)
    - c<-file("z","r")
    - while(TRUE) {
        - rl<-readLines(c,n=1)
        - if(length(rl)==0) {
            - print("reach the end")
            - break
        - } else print(rl)
    - }
    - c<-file("z1","r")
    - readLines(c,n=2)
        - "John 25" "Mary 28"
    - seek(con=c,where=0)    # move to begin
        - 16
    - readLines(c,n=1)
        - "John 25"
    - uci<-"http://www.baidu.com/"
    - uci<-paste(uci,"path/welcome.csv",sep="")
    - ecc<-read.csv(uci)
    - kids<-c("Jack","Jill")
    - ages<-c(12,10)
    - d<-data.frame(kids,ages,stringAsFactors=FALSE)
    - d
        -   kids ages
        - 1 Jack 12
        - 2 Jill 10
    - write.table(d,"kds")
    - write.table(xc,"xcnew",row.names=FALSE,col.names=FALSE)    # write without row and col names
    - cat("abc\n",file="u")
    - cat("de\n",file="u",append=TRUE)    # append to file end
    - cat(file="v",1,2,"xyz\n")
    - c<-file("www","w")
    - writeLines(c("abc","de","f"),c)
    - close(c)
    - sumtree<-function(drtr) {
        - tot<-0
        - fls<-dir(drtr,recursive=TRUE)
        - for (f in fls) {
            - f<-file.path(drtr,f)
            - if(!file.info(f)$isdir) {
                - tot<-tot+sum(scan(f,quiet=TRUE))
            - }
        - }
        - return(tot)
    - }
- socket
    - cons<<-vector(mode="list",length=ncon)
    - options("timeout"=10000)
    - for (i in 1:ncon) {
        - cons[[i]]<<-socketConnection(port=port,server=TRUE,blocking=TRUE,open="a+b")
        - checkin<-unserialize(cons[[i]])
    - }
    - for (i in 1:ncon) {
        - serialize(c(i,ncon),cons[[i]])
    - }
    - repeat {
        - if(remainingclients==0) break
        - rdy<-which(socketSelect(cons))
        - j<-sample(1:length(rdy),1)
        - con<-cons[[rdy[j]]]
        - req<-unserialize(con)
    - }
    - binwrite<-function(dt,md,cn) {
        - writeBin(dt,con=cn)
    - }
    - binread<-function(cn,md,sz) {
        - return(readBin(con=cn,what=md,n=sz))
    - }
    - options("timeout"=10000)
    - con<-socketConnection(host=host,port=port,blocking=TRUE,open="a+b")
    - serialize(list(req="checking in"),con)
    - myidandnclnt<-unserialize(con)
    - myinfo<<-list(con=con,myid=myidandclnt[1],nclnt=myidandclnt[2])
上一篇 下一篇

猜你喜欢

热点阅读