csv文件示例
Welcome! If you want to start diving into data science and statistics, then data frames, CSV files, and R will be essential tools for you. Let's see how you can use their amazing capabilities.
欢迎! 如果您想开始研究数据科学和统计学,那么数据框,CSV文件和R将是您必不可少的工具。 让我们看看如何使用它们的惊人功能。
In this article, you will learn:
在本文中,您将学习:
- What CSV files are and what they are used for. 什么是CSV文件以及它们的用途。
- How to create CSV files using Google Sheets. 如何使用Google表格创建CSV文件。
- How to read CSV files in R. 如何在R中读取CSV文件。
- What Data Frames are and what they are used for. 什么是数据帧以及它们的用途。
- How to access the elements of a data frame. 如何访问数据框的元素。
- How to modify a data frame. 如何修改数据框。
- How to add and delete rows and columns. 如何添加和删除行和列。
We will use RStudio, an open-source IDE (Integrated Development Environment) to run the examples.
我们将使用RStudio(开放源代码IDE(集成开发环境))运行示例。
Let's begin! 🔅
让我们开始! 🔅
CSV文件简介 (🔹 Introduction to CSV Files)
CSV (Comma-separated Values) files can be considered one of the building blocks of data analysis because they are used to store data represented in the form of a table. In this file, values are separated by commas to represent the different columns of the table, like in this example:
CSV(逗号分隔值)文件可被视为数据分析的基础模块之一,因为它们用于存储以表格形式表示的数据。 在此文件中,值用逗号分隔以代表表的不同列,如以下示例所示:
We will generate this file using Google Sheets.
我们将使用Google表格生成此文件。
🔸如何使用Google表格创建CSV文件 (🔸 How to Create a CSV File Using Google Sheets)
Let's create your first CSV file using Google Sheets.
让我们使用Google表格创建第一个CSV文件。
Step 1: Go to the Google Sheets Website and click on "Go to Google Sheets":
第1步:转到Google表格网站 ,然后点击“转到Google表格”:
💡 Tip: You can access Google Sheets by clicking on the button located at the top-right edge of Google's Home Page:
💡提示:您可以通过单击Google主页右上角的按钮来访问Google表格:
If we zoom in, we see the "Sheets" button:
如果放大,则会看到“表格”按钮:
💡 Tip: To use Google Sheets, you need to have a Gmail account. Alternatively, you can create a CSV file using MS Excel or another spreadsheet editor.
💡 提示:要使用Google表格,您需要拥有一个Gmail帐户。 或者,您可以使用MS Excel或其他电子表格编辑器创建CSV文件。
You will see this panel:
您将看到此面板:
Step 2: Create a blank spreadsheet by clicking on the "+" button.
步骤2:通过点击“ +”按钮创建一个空白电子表格。
Now you have a new empty spreadsheet:
现在您有了一个新的空电子表格:
Step 3: Change the name of the spreadsheet to students_data
. We will need to use the name of the file to work with data frames. Write the new name and click enter to confirm the change.
步骤3:将电子表格的名称更改为students_data
。 我们将需要使用文件名来处理数据帧。 输入新名称,然后单击Enter确认更改。
Step 4: In the first row of the spreadsheet, write the titles of the columns.
步骤4:在电子表格的第一行中,编写各列的标题。
When you import a CSV file in R, the titles of the columns are called variables. We will define six variables: first_name
, last_name
, age
, num_siblings
, num_pets
, and eye_color
, as you can see right here below:
在R中导入CSV文件时,列的标题称为变量 。 我们将定义六个变量: first_name
, last_name
, age
, num_siblings
, num_pets
和eye_color
,如下所示:
💡 Tip: Notice that the names are written in lowercase and words are separated with an underscore. This is not mandatory, but since you will need to access these names in R, it's very common to use this format.
💡 提示:请注意,名称以小写字母书写,单词之间用下划线分隔。 这不是强制性的,但是由于您将需要在R中访问这些名称,因此使用这种格式非常普遍。
Step 5: Enter the data for each one of the columns.
步骤5:为每一列输入数据。
When you read the file in R, each row is called an observation, and it corresponds to data taken from an individual, animal, object, or entity that we collected data from.
当您在R中读取文件时,每一行都称为观察值 ,它对应于从我们收集数据的个人,动物,物体或实体获取的数据。
In this case, each row corresponds to the data of a student:
在这种情况下,每一行对应于一个学生的数据:
Step 6: Download the CSV file by clicking on File -> Download -> Comma-separated values
, as you can see below:
步骤6:点击File -> Download -> Comma-separated values
下载CSV文件,如下所示: