metan (version 1.2.1)

utils_rows_cols: Utilities for handling with rows and columns

Description

  • add_cols(): Add one or more columns to an existing data frame. If specified .before or .after columns does not exist, columns are appended at the end of the data. Return a data frame with all the original columns in .data plus the columns declared in .... In add_cols() columns in .data are available for the expressions. So, it is possible to add a column based on existing data.

  • add_rows(): Add one or more rows to an existing data frame. If specified .before or .after rows does not exist, rows are appended at the end of the data. Return a data frame with all the original rows in .data plus the rows declared in ....

  • all_pairs(): Get all the possible pairs between the levels of a factor.

  • column_exists(): Checks if a column exists in a data frame. Return a logical value.

  • columns_to_first(): Move columns to first positions in .data.

  • columns_to_last(): Move columns to last positions in .data.

  • concatenate(): Concatenate columns of a data frame. If drop = TRUE then the existing variables are dropped.

  • get_levels(): Get the levels of a factor variable.

  • get_level_size(): Get the size of each level of a factor variable.

  • remove_cols(): Remove one or more columns from a data frame.

  • remove_rows(): Remove one or more rows from a data frame.

  • reorder_cols(): Reorder columns in a data frame.

  • select_cols(): Select one or more columns from a data frame.

  • select_first_col(): Select first variable, possibly with an offset.

  • select_last_col(): Select last variable, possibly with an offset.

  • select_numeric_cols(): Select all the numeric columns of a data frame.

  • select_non_numeric_cols(): Select all the non-numeric columns of a data frame.

  • select_rows(): Select one or more rows from a data frame.

Usage

add_cols(.data, ..., .before = NULL, .after = NULL)

add_rows(.data, ..., .before = NULL, .after = NULL)

all_pairs(.data, levels)

column_to_first(.data, ...)

column_to_last(.data, ...)

column_exists(.data, cols)

concatenate( .data, ..., new_var = new_var, sep = "_", drop = FALSE, pull = FALSE, .before = NULL, .after = NULL )

get_levels(.data, group)

get_level_size(.data, group)

reorder_cols(.data, ..., .before = NULL, .after = NULL)

remove_cols(.data, ...)

remove_rows(.data, ...)

select_first_col(.data, offset = NULL)

select_last_col(.data, offset = NULL)

select_numeric_cols(.data)

select_non_numeric_cols(.data)

select_cols(.data, ...)

select_rows(.data, ...)

Arguments

.data

A data frame

...

The argument depends on the function used.

  • For add_cols() and add_rows() is name-value pairs. All values must have one element for each row in .data when using add_cols() or one element for each column in .data when using add_rows(). Values of length 1 will be recycled when using add_cols().

  • For remove_cols() and select_cols(), ... is the column name or column index of the variable(s) to be dropped.

  • For columns_to_first() and columns_to_last(), ... is the column name or column index of the variable(s) to be moved to first or last in .data.

  • For remove_rows() and select_rows(), ... is an integer row value.

  • For concatenate(), ... is the unquoted variable names to be concatenated.

.before, .after

For add_cols(), concatenate(), and reorder_cols(), one-based column index or column name where to add the new columns, default: .after last column. For add_rows(), one-based row index where to add the new rows, default: .after last row.

levels

The levels of a factor or a numeric vector.

cols

A quoted variable name to check if it exists in .data.

new_var

The name of the new variable containing the concatenated values. Defaults to new_var.

sep

The separator to appear between concatenated variables. Defaults to "_".

drop

Logical argument. If TRUE keeps the new variable new_var and drops the existing ones. Defaults to FALSE.

pull

Logical argument. If TRUE, returns the last column (on the assumption that's the column you've created most recently), as a vector.

group

A factor variable to get the levels.

offset

Set it to n to select the nth variable from the end (for select_last_col()) of from the begin (for select_first_col())

Examples

Run this code
# NOT RUN {
library(metan)

################# Adding columns #################
# Variables x and y .after last column
data_ge %>%
  add_cols(x = 10,
           y = 30)
# Variables x and y .before the variable GEN
data_ge %>%
  add_cols(x = 10,
           y = 30,
           .before = "GEN")

# Creating a new variable based on the existing ones.
data_ge %>%
  add_cols(GY2 = GY^2,
           GY2_HM = GY2 + HM,
           .after = "GY")

############### Reordering columns ###############
reorder_cols(data_ge2, NKR, .before = "ENV")
reorder_cols(data_ge2, ENV, GEN, .after = "ED")

######## Selecting and removing columns ##########
select_cols(data_ge2, GEN, REP)
remove_cols(data_ge2, GEN, REP)

########## Selecting and removing rows ###########
select_rows(data_ge2, 2:3)
remove_rows(data_ge2, 2:3)

########### Concatenating columns ################
concatenate(data_ge, ENV, GEN, REP)
concatenate(data_ge, ENV, GEN, REP, drop = TRUE)

# Combine with add_cols() and replace_string()
data_ge2 %>%
 add_cols(ENV_GEN = concatenate(., ENV, GEN, pull = TRUE),
          .after = "GEN") %>%
 replace_string(ENV_GEN,
                pattern = "H",
                replacement = "HYB_",
                .after = "ENV_GEN")


################### Adding rows ##################
data_ge %>%
  add_rows(ENV = "E_TEST",
           GEN = "G_TEST",
           REP = 3,
           GY = 10.3,
           HM = 100.11,
           .after = 1)
########## checking if a column exists ###########
column_exists(data_g, "GEN")

####### get the levels and size of levels ########
get_levels(data_g, GEN)
get_level_size(data_g, GEN)

############## all possible pairs ################
all_pairs(data_g, GEN)

########## select numeric variables only #########
select_numeric_cols(data_g)
select_non_numeric_cols(data_g)
# }

Run the code above in your browser using DataCamp Workspace