Application of R language in finance II

3. File input and output and common errors

3.1 R script file input (open)

  • Using the Rstudio interface
  • Using R interface
  • Command open
file.edit("#dir",fileEncoding = "UTF-8") #Import. R file, import script file of R

3.2 R script file storage (saving)

  • Using the Rstudio interface
  • Using R interface

3.3 output of other documents (save)

  • Save the running results in the document:

    • Save the running results as. R or. txt file → sink()
    • Note:
     #R script file output (save)
     #Save run results, sink()
     sink("R The application of language in Finance/sink.R") #Start line #Where and what name do you want to save the file
     x <- 2
     y <- x * 2
     cat("x=",x,"\n") #\n line feed
     cat("y=",y)
     sink() #End line
    
  • Save existing objects:

    • Save the running results as. Rdata → save.image()
    #Save objects in R workspace
    save.image("R The application of language in Finance/sink.RData")#Save existing objects
    load("R The application of language in Finance/sink.RData") #Import RData file
    
    • Using the Rstudio interface

3.4 graphic output (save)

  • Using the Rstudio interface

  • command

    #Drawing saving
    #Save as PDF file
    pdf("R The application of language in Finance/sink.pdf") #Open statement
    plot(1:10,2:11,type="l") #Drawing
    dev.off() #Closing statement, must have
    
    #Save as png file
    png("E:/R CLASS/Chapter I/code/figure1.png")
    plot(1:10,2:11,type="l") #Drawing
    dev.off()
    
    #Save as jpeg file
    jpeg("E:/R CLASS/Chapter I/code/figure1.jpg")
    plot(1:10,2:11,type="l") #Drawing
    dev.off()
    
    #Save as bmp file
    bmp("E:/R CLASS/Chapter I/code/figure1.bmp")
    plot(1:10,2:11,type="l") #Drawing
    dev.off()
    

3.5 common functions

  • Run the R script file:

    • Using the Rstudio interface

    • command

      #Run script file
      source("E:\\R CLASS\\Chapter I\\code\\figure.R",encoding = "UTF-8")###Run the figure.R file
      
  • List folders and files in the current directory:

    #Lists the folders and files in the current directory
    list.dirs("D:/Rwork") #Lists the folders in the current working directory
    list.files("D:/Rwork") #Lists the files in the current working directory
    

4. Data type

#View the data type of object x
class(x) 

#View the length of object x
length(x)

#Judge data type
is.numeric() #Numeric or not
is.character() #Character type
is.logical() #Logical or not
is.complex() #Plural or not

#Data type conversion
as.numeric() #Convert to numeric
as.character() #Convert to character
as.logical() #Convert to logical
as.complex() #Convert to plural

4.1 object

	Object is R The entities created and operated by language can be variables, arrays, strings, functions and other structures composed of these elements. All combinations of objects stored in the current working path are called workspaces(workspace). 

4.2 five basic types

4.2.1 numerical type:

  • It is the simplest type of object, integer, decimal or scientific counting. It is integer or double precision. Double precision is the default

  • Data operation: add +, subtract -, multiply *, divide /, integer division% /%, remainder%%, power calculation ^ or * *, strictly equal to = =, not equal to=

4.2.2 character type:

  • A string in single and double quotation marks

  • In the case of double quotation marks, if you want to output double quotation marks, you can add double quotation marks after the backslash \;

  • If double quotation marks are output in the case of single quotation marks, you do not need to add backslashes, but directly add double quotation marks;

4.2.3 logical:

  • Only T(TRUE) or F(FALSE) can be taken

  • TRUE is 1 and FALSE is 0;

  • Logical operation:

    • Logic and & (compare all elements in two objects one by one and return the comparison result one by one. It is required that the length of two objects must be the same);
    • &&(only the first element of two objects is compared, and there is no requirement for the length of two objects).
    • Logical or | (compare all elements in the two objects one by one, and return the comparison result one by one. It is required that the length of the two objects must be the same);
    • ||(only the first element of two objects is compared, and there is no requirement for the length of two objects).

4.2.4 complex

Complex operation is supported in R, a+bi

#m is a complex number
Re(m) #Take out the real part
Im(m) #Remove the imaginary part
Mod() #Module for calculating complex numbers
Conj() #Computing the conjugate of complex numbers

Keywords: R Language

Added by silvermice on Fri, 17 Sep 2021 23:02:29 +0300