R Language Basics

Source: Internet
Author: User

= = = Basic Knowledge = = =
1. Assigning values
X <-3
Y <-4
Z <-sqrt (x^2 + y^2)

2. Create a vector
C (...)

3. Sequence values
1:5 1 2 3 4 5
Seq (from=1, to=5, by=2) sequence functions
Rep (1, times=5) repeats the same value

4. Function definition
function (param1, ...., paramn) expr

5. Read and write CSV files
Read.csv
Write.csv

6. Search Path
Search ()

7. Loading the Library
Library ()

8. Installation package
Install.packages ("PackageName")

9. How to execute Scripts
Source ("Hello." R ")
Rscript scriptfile arg1 arg2 arg3
R-CMD BATCH scriptfile outputfile

10. Get and set environment variables
Sys.getenv ("SHELL")
Sys.setenv (shell= "/bin/sh")

11. Connect to MySQL
Library (Rmysql)
Con <-dbconnect (MySQL (), user= "userid", password= "pswd",
Host= "hostname", client.flag=client_multi_results)

12. Vectors
One-dimensional array for storing numeric, character, or logical data. function C () that performs the combined function can be used to
Create a vector; we can access the elements in the vector by the value of the position of the given element in square brackets:
Exp:a[c (2, 4)]
Used to access the second and fourth elements in vector a.

13. Matrix
A two-dimensional array, except that each element has the same pattern (numeric, character, or logical)
Matrices can be created from the function matrix. We can use subscript and square brackets to select rows in the Matrix,
Column or element. X[i,] refers to the matrix X in the line I, X[,j] refers to column J, X[i, J] refers to the first line of J elements.

14. Arrays (Array)
The matrix is similar, but the dimension can be greater than 2. Arrays can be created by using the array function
The data in the array can also have only one pattern.
Because different columns can contain data for different patterns (numeric, character, and so on), the concept of the data frame
More general than the matrix. It's similar to the datasets you see in SAS, SPSS, and Stata. Data frame
will be the data structure you most often work with in R. The pattern for each column of data must be unique, but you can add multiple
The different columns of the pattern are put together to form a data frame. Because the data frame is the shape of the data set that analysts typically envision
Closer, we will use the term columns and variables interchangeably when we discuss the data frame. How to select elements in a data frame
There are several kinds. You can use the preceding (such as in The Matrix) mark number, or directly specify the column name.

15. Lists (list)
One of the most complex types of data in R. In general, a list is a number of objects (or components, component)
An ordered set. Lists allow you to integrate several (possibly unrelated) objects into a single object name. For example, in a list
May be a combination of several vectors, matrices, data frames, and even other lists. List can be created using the function list ()

16.RODBC Connection Database
Odbcconnect (dsn,uid= "", pwd= "") Establish a connection to the ODBC database
SqlFetch (channel,sqltable) reads a table from an ODBC database into a data frame
SQLQuery (channel,query) submits a query to the ODBC database and returns the results
Sqlsave (Channel,mydf,tablename= writes or updates the data frame (append=true) to the ODBC database
Sqtable,append=false) in a table
Sqldrop (channel,sqtable) Deleting a table in an ODBC database
Close (channel) Closed connection

17. Utility Functions
Ength (object) Displays the number of elements/components in an object
Dim (object) Displays the dimensions of an object
Str (object) shows the structure of an object
Class (object) displays the classes or types of an object
Mode (object) shows the pattern of an object
Names (object) displays the names of the components in an object
C (Object, Object,...) Merging objects into a vector
Cbind (Object, object, ...) Merging objects by column
Rbind (Object, object, ...) Merging objects by rows
An object is exported by object
Head (object) lists the starting part of an object
Tail (object) lists the last part of an object
LS () Displays the current list of objects
Rm (Object, object, ...) Delete one or more objects
NewObject <-Edit object and save as NewObject
Fix (object) directly edits objects
Plot a generic function that can draw multiple types of x, Y axis graphics
Par change graphics parameters
Parplot Bar graph function
BoxPlot Box Line diagram function

R Language Basics

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.