View source: R/dataframe_tools.R
drop_invar_cols | R Documentation |
Deletes columns from a dataframe if they do not vary. For character
and factor
columns, this means that every row of the column contains exactly the same string.
For numeric
columns, the numbers are rounded to a nearest common value and then
checked to see if every rounded number is the same.
drop_invar_cols(
df,
from = 1,
to = NULL,
cols = NULL,
nearest = NULL,
dir = NULL
)
df |
(Dataframe) A dataframe. |
from, to |
(Numeric or |
cols |
(Numeric or |
nearest |
(Numeric or |
dir |
(Character or |
A copy of df
with all invariant columns removed.
Desi Quintans (http://www.desiquintans.com)
df <- data.frame(stringsAsFactors=FALSE,
char_invar = c("A", "A", "A", "A", "A"),
char_var = c("A", "A", "A", "B", "A"),
num_invar = c(1L, 1L, 1L, 1L, 1L),
num_mean_0 = c(0, -0.1, 0.1, 0.01, -0.01),
num_var = c(0, 0.2, 0.8, 0.03, 0.4)
)
df
#> char_invar char_var num_invar num_mean_0 num_var
#> 1 A A 1 0.00 0.00
#> 2 A A 1 -0.10 0.20
#> 3 A A 1 0.10 0.80
#> 4 A B 1 0.01 0.03
#> 5 A A 1 -0.01 0.40
drop_invar_cols(df)
#> char_var num_var
#> 1 A 0.00
#> 2 A 0.20
#> 3 A 0.80
#> 4 B 0.03
#> 5 A 0.40
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.