rain1024 / gVim-Pathogen

My Coding Life
http://rain1024.github.io/gVim-Pathogen
2 stars 0 forks source link

R Language #37

Open rain1024 opened 10 years ago

rain1024 commented 10 years ago

References

x = c(1,2,3)
length(x)
x = matrix(data=c(1,2,3,4), nrow=2, ncol=2)

2. Produce data

> x=rnorm(50)
> x
[1] -0.69778147 -0.58733711 -0.41821011  0.07711663 -0.32712598  0.09337342
 [7]  0.22503699  0.76625162 -0.62007099 -1.06133777  0.70245886 -0.34257002
[13]  1.79848837 -1.24706944 -1.18201996  0.02054658 -1.02699651 -1.27693597
[19] -0.71657301  0.53884647  0.70948891 -0.22250508  1.79929778 -0.27810862
[25] -0.24404130 -1.30579768 -0.47628860  2.13341798  0.28809433  1.53404013
[31]  0.41466097  0.07932241  1.27776182  1.35676481 -0.31950235  1.60930600
[37] -0.99313057  1.77072948 -0.11114181 -1.48573206 -0.44896189  0.13572570
[43] -0.31273919 -0.38230438  0.97229612 -0.63988021 -0.83462887  0.32951019
[49]  0.63517490 -2.09176003
> y=x+rnorm(50,mean=50,sd=.1)
> cor(x,y)
[1] 0.995
rain1024 commented 10 years ago

Indexing Data

> A=matrix(1:16,4,4)
> A
      [,1] [,2] [,3] [,4]
[1,]    1    5   9    13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
> A[2,3]
[1] 10

> A[c(1,3),c(2,4)]
    [,1] [,2]
[1,] 5 13
[2,] 7 15

> A[1:3,2:4]
    [,1] [,2] [,3]
[1,] 5 9 13
[2,] 6 10 14
[3,] 7 11 15

> A[1:2,]
    [,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14

> A[,1:2]
    [,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
rain1024 commented 10 years ago

Loading Data

Auto=read.table("Auto.data")
fix(Auto)
> Auto=read.csv("Auto.csv",header=T,na.strings ="?")
> fix(Auto)
> dim(Auto)
[1] 397 9
> Auto[1:4,]
# names
# Functions to get or set the names of an object.
> names(Auto)
[1] "mpg"          "cylinders"    "displacement" "horsepower"   "weight"
[6] "acceleration" "year"         "origin"       "name"
rain1024 commented 10 years ago

Graphic

> x=rnorm(100)
> y=rnorm(100)
> plot(x,y)
> plot(x,y,xlab="this is the x-axis",ylab="this is the y-axis",
main="Plot of X vs Y")

1 Figure

> pdf("Figure.pdf")
> plot(x,y,col="green")
> dev.off()
null device
                1

2 Contour

# The contour() function produces a *contour plot* in order to represent 
# three-dimensional data;
# it is like a topographical map. It takes three arguments:
#    1. A vector of thexvalues (the first dimension),
#    2. A vector of theyvalues (the second dimension), and
#    3. A matrix whose elements correspond to thezvalue (the third dimension)
#    for each pair of (x,y) coordinates.
> x=seq(1,10)
>x
[1]12345678910
> x=1:10
>x
[1]12345678910
> x=seq(-pi,pi,length =50)
>y=x
> f=outer(x,y,function (x,y)cos(y)/(1+x^2))
> contour(x,y,f)
> contour(x,y,f,nlevels =45,add=T)
> fa=(f-t(f))/2
> contour(x,y,fa,nlevels =15)
> image(x,y,fa)
> persp(x,y,fa)
> persp(x,y,fa,theta=30)
> persp(x,y,fa,theta=30,phi=20)
> persp(x,y,fa,theta=30,phi=70)
> persp(x,y,fa,theta=30,phi=40)

abc

rain1024 commented 10 years ago

Library

install.packages("ggplot2")
rain1024 commented 10 years ago

Installation

Ubuntu

apt-get install r-base