utils_rows_cols: Utilities for handling with rows and columns

Description Usage Arguments Author(s) Examples

Description

[Stable]

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

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 add_prefix() and add_suffix(), ... is the column name to add the prefix or suffix, respectively. Select helpers are allowed.

  • 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.

  • For get_levels(), get_level_comb(), and get_level_size() ... is the unquoted variable names to get the levels, levels combinations and levels size, respectively.

.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.

var

Name of column to use for rownames.

levels

The levels of a factor or a numeric vector.

prefix, suffix

The prefix and suffix used in add_prefix() and add_suffix(), respectively.

sep

The separator to appear when using concatenate(), add_prefix(), or add_suffix(). Defaults to to "_".

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.

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.

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())

Author(s)

Tiago Olivoto tiagoolivoto@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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, where(is.factor), .after = last_col())

######## 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_")

# Use prefixes and suffixes
concatenate(data_ge2, REP, prefix = "REP", new_var = REP)

# Use prefixes and suffixes (the ear traits EH, EP, EL, and ED)
add_prefix(data_ge2, PH, EH, EP, EL, prefix = "EAR")
add_suffix(data_ge2, PH, EH, EP, EL, suffix = "EAR", sep = ".")

# Use prefixes and suffixes (colnames)
concatenate(data_ge2, REP, prefix = "REP", new_var = REP)


########### formating column names ###############
# Creating data with messy column names
df <- head(data_ge, 3)
colnames(df) <- c("Env", "gen", "Rep", "GY", "hm")
df
colnames_to_lower(df)
colnames_to_upper(df)
colnames_to_title(df)


################### Adding rows ##################
data_ge %>%
  add_rows(GY = 10.3,
           HM = 100.11,
           .after = 1)

########## checking if a column exists ###########
column_exists(data_g, "GEN")

####### get the levels, level combinations and size of levels ########
get_levels(data_g, GEN)
get_levels_comb(data_ge, ENV, 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)

metan documentation built on Nov. 10, 2021, 9:11 a.m.