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