找回密码
 注册
查看: 6494|回复: 0

R语言:Extract.data.frame()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 21:01:04 | 显示全部楼层 |阅读模式
Extract.data.frame(base)
Extract.data.frame()所属R语言包:base

                                        Extract or Replace Parts of a Data Frame
                                         数据框中提取或替换零件

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Extract or replace subsets of data frames.
提取或替换的数据框的子集。


用法----------Usage----------


## S3 method for class 'data.frame'
x[i, j, drop = ]
## S3 replacement method for class 'data.frame'
x[i, j] <- value
## S3 method for class 'data.frame'
x[[..., exact = TRUE]]
## S3 replacement method for class 'data.frame'
x[[i, j]] <- value
## S3 replacement method for class 'data.frame'
x$name <- value



参数----------Arguments----------

参数:x
data frame.
数据框。


参数:i, j, ...
elements to extract or replace.  For [ and [[, these are numeric or character or, for [ only, empty.  Numeric values are coerced to integer as if by as.integer.  For replacement by [, a logical matrix is allowed.
元素提取或更换。对于[和[[,这些都是numeric或character或[只,空。数值仿佛被强制为整数as.integer。对于更换[,允许一个逻辑矩阵。


参数:name
A literal character string or a name (possibly backtick quoted).
文字字符串或名称(可能是反引号引述)。


参数:drop
logical.  If TRUE the result is coerced to the lowest possible dimension.  The default is to drop if only one column is left, but not to drop if only one row is left.
逻辑。如果TRUE结果被强迫到尽可能低的层面。默认是放弃离开,如果只有一列,但不要放弃,如果只有一行离开。


参数:value
A suitable replacement value: it will be repeated a whole number of times if necessary and it may be coerced: see the Coercion section.  If NULL, deletes the column if a single column is selected.
一个合适的替代值:将反复多次的整数,如果有必要,它可能会被强制:强制部分。如果NULL,删除列,如果被选中的单个列。


参数:exact
logical: see [, and applies to column names.
逻辑:看到[,适用于列名。


Details

详情----------Details----------

Data frames can be indexed in several modes.  When [ and [[ are used with a single index (x[i] or x[[i]]), they index the data frame as if it were a list.  In this usage a drop argument is ignored, with a warning.
在几种模式下,数据框可以被索引。当[和[[(x[i]或x[[i]]用一个单一的指数),指数的数据框,如果它是一个列表。在这种用法drop参数将被忽略,一个警告。

Note that there is no data.frame method for $, so x$name uses the default method which treats x as a list.  There is a replacement method which checks value for the correct number of rows, and replicates it if necessary.
请注意,有没有data.frame$方法,所以x$name使用默认的方法对待x作为一个列表。有一种替代方法检查value正确的行数,重复,如有必要。

When [ and [[ are used with two indices (x[i, j] and x[[i, j]]) they act like indexing a matrix:  [[ can only be used to select one element.  Note that for each selected column, xj say, typically (if it is not matrix-like), the resulting column will be xj[i], and hence rely on the corresponding [ method, see the examples section.
当[和[[用两个指数(x[i, j]和x[[i, j]])他们像索引矩阵:[[只能被用来选择一个元素。请注意,每个选定列,xj说,通常(如果它不是像矩阵),结果列将是xj[i],因此依赖于相应的[方法,请参阅示例一节。

If [ returns a data frame it will have unique (and non-missing) row names, if necessary transforming the row names using make.unique.  Similarly, if columns are selected column names will be transformed to be unique if necessary (e.g. if columns are selected more than once, or if more than one column of a given name is selected if the data frame has duplicate column names).
如果[返回一个数据框,它具有独特的(非缺失),行名,如果有必要改变行的名称,使用make.unique。同样,如果选择列的列名,将被改造成为独特的,如果有必要(例如,如果选择列不止一次,或一个给定的名称,如果多个列被选中,如果数据框有重复的列名)。

When drop = TRUE, this is applied to the subsetting of any matrices contained in the data frame as well as to the data frame itself.
当drop = TRUE,这是适用于在数据框以及数据框本身所载的任何矩阵的子集。

The replacement methods can be used to add whole column(s) by specifying non-existent column(s), in which case the column(s) are added at the right-hand edge of the data frame and numerical indices must be contiguous to existing indices.  On the other hand, rows can be added at any row after the current last row, and the columns will be in-filled with missing values.  Missing values in the indices are not allowed for replacement.
更换方法可以用来指定不存在的列(S),列(S),在这种情况下,添加在右手边的数据框和数值指标,以增加整列(S),必须是连续的现有指数。另一方面,行可以加在当前的最后一排后的任何行,列将在充满遗漏值。失踪指数值不得更换。

For [ the replacement value can be a list: each element of the list is used to replace (part of) one column, recycling the list as necessary.  If columns specified by number are created, the names (if any) of the corresponding list elements are used to name the columns.  If the replacement is not selecting rows, list values can contain NULL elements which will cause the corresponding columns to be deleted.  (See the Examples.)
[替换值可以是一个列表:列表中的每个元素被用来替换(部分)一列,必要的回收列表。如果由数字指定的列创建,相应的列表元素的名称(如有)被用来命名的列。如果更换不选择行,列表中的值可以包含NULL元素,这将导致相应的列被删除。 (见例子)。

Matrix indexing (x[i] with a logical or a 2-column integer matrix i) using [ is not recommended, and barely supported.  For extraction, x is first coerced to a matrix. For replacement, a logical matrix (only) can be used to select the elements to be replaced in the same way as for a matrix.
矩阵索引(x[i]逻辑或2列整数矩阵i)[不推荐,勉强支持。提取,x第一次被强制到一个矩阵。更换,逻辑矩阵(只),可用于选择要在同样的方式为矩阵替换元素。

Both [ and [[ extraction methods partially match row names.  By default neither partially match column names, but [[ will unless exact=TRUE.  If you want to do exact matching on row names use match as in the examples.
既[和[[方法提取部分匹配的行名。默认情况下,既不部分匹配的列名,但[[除非exact=TRUE。如果你想要做精确匹配就行名称中使用match例子。


值----------Value----------

For [ a data frame, list or a single column (the latter two only when dimensions have been dropped).  If matrix indexing is used for extraction a matrix results.  If the result would be a data frame an error results if undefined columns are selected (as there is no general concept of a 'missing' column in a data frame).  Otherwise if a single column is selected and this is undefined the result is NULL.
[一个数据框,列表或单个列(后者只有当尺寸已经下降)。如果矩阵索引被用于提取矩阵结果。如果其结果将是一个数据框错误结果,如果未定义列选中(因为没有一个失踪列在一个数据框的一般概念)。否则,如果选择单个列,这是不确定的结果是NULL。

For [[ a column of the data frame or NULL (extraction with one index) or a length-one vector (extraction with two indices).
[[的数据框(同一个索引中提取),或一个向量的长度(两个指数提取)NULL列。

For $, a column of the data frame (or NULL).
$,列的数据框(或NULL)。

For [<-, [[<- and $<-, a data frame.
[<-,[[<-和$<-,一个数据框。


强迫----------Coercion----------

The story over when replacement values are coerced is a complicated one, and one that has changed during R's development.  This section is a guide only.
过的故事被强制替换值时,是一个复杂的,一个R的发展过程中改变。本节是仅供参考。

When [ and [[ are used to add or replace a whole column, no coercion takes place but value will be replicated (by calling the generic function rep) to the right length if an exact number of repeats can be used.
当[和[[用来增加或更换一整列,没有强迫发生,但value将被复制的权利(通过调用泛型函数rep)长度可以用来重复的确切人数。

When [ is used with a logical matrix, each value is coerced to the type of the column into which it is to be placed.
当[逻辑矩阵,每个值被强迫把它放置到列类型。

When  [ and [[ are used with two indices, the column will be coerced as necessary to accommodate the value.
当[和[[两个指数,该列将被强制为必要的适应值。

Note that when the replacement value is an array (including a matrix) it is not treated as a series of columns (as data.frame and as.data.frame do) but inserted as a single column.
注意时,替换值是一个数组(包括矩阵),它不被视为一系列的列(data.frame和as.data.frame做的),但作为一个单独的列中插入。


警告----------Warning----------

The default behaviour when only one row is left is equivalent to specifying drop = FALSE.  To drop from a data frame to a list, drop = TRUE has to be specified explicitly.
只有一行离开时的默认行为是相当于指定drop = FALSE。从一个数据框下降到一个列表,drop = TRUE必须明确指定。

Arguments other than drop and exact should not be named: there is a warning if they are and the behaviour differs from the description here.
参数以外drop和exact不应该被命名为:有一个警告,如果他们的行为,从这里的描述不同。


参见----------See Also----------

subset which is often easier for extraction, data.frame, Extract.
subset这往往是容易提取,data.frame,Extract。


举例----------Examples----------


sw &lt;- swiss[1:5, 1:4]  # select a manageable subset[选择一个可管理的子集]

sw[1:3]      # select columns[选择列]
sw[, 1:3]    # same[同]
sw[4:5, 1:3] # select rows and columns[选择行和列]
sw[1]        # a one-column data frame[一列的数据框]
sw[, 1, drop = FALSE]  # the same[同一]
sw[, 1]      # a (unnamed) vector[(无名)向量]
sw[[1]]      # the same[同一]

sw[1,]       # a one-row data frame[一个行数据框]
sw[1,, drop=TRUE]  # a list[列表]

sw["C", ] # partially matches[部分匹配]
sw[match("C", row.names(sw)), ] # no exact match[没有完全匹配]
try(sw[, "Ferti"]) # column names must match exactly[列名必须完全匹配]


swiss[ c(1, 1:2), ]   # duplicate row, unique row names are created[重复的行,创建独特的行名]

sw[sw &lt;= 6] &lt;- 6  # logical matrix indexing[逻辑矩阵索引]
sw

## adding a column[#添加一列]
sw["new1"] &lt;- LETTERS[1:5]   # adds a character column[添加一个字符列]
sw[["new2"]] &lt;- letters[1:5] # ditto[同上]
sw[, "new3"] &lt;- LETTERS[1:5] # ditto[同上]
sw$new4 <- 1:5
sapply(sw, class)
sw$new4 &lt;- NULL              # delete the column[删除列]
sw
sw[6:8] <- list(letters[10:14], NULL, aa=1:5)
# update col. 6, delete 7, append[更新山坳。 6,删除,追加]
sw

## matrices in a data frame[#在一个数据框的矩阵]
A <- data.frame(x=1:3, y=I(matrix(4:6)), z=I(matrix(letters[1:9],3,3)))
A[1:3, "y"] # a matrix[矩阵]
A[1:3, "z"] # a matrix[矩阵]
A[, "y"]    # a matrix[矩阵]

## keeping special attributes: use a class with a[特殊属性:#保持使用一个类]
## "as.data.frame" and "[" method:[#“as.data.frame的”和“[”方法:]


as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function(x,i,...) {
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  r
}

d <- data.frame(i= 0:7, f= gl(2,4),
                u= structure(11:18, unit = "kg", class="avector"))
str(d[2:4, -1]) # 'u' keeps its "unit"[U保持其“单位”]


转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-2-3 04:44 , Processed in 0.028798 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表