1. Clear Screen Ctrl+L2. Exit Q ()3. Set the workspace getwd () SETWD ('D:\\Program Files\\rstudio\\workspace')4. List of files under current working directory List.files () Choose.dir () dir ("D:\\Program Files\\rstudio\\workspace", Full.names=t, All.files=t, recursive=T)5. Install package install.packages ("Gstat") Remove.packages ("Gstat")#DeleteUpdate.packages ()#update all packages that are already installedAvailabe.packages ()#available updates to the CentralDownload.packages (pkgs,...)#Download the specified packagePackagestatus ()#back to updatable information6. Load Package Library (GSTAT) detach ("Package:gstat")#reverse operation of the libraryLibrary ()#View all available packagesSearch ()#View the package that is currently being transferred into memory7. Find help? gstathelp ('Gstat') Apropos ('Gstat')8. Environment variables (options) options ("digits")#default print character lengthOptions (digits=3)#Set Print character length9. Run the System command systems ("ls x*") files<-System ("ls x*", intern=t)#save Output as R object, add parameter Intern=t10. Memory Management Rgui--min-vsize=10m--max-vsize=100m--min-nsize=500k--max-nsize=1m#Manage memory at startup#Modify maximum memory and query memory information after startupHelp (memory.size) memory.size (Max=FALSE) memory.limit (Size=NA) Memory.limit () memory.profile ()11. Display in-memory variables ls () ls.str () str (x)12. Clear the In-memory variable RM (list=ls ()) RM ('a')13. Test run time#include function in System.time (), Return run timeSystem.time ( for(Iinch1:100) Mad (runif (1000)))15the object types of atomic type R include numeric (numeric), complex (complex), logic (logical), character (character) plain (raw ) NA16The vector vector must ensure that all its elements are the same pattern. The types of vector objects include: real, complex, logical, string. They are atomic (atomic), which is the same as the element type. 17factor A factor includes not only the categorical variables themselves but also the possible levels of variables (even if they do not appear in the data). The factor function factor creates a factor with the following options: Factor (x, Levels= Sort (Unique (x), na.last = TRUE), labels = levels, exclude = na, ordered = is. Ordered (x))18List (list ) is an object that consists of an ordered set of objects. The object contained in the list is also known as its component (components). Each component can have a different length and type. Lst<-List (name="Fred", wife="Mary", No.children=3, Child.ages=c (4,7,9) Gets the components Lst$name and lst[[1]] return results are"Fred", Lst$wife and lst[[2]] The return is"Mary", lst$child.ages[1] and Lst[[4]][1] Return the same number 4 here's a special note lst[[1]] and lst[1] the difference. [[...]] Is the operator used to select a single element, while the [...] is a more general subscript operator. So the former gets the first object in the list LST, and the component names in the named list containing the component names (named list) are excluded from the loss. The latter is a sub-list of the list LST consisting only of the first element. In the case of a named list, the component names are passed to the child list. 19The . Data frame (data.frame) data frame (Data.frame) is also a list central is a list that belongs to the "Data.frame" class. However, there are some restrictions on list objects that might be part of a data frame. The component must be a vector (numeric, character, logical), factor, numeric matrix, list, or other data frame, and the number of rows per column must be equal. A data frame is often considered to be a matrix of columns of different patterns and attributes. It can appear as a matrix, and the ranks can be accessed through the index of the matrix. 20The matrix matrix is a double subscript (2-D) array, but it is so important that it needs to be discussed separately. Includes many operators and functions that operate on matrices only. The name is the same as array (). The subscript order of the matrix is first column, then second column, and so on. D<-matrix (c (1,2,3,4,5,6,7,8,9), nc=3)21starray and dimension-named arrays can be thought of as a collection of elements with the same number of subscript types, such as numeric Central, which are generalizations of matrices. R has some simple tools for creating and working with arrays, especially matrices. A vector can be used as an array in R only after the Dim attribute is defined. Suppose that z is a vector with 1500 elements. So Dim (z)<-c (3,5,100), the assignment of the Dim attribute makes the Z-vector a 3-dimensional 3*5*an array of 100. #the order of the names is always row, column, third dimension,..., each dimension can also have a total name, or there can be noRabbits <-Array (c (0, 0,6, 5, 3, 0, 3, 6, 6, 2, 0, 4, 5, 6, 1, 0,2, 5, 0, 0), Dim= C (2, 2, 5), Dimnames=List (Delay= C ("None","1.5h"), Response= C ("Cured","Died"), Penicillin.level= C ("1/8"," Quarter"," the","1","4")))22. String and related operations the function for the string hasPrint, paste, cat, nchar, strsplit, regexpr, grep, gsub, sub etc. > seq="ggggcgaaaccgagactctcaaatgacttttctga"> seq=strsplit (SEQ,"") CHARTR (old, New, X): String substitution tolower (x), ToUpper (x): Case transform Paste, Cat: Merge string nchar: How many letters substr (x, start, S TOP): Take substring strsplit: Split string by delimiter gsub, sub: substring replace regexpr, grep: Find substring23. Fractional MASS fraction function to convert decimals to approximate fractions> Fractions (0.291667)24. Date and time current date and time Central returns the date and Datetimeclasses objects. >Date ()>sys.time ()>sys.date ()> s='08:10:00'> Z=strptime (s),'%h:%m:%s') >class(z) A string converted to a datetime function has format and as.character format to convert the date Posixlt Posixct to a string of the specified format. For example> W=format. Posixlt (z,format="%y-%m-%d") As.character converted to a string. For example> w=As.character (z) strptime converts the string to "Posixlt", for example> W=strptime ("09:10:00",'%h:%m:%s') strftime is format. A wrapper of Posixlt#converts the datetimecleses to Date. For example > as. Date (W) #Time DifferenceAs.double (Difftime (w,z,units="sec"))16. String expressions and evaluation#Returns an expression list, but not evaluatedParse (text="0==1") expression (0= = 1)#find the value of the expression represented by the stringEval (Parse (text="0==1"))#Deparse () returns the function code string. Substitute () replaces the variable with its value.17read external Data (Meuse)#RDARead.table (File=file.choose (), header=t)#textRead.csv ('D:/r/test2.csv', Header=t, Row.names=1)#CSVdata<-read.table (File="Clipboard", sep="\ t", header=F, colclasses="Numeric", na.strings ="-")#fetching data from the ClipboardData<-t (Matrix (Scan) ("FileName", sep=','), 5, 364)#read Big Data, read in 364 lines, have 5 numbers per line18Assigning a value to a variable data.entry (X) opens a graphical editor and can modify the value height by clicking on the appropriate cell<-c (1.75, 1.80, 1.65, 1.90, 1.74, 1.91seq (): Sequential generation, default by IS 1letters (): Sequential generation of letters, default by IS 119. View object type mode (x)#Display data typetypeof (x) Length (x)22. Get and Change object properties attributes (Y) str (y) Table (x) Dim (Y)#To view or change the dimensions of an object, this will be an array. dimnames (y) dimnames (y) $xclass(y)#determine the class of Yattr (Z,"Dim") <-C (10,10)#allow R to treat Z as a 10x10 matrix. 20. Type conversion as (x,"character") as.character (x) as.numeric (s) rbind (): The vector is combined into a matrix by rows, and Cbind () is reversed. Round (): The effect of rounding, the default conversion to an integer. Mean (): average. SUM (): Sum. Min (): Min Max (): Max value prod (): The number of data sets is multiplied. var (): Variance. SD (): Standard deviation. Which (): Gets the Subscript rev () of the vector: sort (): t (): Multiply matrix transpose matrix is a%*%B,a*B is the number multiplied by the relative position. Diag (): A diagonal vector for matrices, a diagonal matrix for vectors, and a unit vector matrix for a constant parameter. Solve (a): Finding the inverse matrix of matrices; Solve (A, B): Ask Ax=solution of the B-equation group. Rnorm (): Generates a normal distribution number. Rpois (): Poisson distribution Rexp (): Exponential distribution Rgamma (): Gamma distribution runif (): Evenly distributed Rbinom (): Two-item Distribution Rgeom (): Geometric distribution------------------------------------------------------>Library (SP)>Library (Maptools)>Library (Raster)> maxd3 = Readasciigrid ("Max.d3.txt")> rasterlayer.maxd3 =Raster (MAXD3)> maxd1 = Readasciigrid ("Max.d1.txt")> rasterlayer.maxd1 =Raster (MAXD1)> rasterlayer.max.d3.d1 = rasterlayer.maxd3-Rasterlayer.maxd1> rasterlayer.maxrange = rasterlayer.maxd1>0> maxrange = rastertopolygons (Rasterlayer.maxrange, dissolve=TRUE)>plot (Maxrange)> cols = grey.colors (20, 0.1, 0.9, 2.2)> Image (Maxd3, Col=cols, Useraster=true)
R language Learning