- Modern R Programming Cookbook
- Jaynal Abedin
- 229字
- 2021-07-08 09:48:32
How to do it…
Let's take a look at the following steps to learn how to create a data frame in R:
- To create a data frame in R, you will have to use a function called data.frame(). This function is the convenient way to create a data frame. Within the data.frame() function, it contains the named vector that ultimately represents columns of the dataset. Each column's data type could be very different from another. For example, one column could be numeric, another column could be character, and the other columns could be logical. Here is an example of creating a small data frame object using the data.frame() function:
datA <- data.frame(ID = 1:5, hourSpetOnInternet =
c(5,3,4,1,2), GENDER = c("M", "F", "F", "M", "F"))
- After creating the data frame, you can now check the properties of it as follows:
- Data type of each of the columns
- Number of rows
- Number of columns
- Names of the columns
- Printing the content of the data frame
- Printing the first and last few rows of the data frame
- Accessing a single column
- To determine the data types of each column, execute the following code snippet:
str(datA)
> str(datA)'data.frame': 5 obs. of 3 variables:
$ ID : int 1 2 3 4 5
$ hourSpetOnInternet: num 5 3 4 1 2
$ GENDER : Factor w/ 2 levels "F","M": 2 1 1 2 1
nrow(datA) # to know number of rows in the data frame
ncol(datA) # to know number of columns in the data frame
head(datA, n=2) # print first 2 rows of the data frame
tail(datA, n=2) # print last 2 rows of the data frame
datA$ID # to get access to ID variable only
datA[["ID"]] # to get access to ID variable only
names(datA) # to get column names of the data frame
colnames(datA) # to get column names of the data frame
推薦閱讀
- What's New in TensorFlow 2.0
- jQuery EasyUI網站開發實戰
- 從0到1:HTML+CSS快速上手
- C語言程序設計實踐教程
- Java 9模塊化開發:核心原則與實踐
- 碼上行動:用ChatGPT學會Python編程
- 速學Python:程序設計從入門到進階
- Java并發編程之美
- Android移動應用開發項目教程
- Node.js 6.x Blueprints
- Learning TypeScript
- PHP 7 Programming Blueprints
- AI輔助編程Python實戰:基于GitHub Copilot和ChatGPT
- Azure for Architects
- Python自動化運維:技術與最佳實踐