Functions Working on Data Frames
Hauke Sonnenberg
2024-03-28
Source:vignettes/functions_data_frame.Rmd
functions_data_frame.Rmd
Function addRowWithName()
This function does no more than adding a row to a data frame with
rbind
and giving it a name:
x <- data.frame(value = 10:11)
new_row <- data.frame(value = sum(x$value))
kwb.utils::addRowWithName(x, new_row, row.name = "total")
#> value
#> 1 10
#> 2 11
#> total 21
Function addSuffixToColumns()
This function adds a postfix to the column names of a data frame. This may be useful when column-binding data frames. The function can be used to indicate the origin of each column by giving it a suffix related to the data frame that it originates from.
# Define a first data frame
data_frame_1 <- data.frame(
id = 1:2,
first = c("Peter", "Mary"),
last = c("Miller", "Smith")
)
# Define a second data frame
data_frame_2 <- data.frame(
height_cm = c(181, 171),
weigth_kg = c(68, 59)
)
# Column-bind the data frames, after giving their columns a unique suffix
cbind(
kwb.utils::addSuffixToColumns(data_frame_1, "_1"),
kwb.utils::addSuffixToColumns(data_frame_2, "_2")
)
#> id_1 first_1 last_1 height_cm_2 weigth_kg_2
#> 1 1 Peter Miller 181 68
#> 2 2 Mary Smith 171 59
Function asNoFactorDataFrame
This function is a shortcut to
as.data.frame(..., stringsAsFactors = FALSE)
. Using this
function may slightly improve the readability of a script (as the number
of arguments passed to the function is reduced):
m <- matrix(letters[1:6], nrow = 2)
result_1 <- kwb.utils::asNoFactorDataFrame(m)
result_2 <- as.data.frame(m, stringsAsFactors = FALSE)
identical(result_1, result_2)
#> [1] TRUE
Functions Working on Data Frames