R language Notes

Source: Internet
Author: User

Several recent projects have been done using the R language. As the teacher said, learning quickly and forgetting quickly. Tidy up and put it here for easy reference later.

To install the required packages:
Install.packages ("xxxx")

Load Package:
Library ("xxxx")

Show/Change Working directory
GETWD ()/setwd ("D:/temp")

File operation:
1. Read the CSV file
Var=read.csv ("Demo.csv", Header=false)--header indicates if there is a table header, default is True

2. Reading data files
Data=read.table ("Demo.data")

3. Reading a text file
Var=readlines ("Demo.txt")

4. Output CSV file
Write.csv (Var, "Var.csv")

5. Output Plain text files
Write (Var, "Var.txt")

6. Save the entire running space to a file
Save.image ("Demo. RData ")

7. read files to run space
Load ("demo. RData ")

Clear all variables for run space:
RM (List=ls ())

String manipulation:
1. Get the string length
NCHAR ("ASDF")--different from length

2. Connection string
Paste ("abc", "Def")--default with space interval, if cancel interval, available parameter sep= ""

3. Splitting a string
Str1=strsplit ("ABC def", "")--the partition result is a list type, if you want to convert to a vector using str1=unlist (Strsplit ("ABC def", "" "))

4. String interception
substr ("Hello", 2, 3)--parameter indicates the starting and ending position

5. String substitution
Gsub ("A", "C", "ASDFASDF")--replace ASDFASDF with CSDFCSDF

6. String Matching
grep ("oo", C ("Hello", "World", "good"))--searches for a specific expression in a given string, returns its position index

Collection (vector) operations:
1. Finding the intersection
Intersect (C ("A", "B", "C"), C ("A", "C", "D"))

2. Seek and set
Union (C ("A", "B", "C"), C ("A", "C", "D"))

3. Finding the difference set
Setdiff (C ("A", "B", "C"), C ("A", "C", "D"))

4. Vector de-weight
Unique (C ("A", "B", "A", "C"))

5. Vector sorting
Sort (C ("A", "B", "A", "C"))/rev (Sort (C ("A", "B", "A", "C")))

R language Notes

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.