Article directory
foreword
in the previousR language - system installation (R4.3.0) and basic usage, we have learned how to installR softwareandRS studio, Next, we will further study the basic instructions of the R language.
1. Basic instructions in R language
In R language, we usecommandTo interact with R, these commands can be entered in the Script or Console interface of the R window, and then press Enter key executionThat's it.
2. What are the ways to import files into R?
In the R language, there are many ways to input files, the following are some of the commonly used ways:
read. table()
function: Used to input text files, such as CSV, TSV, etc.read.csv()
function:andread. table()
Similar function for importing files in CSV format.readRDS()
function: Used to import files in RDS format. RDS files are R-specific binary formats that are commonly used to store objects in R.load()
function: Used to import files in RData format. RData files are R-specific binary formats that are commonly used to store entire work environments.
The above methods are common methods for inputting files in R language, and of course there are other methods, such as read. table2()
,read.csv2()
,readLines()
And so on, you can choose the most suitable method according to different needs.
in,header = TRUE
Indicates that the first row is a header row,sep = "\t"
Indicates that the separator is Tab. If the .CSV file does not have a header row, you can replace the header
The parameter is set to FALSE
,
3. Basic operations of R
The R language supports basic arithmetic symbols such as the plus sign (+
), minus sign (-
),Multiplication sign(*
), division sign (/
)wait. Here are some simple examples:
4. Variables and data types
In R language, we can use variables to store data. Before using a variable, we need to give it a name, for example x
or the y
, then use the equals sign (=
)or <-
to specify the value of the variable. I personally like to use <-
,becauseThis way of writing is more concise, and it can also avoid confusion with the parameters in the function.
In addition, variables can also store different data types, such as integers, floating-point numbers, strings, and so on. Here are some common data types and examples:
5. Vectors and DataFrames
in Rvectoris a sequence of the same data type, you can use c()
function to create. Here is an example:
also,data frame (data.frame)It is one of the most commonly used data structures in R, similar to Excel tables. A data frame can be composed of multiple columns, and each column can have different data types. The following is an example:
Here is a little explanation. When creating a data frame, we need to specify the name and value of each field. You can use data. frame()
function to create. In the above example, we created a file named df
A data frame with three fields:name
,age
and student
, the result is shown in the console.
6. Functions
In the R language,functionIt is an independent unit that can perform a specific task, accept parameters (parameter) and output results. Here is an example:
In the above example, we defined a class named square
function that accepts a parameter x
, and calculate x
squared. use return()
keyword to pass calculation results back and forth. Finally, we use square()
function to calculate the square of 3 and store the result in the y
variable.
7. Data processing
There are many built-in functions and packages in R language that can be used for data processing, such as data selection, data filtering, data merging, and so on. Here is an example showing how to use dplyr
suiteData selection and screening: [Note: The method of downloading dplyrinstall.packages(“dplyr”)]
In the above example, we use data. frame()
function creates a df
A data frame with three fields. Next, we use dplyr
in the kit %>%
operator, will df
The data frame is passed to select()
function and filter()
Functions for data selection and filtering. Finally, we store the selected data in the result
variable.
Summarize
The above are some basic commands and examples of R language. These basic instructions are the necessary knowledge for learning R language, which can help beginners to perform basic data processing and analysis. If you think the writing is okay, please share the information.By the way, welcome to R world!