zoukankan      html  css  js  c++  java
  • R Language Learn Notes

    One

    #install package
    install.packages("ggplot2")
    
    #load library
    library(ggplot2)
    #update.packages()
    
    #vector
    v=c(1,4,4,3,2,2,3)
    #get vector elements, index is 2,3,4
    v[c(2,3,4)]
    #get vector elements, index range is 2 to 4
    v[2:4]
    #get vector elements, index is 2,4,3
    v[c(2,4,3)]
    #delete vector elements index is 2
    v[-2]
    #delete vector elements index range is 2 to 4
    v[-2:-4]
    #get vector elements, which value < 3
    v[v<3]
    #get vector elements index, which value = 3
    which(v==3)
    #get vector max element index
    which.max(v)
    #get vector min element index
    which.min(v)
    
    #get random values
    set.seed(250)
    a=runif(3,min = 0, max = 100)
    
    floor(a)
    ceiling(a)
    round(a, 3)
    
    #read data from file
    data1=read.table(file = "/Users/hwgt/workspace/Rlang/data_1.txt", header = TRUE)
    #header to variable
    attach(data1)
    
    #draw
    set.seed(123)
    x=rnorm(100, mean = 100, sd = 10)
    set.seed(234)
    y=rnorm(100, mean = 100, sd = 10)
    hist(x, breaks = 20)
    plot(density(x))
    plot(x)
    boxplot(x, y)
    boxplot(yage)
    qqnorm(x)
    qqline(x)
    qqplot(x, y)
    

    Two

    # vector
    a = c(1, 2, 3, 4, 5)
    b = c("one", "two", "three")
    c = c(TRUE, FALSE)
    
    
    # matrix
    x = matrix(1:20, nrow = 5, ncol = 4, byrow = TRUE)
    x
    y = matrix(1:20, nrow = 5, ncol = 4, byrow = FALSE)
    y
    
    x[2,]
    x[,2]
    x[1,4]
    x[2,c(2,4)]
    x[3:5,2]
    
    rnames = c("apple", "banana", "orange", "melon", "corn")
    cnames = c("cat", "dog", "bird", "pig")
    m = matrix(1:20, 5, 4, TRUE)
    rownames(m) = rnames
    colnames(m) = cnames
    m
    
    dim1 = c("A1", "A2")
    dim2 = c("B1", "B2", "B3")
    dim3 = c("C1", "C2", "C3", "C4")
    dim4 = c("D1", "D2", "D3")
    z = array(1:72, c(2,3,4,3), dimnames = list(dim1, dim2, dim3, dim4))
    z
    z[1,2,3,]
    
    
    # data frame
    patientID = c(1,2,3,4)
    age = c(25,26,27,28)
    diabetes = c("Type1", "Type2", "Type1", "Type2")
    status = c("Poor", "Improved", "Excellent", "Poor")
    patientData = data.frame(patientID, age, diabetes, status)
    patientData
    
    
    # list
    listData = list(patientData, x)
    listData[1]
    listData[2]
    
    # graphs
    par(mfrow=c(2,2))
    plot(rnorm(50), pch=17)
    plot(rnorm(20), type="l", lty=5)
    plot(rnorm(100), cex=0.5)
    plot(rnorm(200), lwd=2)
    

    Three

    # operator
    
    # control flow
    # for loop
    for(a in 1:10) {
      print(a)
    }
    
    # while loop
    i = 1
    while(i <= 10) {
      print(i)
      i = i + 1
    }
    
    # if
    i = 2
    if (i == 1) {
      print("hello r")
    } else if(i == 2){
      print("goodbye r")
    } else {
      print("good r")
    }
    
    # switch
    feelings = c("sad", "afraid")
    for(i in feelings) {
      print(
        switch(i,
               a = "a",
               sad = "b",
               c = "c",
               afraid = "d",
               e = "e")
      )
    }
    # function
    numSum = function(a, b) {
      return(a + b)
    }
    print(numSum(1,2))
    

    Four

    # Bar Chart
    #install.packages("vcd")
    library(vcd)
    counts = table(Arthritis$Improved)
    counts
    par(mfrow=c(2,2))
    
    barplot(counts,
            main="Simple Bar Plot",
            xlab = "Improvement",
            ylab = "Frequency")
    
    barplot(counts,
            main="Horizontal Bar Plot",
            xlab = "Frequency",
            ylab = "Improvement",
            horiz = TRUE)
    
    counts <- table(Arthritis$Improved, Arthritis$Treatment)
    counts
    
    barplot(counts,
            main="Stacked Bar Plot",
            xlab = "Treatment",
            ylab = "Frequency",
            col = c("red", "yellow", "green"),
            legend = rownames(counts))
    
    barplot(counts,
            main="Grouped Bar Plot",
            xlab = "Treatment",
            ylab = "Frequency",
            col = c("red", "yellow", "green"),
            legend = rownames(counts),
            beside = TRUE)
    
    
    # Pie Chart
    #install.packages("plotrix")
    library(plotrix)
    par(mfrow=c(2,2))
    slices <- c(10,12,4,16,8)
    lbls <- c("US", "UK", "AU", "GE", "FR")
    pie(slices, lbls, main = "Simple Pie Chart", edges = 300, radius = 1)
    
    pct <- round(slices/sum(slices)*100)
    lbls2 <- paste(lbls, " ", pct, "%", sep = " ")
    pie(slices, lbls2, main = "Simple Pie Chart With Progress", edges = 300, radius = 1)
    
    pie3D(slices,labels =  lbls, explode = 0.1, main = "3D Pie Chart", edges = 300, radius = 1)
    
    # Fan Plot
    slices3 = c(10, 12, 4, 16, 8)
    lbls3 = c("US", "UK", "DE", "KR", "CN")
    fan.plot(slices3, labels = lbls3, main = "Fan Plot")
    
    # Dot Chart
    dotchart(mtcars$mpg, labels = row.names(mtcars), cex = 0.7,main = "Dot Chart", xlab = "Miles Per Gallon")
    
    # Summary
    head(mtcars)
    summary(mtcars)
    
    # Tables
    attach(mtcars)
    table(cyl)
    summary(mpg)
    table(cut(mpg, seq(10, 34, by=2)))
    
    # Correlations
    states = state.x77[, 1:6]
    cov(states)
    var(states)
    cor(states)
    
    # T Test
    x = rnorm(100, mean = 0, sd = 1)
    y = rnorm(100, mean = 30, sd = 1)
    t.test(x, y, alt="two.sided", paired = TRUE)
    
    # Wilcoxon
    wilcox.test(x, y, alt="less")
    
  • 相关阅读:
    ECMAScript 6 基础入门
    软件历史版本存档及下载
    arduino 编程基础
    生活中的实验 —— 家庭电路
    电子元件 —— 继电器
    电与磁 —— 电磁铁
    windows cmd 命令行 —— 进程与服务
    计算机硬件、摄影设备、物质、材料英语
    DHCP服务器备份、还原、迁移
    SVN同步
  • 原文地址:https://www.cnblogs.com/lurenjiashuo/p/R-Language-Learn-Notes.html
Copyright © 2011-2022 走看看