| df.subset | R Documentation |
This function returns subsets of data frames which meet conditions.
df.subset(data, ..., subset = NULL, drop = TRUE, check = TRUE)
data |
a data frame. |
... |
an expression indicating variables to select from the data frame
specified in |
subset |
a logical expression indicating rows to keep, e.g., |
drop |
logical: if |
check |
logical: if |
The argument ... is used to specify an expression indicating the
variables to select and/or remove from the data frame specified in data.
There are six operators which can be used in the expression ...:
+) OperatorThe plus operator is used to select
variables matching a prefix from the data frame specified in data. For
example, df.subset(dat, +x) selects all variables with the
prefix x. Note that this operator is equivalent to the function
starts_with() from the tidyselect package.
-) OperatorThe minus operator is used to select
variables matching a suffix from the data frame specified in data. For
example, df.subset(dat, -y) selects all variables with the
suffix y. Note that this operator is equivalent to the function
ends_with() from the tidyselect package.
~) OperatorThe tilde operator is used to select
variables containing a word from the data frame specified in data. For
example, df.subset(dat, ~al) selects all variables with the word
al. Note that this operator is equivalent to the function
contains() from the tidyselect package.
:) operatorThe colon operator is used to select
a range of consecutive variables from the data frame specified in data.
For example, df.subset(dat, x:z) selects all variables from
x to z. Note that this operator is equivalent to the :
operator from the select function in the dplyr package.
::) OperatorThe double colon operator
is used to select numbered variables from the data frame specified in
data. For example, df.subset(dat, x1::x3) selects the
variables x1, x2, and x3. Note that this operator is
similar to the function num_range() from the tidyselect
package.
!) OperatorThe exclamation point
operator is used to drop variables from the data frame specified in the argument
data or for taking the complement of a set of variables. For example,
df.subset(dat, !x) selects all variables except the variable x,
df.subset(dat, !~x) selects all variables except variables with the
prefix x, or df.subset(dat, x1:x10, !x3:x5) selects all variables
from x1 to x10 but excludes all variables from x3 to
x5. Note that this operator is equivalent to the ! operator from
the select function in the dplyr package.
Operators can be combined within the same function call. For example,
df.subset(dat, +x, -y, !x2:x4, z) selects all variables with the prefix
x and with the suffix y but excludes variables from x2 to
x4 and select variable z.
Returns a data frame containing the variables and rows selected in the argument
... and rows selected in the argument subset.
Takuya Yanagida takuya.yanagida@univie.ac.at
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
df.check, df.duplicated, df.unique,
df.head, df.tail, df.long,
df.wide, df.merge, df.move,
df.rbind, df.rename, df.sort,
## Not run:
#----------------------------------------------------------------------------
# Select single variables
# Example 1: Select 'Sepal.Length' and 'Petal.Width'
df.subset(iris, Sepal.Length, Petal.Width)
#----------------------------------------------------------------------------
# Select rows
# Example 2a: Select all variables, select rows with 'Species' equal 'setosa'
df.subset(iris, subset = Species == "setosa")
# Example 2b: Select all variables, select rows with 'Petal.Length' smaller 1.2
df.subset(iris, subset = Petal.Length < 1.2)
#----------------------------------------------------------------------------
# Select variables matching a prefix using the + operator
# Example 3: Select variables with prefix 'Petal'
df.subset(iris, +Petal)
#----------------------------------------------------------------------------
# Select variables matching a suffix using the - operator
# Example 4: Select variables with suffix 'Width'
df.subset(iris, -Width)
#----------------------------------------------------------------------------
# Select variables containing a word using the ~ operator
#
# Example 5: Select variables containing 'al'
df.subset(iris, ~al)
#----------------------------------------------------------------------------
# Select consecutive variables using the : operator
# Example 6: Select all variables from 'Sepal.Width' to 'Petal.Width'
df.subset(iris, Sepal.Width:Petal.Width)
#----------------------------------------------------------------------------
# Select numbered variables using the :: operator
# Example 7: Select all variables from 'x1' to 'x3' and 'y1' to 'y3'
df.subset(anscombe, x1::x3, y1::y3)
#
#----------------------------------------------------------------------------
# Drop variables using the ! operator
# Example 8a: Select all variables except 'Sepal.Width'
df.subset(iris, !Sepal.Width)
# Example 8b: Select all variables except variables with prefix 'Petal'
df.subset(iris, !+Petal)
# Example 8c: Select all variables except variables with suffix 'Width'
df.subset(iris, !-Width)
# Example 8d: Select all variables except 'Sepal.Width' to 'Petal.Width'
df.subset(iris, !Sepal.Width:Petal.Width)
#----------------------------------------------------------------------------
# Combine +, -, !, and : operators
# Example 9: Select variables with prefix 'x' and suffix '3', but exclude
# variables from 'x2' to 'x3'
df.subset(anscombe, +x, -3, !x2:x3)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.