R Language Basics Programming skills Compilation-17

Source: Internet
Author: User

1. Timestamp function Output Current time

Timestamp ()

# #------Sun APR 20:54:06------# #

The function can enter the current system time, can be used for a long time the program output the current time, to determine whether the program is working properly, or to debug, to determine which code is less efficient.

2. BoxPlot diagram of multiple comparisons

A=c (1,2,3,4,5,2,1,2,4,2,5,6)

B=c ("A", "a", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D")

C=c (1,2,3,4,5,5,2,1,2,4,2,5,6,5,6)

D=c ("A", "a", "a", "a", "B", "B", "B", "C", "C", "C", "C", "C", "D", "D", "D")

BoxPlot (a~b,boxwex=.24,at=1:4-.2,col= "Orange", Axes=false)

# define the width of the box chart

Box (); Axis (2)

Axis (1,at=c (1:4-.2,1:4+.2), Rep (c ("\u2640", "\u2642"), each=4))

BoxPlot (c~d,boxwex=.24,at=1:4+.2,col= "Yellow", Add=true,axes=false) # Add

3. Draw a Map

Require (MAPS)

Map (Database = "state", fill =false, boundary = TRUE, Interior = false, col = "Grey60", LWD = 0.6,lty = 1, add = False, re Solution = 0)

Map (Database = "state", fill =false, boundary = FALSE, Interior = true, col = "Grey70", LWD = 0.5,lty = "+", add = TRUE, Resolution = 0)

Par (Cex.axis = 0.7)

Map.axes ()

X1 = C (-109,-108,-103,-99,-97, -99,-107)

Y1 = C (39, 43, 48, 43, 39, 33, 37)

Xspline (x = x1, y = y1, shape = 0.9, open =false, border = NA, col = "#00FF0037")

x2 = C (-107,-107,-103,-101,-99, -101,-105)

y2 = C (39, 41, 43, 41, 39, 37, 38)

Xspline (x = x2, y = y2, shape = 0.9, open =false, border = NA, col = "#FF000073")

Points ( -103,, pch = 19)


Require (fields)

Data (Ozone2)

X<-ozone2$lon.lat

Y<-ozone2$y[16,] # June 18, 1987

fit<-Tps (x, y)

Surface (FIT)

US (add=true, col= "Magenta", lwd=2)

Title ("Daily Max 8 hour ozone INPPB, June 18th, 1987")


4. Draw a double axis

Drawing a double-axis graphic is not a good graphic, which can be confusing to the reader.

plot (1, main = ' How to torture the reader's eyes ')

Axis (2, runif (7,. 6, 1.4), line=1)

Axis (4, runif (3,. 6, 1.4), line=1)


5. Plot density functions and distribution function curves

X=seq ( -5,5,0.1)

Plot (X,dnorm (x), type= "L") # density function Curve

Plot (X,pnorm (x), type= "L") # distribution function Curve


6. Requirements for z-coordinates in three-dimensional graphs

Zis aMatrix! (Not a function) you have to understand that three-dimensional graphics and two-dimensional graphics are different, not that there are three vectorsx,y,Zyou can draw a3D(unless it's just a point of drawing),3DA graph is usually a surface, so you need aGridform of data, that is, each pair(x, y)corresponding to theZvalues. such as:

PERSP (X=1:10, Y=1:10,z=matrix (Rnorm (10*10), 10,10))


7. Determine if the data frame is the same

DF1 = = Df2

The comparison can be used to determine if each corresponding element in the data frame is equal.

8. Using the Exists letternumberDetermine if a variable already exists

if (!exists ("DataPath")) {

Datapath<-choose.dir (default = "", caption = "Please select a data source folder")

}

9. Labeling points for scatter plots

X=c (1,1,2,2,1,1,2)

Y=c (1,2,2,5,5,3,3)

Lbls <-C (

"List (x1,y1^ (1))", "List (x2,y2^ (2))", "List (x3,y3^ (3))",

"List (x4,y4^ (4))", "List (x5,y5^ (5))", "List (x6,y6^ (6))",

"List (x7,y7^ (7))"

)

Lbls <-Paste ("(", Lbls, ")", "" ")

{Plot (x,y,main= "x VsY", xlab= "x", ylab= "Y", xlim=c (. 9,2.1), Ylim=c (. 9,5))

Lines (x,y,col= "Black", lty= "dotted")

Text (X,y-.1,labels=parse (TEXT=LBLS))}

Explanation of the density function

X=rnorm (+) # x is what you want to do with the kernel density estimate, density () in N The default value is +

n=1000 # N is the number of points you want to make a nuclear density estimate, x can be specified when not too long n=length (x)

a=density (x,n) # a is a density () the results of all the calculations given

A[1:2] # you want the density to estimate the coordinates of all points on the curve, where

a$y # density () The given kernel density estimate, which is the ordinate in the graph

a$x # with the a$y the corresponding horizontal axis

Density.default # density () function Code

11. Using the Getanywhere function to find R objects

Some of the function's source code is hidden, you can try to get it in the following ways:

Getanywhere (Princomp.default)

12. Drawing Polar coordinates

Library (Plotrix)

T<-seq (-pi,pi,0.1)

R<-1-cos (T/2)

Radial.plot (r,t,rp.type= "P", lwd=3,line.col=2)


13. Use the Assign function to assign a value to a variable name in string form

For (i in 1:6) {#--Create objects ' R.1 ', ' r.2 ', ... ' R.6 '--

Nam <-paste ("R", I, Sep = ".")

Assign (NAM, 1:i)

}

LS (pattern = "^r"). $")

##--Global assignment within a function:

MYF <-function (x) {

Innerf <-function (x) Assign ("Global.res", x^2, EnviR =. GLOBALENV)

Innerf (x+1)

}

MYF (3)

Global.res # 16

A <-1:4

Assign ("A[1]", 2)

A[1] = = 2 # FALSE

Get ("a[1]") = = 2 # TRUE

14.Negative root Operation

Operations that can be converted to complex numbers

( -1+0i) ^ (1/3)

[1] 0.5+0.8660254i

The "empty set" is represented in the. R language

The following is an "empty set" of different data types

List ()

C ()

Data.frame ()

Factor ()

R Language Basics Programming skills Compilation-17

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.