R CSV Files
As a professional statistical tool, R would be meaningless if data could only be imported and exported manually, so R supports batch retrieval of data from mainstream tabular storage formats (such as CSV, Excel, XML, etc.).
CSV table interaction
CSV (Comma-Separated Values, sometimes also called character-separated values because the delimiter need not be a comma) is a very popular tabular storage file format, suitable for storing small to medium-sized datasets.
Since most software supports this file format, it is commonly used for data storage and exchange.
CSV is essentially text, and its file format is extremely simple: data is saved line by line as text, each record is separated into fields by a delimiter, and each record has the same sequence of fields.
The following is a simple sites.csv file (stored in the same directory as the test program):
id,name,url,likes
1,Google,www.google.com,111
2,Runoob,www.runoob.com,222
3,Taobao,www.taobao.com,333
CSV uses commas to separate columns; if the data contains a comma, the entire data block must be enclosed in double quotes.
**Note:** for text containing non-English characters, pay attention to the encoding used for saving. Since many computers commonly use UTF-8 encoding, I saved it using UTF-8.
Note: The last line of a CSV file needs to keep a blank line, otherwise the program will produce a warning message.
Warning message:
In read.table(file = file, header = header, sep = sep, quote = quote, :
incomplete final line found by readTableHeader on 'sites.csv'

Read the CSV file
Next we can use the read.csv() function to read data from a CSV file:
Example
data
<-
read.csv
(
"sites.csv"
, encoding
=
"UTF-8"
)
(
data
)
If the encoding attribute is not set, the read.csv function will read using the operating system's default text encoding. If you are using a Chinese version of Windows and have not set the system's default encoding, the system default should be GBK. Therefore, please unify text encoding as much as possible to avoid errors.
The output of the code above is:
id name url likes
1 1 Google www.google.com 111
2 2 Runoob www.runoob.com 222
3 3 Taobao www.taobao.com 333
The read.csv() function returns a data frame, so we can easily perform statistical processing on the data. In the following example, we check the number of rows and columns:
Example
data
<-
read.csv
(
"sites.csv"
, encoding
=
"UTF-8"
)
(
is.data.frame
(
data
)
)
Check whether it is a data frame
(
ncol
(
data
)
)
The number of columns
(
nrow
(
data
)
)
Number of rows
The output of the code above is:
[1] TRUE
[1] 4
[1] 3
The following finds the data with the largest likes field in the data frame:
Example
data
<-
read.csv
(
"sites.csv"
, encoding
=
"UTF-8"
)
Data with the largest likes
like
<-
max
(
data
$likes
)
(
like
)
The output of the code above is:
[1] 333
We can also specify search conditions to query data similar to an SQL where clause; the function needed is subset()。
The following example finds data where likes is 222:
Example
data
<-
read.csv
(
"sites.csv"
, encoding
=
"UTF-8"
)
Data where likes is 222
retval
<-
subset
(
data
, likes
==
222
)
(
retval
)
The output of the code above is:
id name url likes
2 2 Runoob www.runoob.com 222
Note: a conditional statement equal to using ==.
Multiple conditions use the & separator; the following example finds data where likes is greater than 1 and name is Runoob:
Example
data
<-
read.csv
(
"sites.csv"
, encoding
=
"UTF-8"
)
Data where likes is greater than 1 and name is Runoob
retval
<-
subset
(
data
, likes
1
&
name
==
"Runoob"
)
(
retval
)
The output of the code above is:
id name url likes
2 2 Runoob www.runoob.com 222
Save as a CSV file
The R language can use write.csv() function to save data as a CSV file.
Continuing the above example, we save the data where likes is 222 to the runoob.csv file:
Example
data
<-
read.csv
(
"sites.csv"
, encoding
=
"UTF-8"
)
Data where likes is 222
retval
<-
subset
(
data
, likes
==
222
)
Write a new file
write.csv
(
retval,
"runoob.csv"
)
newdata
<-
read.csv
(
"runoob.csv"
)
(
newdata
)
The output of the code above is:
X id name url likes
1 2 2 Runoob www.runoob.com 222
X comes from the dataset newper and can be removed with the argument row.names = FALSE:
Example
data
<-
read.csv
(
"sites.csv"
, encoding
=
"UTF-8"
)
Data where likes is 222
retval
<-
subset
(
data
, likes
==
222
)
Write a new file
write.csv
(
retval,
"runoob.csv"
,
row.names
=
FALSE
)
newdata
<-
read.csv
(
"runoob.csv"
)
(
newdata
)
The output of the code above is:
id name url likes
1 2 Runoob www.runoob.com 222
After execution, we can see that the runoob.csv file has been generated:
