经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » R语言 » 查看文章
R Data Frame
来源:cnblogs  作者:功夫 熊猫  时间:2019/8/7 8:50:48  对本文有异议

https://www.datamentor.io/r-programming/data-frame/

Check if a variable is a data frame or not

We can check if a variable is a data frame or not using the class() function.

  1. > x
  2. SN Age Name
  3. 1 1 21 John
  4. 2 2 15 Dora
  5. > typeof(x) # data frame is a special case of list
  6. [1] "list"
  7. > class(x)
  8. [1] "data.frame"

In this example, x can be considered as a list of 3 components with each component having a two element vector. Some useful functions to know more about a data frame are given below.


Functions of data frame

  1. > names(x)
  2. [1] "SN" "Age" "Name"
  3. > ncol(x)
  4. [1] 3
  5. > nrow(x)
  6. [1] 2
  7. > length(x) # returns length of the list, same as ncol()
  8. [1] 3

How to create a Data Frame in R?

We can create a data frame using the data.frame() function

For example, the above shown data frame can be created as follows.

  1. > x <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John","Dora"))
  2. > str(x) # structure of x
  3. 'data.frame': 2 obs. of 3 variables:
  4. $ SN : int 1 2
  5. $ Age : num 21 15
  6. $ Name: Factor w/ 2 levels "Dora","John": 2 1

Notice above that the third column, Name is of type factor, instead of a character vector.

By default, data.frame() function converts character vector into factor.

To suppress this behavior, we can pass the argument stringsAsFactors=FALSE.

  1. > x <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John", "Dora"), stringsAsFactors = FALSE)
  2. > str(x) # now the third column is a character vector
  3. 'data.frame': 2 obs. of 3 variables:
  4. $ SN : int 1 2
  5. $ Age : num 21 15
  6. $ Name: chr "John" "Dora"

Many data input functions of R like, read.table()read.csv()read.delim()read.fwf() also read data into a data frame.


How to access Components of a Data Frame?

Components of data frame can be accessed like a list or like a matrix.


Accessing like a list

We can use either [[[ or $ operator to access columns of data frame.

  1. > x["Name"]
  2. Name
  3. 1 John
  4. 2 Dora
  5. > x$Name
  6. [1] "John" "Dora"
  7. > x[["Name"]]
  8. [1] "John" "Dora"
  9. > x[[3]]
  10. [1] "John" "Dora"

Accessing with [[ or $ is similar. However, it differs for [ in that, indexing with [ will return us a data frame but the other two will reduce it into a vector.


Accessing like a matrix

Data frames can be accessed like a matrix by providing index for row and column.

To illustrate this, we use datasets already available in R. Datasets that are available can be listed with the command library(help = "datasets").

We will use the trees dataset which contains GirthHeight and Volume for Black Cherry Trees.

A data frame can be examined using functions like str() and head().

  1. > str(trees)
  2. 'data.frame': 31 obs. of 3 variables:
  3. $ Girth : num 8.3 8.6 8.8 10.5 10.7 10.8 11 11 11.1 11.2 ...
  4. $ Height: num 70 65 63 72 81 83 66 75 80 75 ...
  5. $ Volume: num 10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 ...
  6. > head(trees,n=3)
  7. Girth Height Volume
  8. 1 8.3 70 10.3
  9. 2 8.6 65 10.3
  10. 3 8.8 63 10.2

We can see that trees is a data frame with 31 rows and 3 columns. We also display the first 3 rows of the data frame.

Now we proceed to access the data frame like a matrix.

  1. > trees[2:3,] # select 2nd and 3rd row
  2. Girth Height Volume
  3. 2 8.6 65 10.3
  4. 3 8.8 63 10.2
  5. > trees[trees$Height > 82,] # selects rows with Height greater than 82
  6. Girth Height Volume
  7. 6 10.8 83 19.7
  8. 17 12.9 85 33.8
  9. 18 13.3 86 27.4
  10. 31 20.6 87 77.0
  11. > trees[10:12,2]
  12. [1] 75 79 76

We can see in the last case that the returned type is a vector since we extracted data from a single column.

This behavior can be avoided by passing the argument drop=FALSE as follows.

  1. > trees[10:12,2, drop = FALSE]
  2. Height
  3. 10 75
  4. 11 79
  5. 12 76

How to modify a Data Frame in R?

Data frames can be modified like we modified matrices through reassignment.

  1. > x
  2. SN Age Name
  3. 1 1 21 John
  4. 2 2 15 Dora
  5. > x[1,"Age"] <- 20; x
  6. SN Age Name
  7. 1 1 20 John
  8. 2 2 15 Dora

Adding Components

Rows can be added to a data frame using the rbind() function.

  1. > rbind(x,list(1,16,"Paul"))
  2. SN Age Name
  3. 1 1 20 John
  4. 2 2 15 Dora
  5. 3 1 16 Paul

Similarly, we can add columns using cbind().

  1. > cbind(x,State=c("NY","FL"))
  2. SN Age Name State
  3. 1 1 20 John NY
  4. 2 2 15 Dora FL

Since data frames are implemented as list, we can also add new columns through simple list-like assignments.

  1. > x
  2. SN Age Name
  3. 1 1 20 John
  4. 2 2 15 Dora
  5. > x$State <- c("NY","FL"); x
  6. SN Age Name State
  7. 1 1 20 John NY
  8. 2 2 15 Dora FL

Deleting Component

Data frame columns can be deleted by assigning NULL to it.

  1. > x$State <- NULL
  2. > x
  3. SN Age Name
  4. 1 1 20 John
  5. 2 2 15 Dora

Similarly, rows can be deleted through reassignments.

  1. > x <- x[-1,]
  2. > x
  3. SN Age Name
  4. 2 2 15 Dora

原文链接:http://www.cnblogs.com/kungfupanda/p/11312830.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号