R语言学习笔记

R语言学习笔记1-基础篇

2015-12-03  本文已影响123人  RudyHe
- Basic Function
    - rnorm(10)
    - mean(abs(rnorm(100)) sd(rnorm(100))
    - source("xxx.R")
    - x<- c(1,2,4) q <- c(x,x,8)
    - y    # print out y
    - q()    # quit from command line
    - seq(1,6,by=3)    # 1 4
    - seq(0,1,length.out=11)    # 0.0,0.1,0.2,...0,9,1.0
    - help(seq), ?seq, example(seq)
- Function
    oddcount <- function(x) {    # oddcount is function name
        k <- 0
        return (k) 
    }
- For statement
    for (n in x) {
        if(n%%2==1) k <- k+1
    }
    for (i in 1:length(x)) {
        if(x[i]%%2==1) k <- k+1
    }
- Data Structure:
    - string: y<-"abc" mode(y)    # "character"
        - u<-paste("abc","def","e")    # abc def e
        - v<-strsplit(u," ")    # split according to blanks 
    - matrix: m<-rbind(c(1,4),c(2,2))    # row bind
        - m[1,2]    # 4
        - m[1,] # row 1
        - m[,2] # column 2
    - list: x<-list(u=2,v="abc")
        - x$u # 2
    - hn <- hist(Nile) # class
        - print(hn)
        - str(hn) # structure
        - attr(,"class") # "histogram"
    - d <- data.frame(list(kids=c("Jack","Jill"),ages=c(12,10)))
        - d, d$age
- Regression Example
    - examsquiz<-read.table("ExamsQuiz.txt",header=FALSE)
    - class(examsquiz) # "data.frame"
    - head(examsquiz) # less
    - lma<-lm(examsquiz[,2] ~ examsquiz[,1])    # using col 1 to fit col 2
    - lma<-lm(examsquiz$V2 ~ examsquiz$V1)    # same as above
    - attribute(lma), summary(lma)    # like print, str
    - lma, lma$coef
    - lmb<-lm(examsquiz[,2] ~ examsquiz[,1] + examsquiz[,3])    # + is not concate symbol
上一篇下一篇

猜你喜欢

热点阅读