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", encoding= "UTF-8")
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" )))
This article from the "Empty" blog, reproduced please contact the author!
R language Notes