{"id":640,"date":"2014-06-17T13:26:31","date_gmt":"2014-06-17T18:26:31","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=640"},"modified":"2014-06-17T13:26:31","modified_gmt":"2014-06-17T18:26:31","slug":"extract-or-replace-parts-of-a-data-frame","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/06\/17\/extract-or-replace-parts-of-a-data-frame\/","title":{"rendered":"Extract or Replace Parts of a Data Frame"},"content":{"rendered":"<table summary=\"page for Extract.data.frame {base}\" width=\"100%\">\n<tbody>\n<tr>\n<td>Extract.data.frame {base}<\/td>\n<td align=\"right\">R Documentation<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"font-weight: bolder; color: #006699;\">Extract or Replace Parts of a Data Frame<\/h2>\n<h3 style=\"font-weight: bold; color: #334d55;\">Description<\/h3>\n<p style=\"color: #333333;\">Extract or replace subsets of data frames.<\/p>\n<h3 style=\"font-weight: bold; color: #334d55;\">Usage<\/h3>\n<pre style=\"color: #000000;\">x[i]\nx[i] &lt;- value\nx[i, j, drop = TRUE]\nx[i, j] &lt;- value\n\nx[[i]]\nx[[i]] &lt;- value\nx[[i, j]]\nx[[i, j]] &lt;- value\n\nx$name\nx$name &lt;- value\n<\/pre>\n<h3 style=\"font-weight: bold; color: #334d55;\">Arguments<\/h3>\n<table summary=\"R argblock\">\n<tbody>\n<tr valign=\"top\">\n<td><code>x<\/code><\/td>\n<td>data frame.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>i, j<\/code><\/td>\n<td>elements to extract or replace.\u00a0<code>i, j<\/code>\u00a0are\u00a0<code>numeric<\/code>\u00a0or\u00a0<code>character<\/code>\u00a0or, for\u00a0<code>[<\/code>\u00a0only, empty. Numeric values are coerced to integer as if by\u00a0<code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/integer.html\">as.integer<\/a><\/code>. For replacement by\u00a0<code>[<\/code>, a logical matrix is allowed.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>drop<\/code><\/td>\n<td>logical. If\u00a0<code>TRUE<\/code>\u00a0the result is coerced to the lowest possible dimension: however, see the Warning below.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>value<\/code><\/td>\n<td>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\u00a0<code>NULL<\/code>, deletes the column if a single column is selected.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>name<\/code><\/td>\n<td>name or literal character string.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 style=\"font-weight: bold; color: #334d55;\">Details<\/h3>\n<p style=\"color: #333333;\">Data frames can be indexed in several modes. When\u00a0<code>[<\/code>\u00a0and\u00a0<code>[[<\/code>\u00a0are used with a single index, they index the data frame as if it were a list. In this usage a\u00a0<code>drop<\/code>\u00a0argument is ignored, with a warning. Using\u00a0<code>$<\/code>\u00a0is equivalent to using\u00a0<code>[[<\/code>\u00a0with a single index.<\/p>\n<p style=\"color: #333333;\">When\u00a0<code>[<\/code>\u00a0and\u00a0<code>[[<\/code>\u00a0are used with two indices they act like indexing a matrix:\u00a0<code>[[<\/code>\u00a0can only be used to select one element.<\/p>\n<p style=\"color: #333333;\">If\u00a0<code>[<\/code>\u00a0returns a data frame it will have unique (and non-missing) row names, if necessary transforming the row names using\u00a0<code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/make.unique.html\">make.unique<\/a><\/code>. Similarly, column names will be transformed (if columns are selected more than once).<\/p>\n<p style=\"color: #333333;\">When\u00a0<code>drop =TRUE<\/code>, this is applied to the subsetting of any matrices contained in the data frame as well as to the data frame itself.<\/p>\n<p style=\"color: #333333;\">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.<\/p>\n<p style=\"color: #333333;\">For\u00a0<code>[<\/code>\u00a0the 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 the 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\u00a0<code>NULL<\/code>elements which will cause the corresponding columns to be deleted.<\/p>\n<p style=\"color: #333333;\">Matrixing indexing using\u00a0<code>[<\/code>\u00a0is not recommended, and barely supported. For extraction,\u00a0<code>x<\/code>\u00a0is first coerced to a matrix. For replacement a logical matrix (only) can be used to select the elements to be replaced in the same ways as for a matrix (except that missing values are not allowed at all).<\/p>\n<h3 style=\"font-weight: bold; color: #334d55;\">Value<\/h3>\n<p style=\"color: #333333;\">For\u00a0<code>[<\/code>\u00a0a 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.<br \/>\nFor\u00a0<code>[[<\/code>\u00a0a column of the data frame (extraction with one index) or a length-one vector (extraction with two indices).<br \/>\nFor\u00a0<code>[&lt;-<\/code>,\u00a0<code>[[&lt;-<\/code>\u00a0and\u00a0<code>$&lt;-<\/code>, a data frame.<\/p>\n<h3 style=\"font-weight: bold; color: #334d55;\">Coercion<\/h3>\n<p style=\"color: #333333;\">The story over when replacement values are coerced is a complicated one, and one that has changed during\u00a0<span style=\"color: #666666; font-family: 'Courier New', Courier;\"><b>R<\/b><\/span>&#8216;s development. This section is a guide only.<\/p>\n<p style=\"color: #333333;\">When\u00a0<code>[<\/code>\u00a0and\u00a0<code>[[<\/code>\u00a0are used to add or replace a whole column, no coercion takes place but\u00a0<code>value<\/code>\u00a0will be replicated (by calling the generic function\u00a0<code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/rep.html\">rep<\/a><\/code>) to the right length if an exact number of repeats can be used.<\/p>\n<p style=\"color: #333333;\">When\u00a0<code>[<\/code>\u00a0is used with a logical matrix, each value is coerced to the type of the column in which it is to be placed.<\/p>\n<p style=\"color: #333333;\">When\u00a0<code>[<\/code>\u00a0and\u00a0<code>[[<\/code>\u00a0are used with two indices, the column will be coerced as necessary to accommodate the value.<\/p>\n<p style=\"color: #333333;\">Note that when the replacement value is an array (including a matrix) it is\u00a0<em>not<\/em>\u00a0treated as a series of columns (as\u00a0<code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/data.frame.html\">data.frame<\/a><\/code>\u00a0and\u00a0<code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/as.data.frame.html\">as.data.frame<\/a><\/code>\u00a0do) but inserted as a single column.<\/p>\n<h3 style=\"font-weight: bold; color: #334d55;\">Warning<\/h3>\n<p style=\"color: #333333;\">Although the default for\u00a0<code>drop<\/code>\u00a0is\u00a0<code>TRUE<\/code>, the default behaviour when only one\u00a0<em>row<\/em>\u00a0is left is equivalent to specifying\u00a0<code>drop = FALSE<\/code>. To drop from a data frame to a list,\u00a0<code>drop = TRUE<\/code>\u00a0has to be specified explicitly.<\/p>\n<h3 style=\"font-weight: bold; color: #334d55;\">See Also<\/h3>\n<p style=\"color: #333333;\"><code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/subset.html\">subset<\/a><\/code>\u00a0which is often easier for extraction,\u00a0<code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/data.frame.html\">data.frame<\/a><\/code>,\u00a0<code><a style=\"font-weight: bold; color: #666666;\" href=\"http:\/\/astrostatistics.psu.edu\/datasets\/R\/html\/base\/html\/Extract.html\">Extract<\/a><\/code>.<\/p>\n<h3 style=\"font-weight: bold; color: #334d55;\">Examples<\/h3>\n<pre style=\"color: #000000;\">sw &lt;- swiss[1:5, 1:4]  # select a manageable subset\n\nsw[1:3]      # select columns\nsw[, 1:3]    # same\nsw[4:5, 1:3] # select rows and columns\nsw[1]        # a one-column data frame\nsw[, 1, drop = FALSE]  # the same\nsw[, 1]      # a (unnamed) vector\nsw[[1]]      # the same\n\nsw[1,]       # a one-row data frame\nsw[1,, drop=TRUE]  # a list\n\nswiss[ c(1, 1:2), ]   # duplicate row, unique row names are created\n\nsw[sw &lt;= 6] &lt;- 6  # logical matrix indexing\nsw\n\n## adding a column\nsw[\"new1\"] &lt;- LETTERS[1:5]   # adds a character column\nsw[[\"new2\"]] &lt;- letters[1:5] # ditto\nsw[, \"new3\"] &lt;- LETTERS[1:5] # ditto\n                             # but this got converted to a factor in 1.7.x\nsw$new4 &lt;- 1:5\nsapply(sw, class)\nsw$new4 &lt;- NULL              # delete the column\nsw\nsw[6:8] &lt;- list(letters[10:14], NULL, aa=1:5) # delete col7, update 6, append\nsw\n\n## matrices in a data frame\nA &lt;- data.frame(x=1:3, y=I(matrix(4:6)), z=I(matrix(letters[1:9],3,3)))\nA[1:3, \"y\"] # a matrix, was a vector prior to 1.8.0\nA[1:3, \"z\"] # a matrix\nA[, \"y\"]    # a matrix\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extract.data.frame {base} R Documentation Extract or Replace Parts of a Data Frame Description Extract or replace subsets of data frames. Usage x[i] x[i] &lt;- value&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-640","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=640"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/640\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}