官术网_书友最值得收藏!

How to do it…

Let's take a look at the following steps to learn how to create a data frame in R:

  1. 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"))
  1. 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
  2. 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
主站蜘蛛池模板: 滕州市| 丹棱县| 钟祥市| 宜都市| 台安县| 桓仁| 延寿县| 通渭县| 雅安市| 大荔县| 山西省| 兴业县| 岳池县| 蒙山县| 平度市| 乳山市| 中江县| 德昌县| 陆丰市| 中阳县| 秀山| 万盛区| 抚远县| 湟源县| 长白| 芦溪县| 宁津县| 吉林市| 柞水县| 克山县| 年辖:市辖区| 博客| 大理市| 郓城县| 息烽县| 察哈| 监利县| 农安县| 夏津县| 宾川县| 汾阳市|