Split a data frame into a list of blocks (either by rows or by columns)
df_to_blocks(DataFrame, blocks, byrow = TRUE)
df_to_blocks(DataFrame, blocks, byrow = TRUE)
DataFrame |
a data frame to split |
blocks |
either a list or a vector indicating the
blocks. If |
byrow |
logical. If |
A list of data frames
Gaston Sanchez
# say you have a data frame iris_df = iris[c(1:3,51:53,101:103),] # list defining the blocks row_blocks = list(1:3, 4:6, 7:9) col_blocks = c(2, 2, 1) # split data into list of blocks (by rows) df_to_blocks(iris_df, row_blocks) # split data into list of blocks (by columns) df_to_blocks(iris_df, col_blocks, byrow=FALSE)
# say you have a data frame iris_df = iris[c(1:3,51:53,101:103),] # list defining the blocks row_blocks = list(1:3, 4:6, 7:9) col_blocks = c(2, 2, 1) # split data into list of blocks (by rows) df_to_blocks(iris_df, row_blocks) # split data into list of blocks (by columns) df_to_blocks(iris_df, col_blocks, byrow=FALSE)
Create an indexed list from the columns of a dummy (or semi-dummy) matrix
dummy_to_list(Dummy)
dummy_to_list(Dummy)
Dummy |
matrix (dummy by columns) |
A list of indexed vectors
Gaston Sanchez
# let's say you have a list like this some_list = list(1:3, 1:2, 1:4) # first create a dummy matrix based on some_list some_dummy = list_to_dummy(some_list) # now apply 'dummy_to_list' dummy_to_list(some_dummy) # a semi-dummy matrix semi_dummy = some_dummy semi_dummy[semi_dummy != 0] = rnorm(listsize(some_list)) dummy_to_list(semi_dummy)
# let's say you have a list like this some_list = list(1:3, 1:2, 1:4) # first create a dummy matrix based on some_list some_dummy = list_to_dummy(some_list) # now apply 'dummy_to_list' dummy_to_list(some_dummy) # a semi-dummy matrix semi_dummy = some_dummy semi_dummy[semi_dummy != 0] = rnorm(listsize(some_list)) dummy_to_list(semi_dummy)
Create a dummy matrix based on the elements of a factor. Each column in the produced matrix is a dummy indicator.
factor_to_dummy(afactor)
factor_to_dummy(afactor)
afactor |
a factor (preferably of vectors) |
A matrix of dummy variables
Gaston Sanchez
vector_to_dummy
,
list_to_dummy
# let's say you have a list like this some_factor = iris$Species[c(1:3,51:53,101:103)] # get dummy matrix factor_to_dummy(some_factor)
# let's say you have a list like this some_factor = iris$Species[c(1:3,51:53,101:103)] # get dummy matrix factor_to_dummy(some_factor)
Get the starting position 'from' and the ending position 'to' of the elements contained in a vector (or a list of vectors)
from_to(x, ...)
from_to(x, ...)
x |
a numeric vector or a list of vectors |
... |
further arguments are ignored |
A list with two vectors: '$from' and '$to'. '$from' contains the indices with starting positions. '$to' contains the indices with ending positions.
Gaston Sanchez
# let's say you have a numeric vector like this num_vec = c(2, 3, 1, 4) # get 'from' and 'to' indices start_end = from_to(num_vec) from = start_end$from to = start_end$to #' let's say you have a list like this str_list = list(c("a","b","c"), c("d", "e"), c("f","g","h")) # get 'from' and 'to' indices start_end = from_to(str_list) from = start_end$from to = start_end$to
# let's say you have a numeric vector like this num_vec = c(2, 3, 1, 4) # get 'from' and 'to' indices start_end = from_to(num_vec) from = start_end$from to = start_end$to #' let's say you have a list like this str_list = list(c("a","b","c"), c("d", "e"), c("f","g","h")) # get 'from' and 'to' indices start_end = from_to(str_list) from = start_end$from to = start_end$to
Applies a function to the unlisted elements of a list
funlist(alist, f, ...)
funlist(alist, f, ...)
alist |
a list |
f |
a function to be applied |
... |
further arguments passed on to |
value
Gaston Sanchez
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the sum of all elements in list1 funlist(list1, sum) # get the maximum element in list1 funlist(list1, max) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the sum removing NAs funlist(list2, sum, na.rm=TRUE)
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the sum of all elements in list1 funlist(list1, sum) # get the maximum element in list1 funlist(list1, max) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the sum removing NAs funlist(list2, sum, na.rm=TRUE)
Create indexed components for the elements of a list.
indexify(x, out)
indexify(x, out)
x |
a numeric vector or list of vectors |
out |
string indicating the output format
( |
A vector (or list) of indexed numbers
Gaston Sanchez
# let's say you have a numeric vector like this num_vec = c(2, 3, 1, 4) # get indices in vector format indexify(num_vec) # let's say you have a list like this str_list = list(c("a","b","c"), c("d", "e"), c("f","g","h")) # get indices in vector format indexify(str_list) # get indices in list format indexify(str_list, "list")
# let's say you have a numeric vector like this num_vec = c(2, 3, 1, 4) # get indices in vector format indexify(num_vec) # let's say you have a list like this str_list = list(c("a","b","c"), c("d", "e"), c("f","g","h")) # get indices in vector format indexify(str_list) # get indices in list format indexify(str_list, "list")
Get the length of the elements contained in a list.
lengths(alist, out = "vector")
lengths(alist, out = "vector")
alist |
a list |
out |
string indicating the format of the output
( |
A vector (or list) with the lengths of the elements in
alist
Gaston Sanchez
# say you have some list some_list = list(1:3, 4:5, 6:9) # length of each vector (output in vector format) lengths(some_list) # length of each vector (output in list format) lengths(some_list, out = 'list') # compare to 'length()' length(some_list)
# say you have some list some_list = list(1:3, 4:5, 6:9) # length of each vector (output in vector format) lengths(some_list) # length of each vector (output in list format) lengths(some_list, out = 'list') # compare to 'length()' length(some_list)
Create a list with vectors of ones from a numeric vector
list_ones(x)
list_ones(x)
x |
a numeric vector |
A list of vectors with ones
Gaston Sanchez
# let's say you have a numeric vector like this num_vec = c(2, 3, 1, 4) # get indices in vector format list_ones(num_vec)
# let's say you have a numeric vector like this num_vec = c(2, 3, 1, 4) # get indices in vector format list_ones(num_vec)
Create a dummy matrix based on the elements of a list. Each column in the produced matrix is a dummy indicator.
list_to_dummy(alist)
list_to_dummy(alist)
alist |
a list of vectors |
A matrix of dummy variables
Gaston Sanchez
# let's say you have a list like this num_list = list(1:3, 4:5, 6:9) # get dummy matrix list_to_dummy(num_list) # try with a list of strings str_list = list(c("a","b","c"), c("d", "e"), c("f","g","h")) list_to_dummy(str_list)
# let's say you have a list like this num_list = list(1:3, 4:5, 6:9) # get dummy matrix list_to_dummy(num_list) # try with a list of strings str_list = list(c("a","b","c"), c("d", "e"), c("f","g","h")) list_to_dummy(str_list)
Create a design-type matrix based on the elements of a list. Each column in the produced matrix is linked to the vectors in the list. See example.
list_to_matrix(alist)
list_to_matrix(alist)
alist |
a list of numeric vectors |
A design-type matrix
Gaston Sanchez
# let's say you have a list like this num_list = list(1:3, 4:5, 6:9) # get design-type matrix list_to_matrix(num_list)
# let's say you have a list like this num_list = list(1:3, 4:5, 6:9) # get design-type matrix list_to_matrix(num_list)
Given a vector of integers, create a list of indexed vectors.
listify(indices)
listify(indices)
indices |
a vector of integers indicating the length of each vector in the produced list |
A list of index vectors
Gaston Sanchez
# let's say you have a vector of indices list like this number_elements = c(3, 1, 5) # get list of index vectors based on 'number_elements' listify(number_elements)
# let's say you have a vector of indices list like this number_elements = c(3, 1, 5) # get list of index vectors based on 'number_elements' listify(number_elements)
Get the total number of elements in a list.
listsize(alist)
listsize(alist)
alist |
a list |
number of elements in alist
.
Gaston Sanchez
some_list = list(1:3, 4:5, 6:9) # number of elems in 'some_list' listsize(some_list)
some_list = list(1:3, 4:5, 6:9) # number of elems in 'some_list' listsize(some_list)
Split a matrix into a list of blocks (either by rows or by columns)
matrix_to_blocks(Matrix, blocks, byrow = TRUE)
matrix_to_blocks(Matrix, blocks, byrow = TRUE)
Matrix |
a matrix to split |
blocks |
either a list or a vector indicating the
blocks. If |
byrow |
logical. If |
A list of matrices
Gaston Sanchez
# matrix with 10 rows and 7 columns M = matrix(rnorm(70), 10, 7) # row blocks row_sets = list(1:3, 4:5, 6:10) # split matrix by rows matrix_to_blocks(M, row_sets) # column blocks col_sets = c(3, 4) # split matrix by rows matrix_to_blocks(M, col_sets, byrow=FALSE)
# matrix with 10 rows and 7 columns M = matrix(rnorm(70), 10, 7) # row blocks row_sets = list(1:3, 4:5, 6:10) # split matrix by rows matrix_to_blocks(M, row_sets) # column blocks col_sets = c(3, 4) # split matrix by rows matrix_to_blocks(M, col_sets, byrow=FALSE)
This is just a wrapper of funlist
using max
maxlist(alist, na.rm = FALSE)
maxlist(alist, na.rm = FALSE)
alist |
a list |
na.rm |
logical indicating whether missing values should be removed |
the maximum
Gaston Sanchez
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the max of all elements in list1 maxlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the max of all elements in list2 removing NAs maxlist(list2, na.rm=TRUE)
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the max of all elements in list1 maxlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the max of all elements in list2 removing NAs maxlist(list2, na.rm=TRUE)
This is just a wrapper of funlist
using
mean
meanlist(alist, na.rm = FALSE)
meanlist(alist, na.rm = FALSE)
alist |
a list |
na.rm |
logical indicating whether missing values should be removed |
the mean
Gaston Sanchez
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the mean of all elements in list1 meanlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the mean of all elements in list2 removing NAs meanlist(list2, na.rm=TRUE)
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the mean of all elements in list1 meanlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the mean of all elements in list2 removing NAs meanlist(list2, na.rm=TRUE)
This is just a wrapper of funlist
using min
minlist(alist, na.rm = FALSE)
minlist(alist, na.rm = FALSE)
alist |
a list |
na.rm |
logical indicating whether missing values should be removed |
the minimum
Gaston Sanchez
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the min of all elements in list1 minlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the min of all elements in list2 removing NAs minlist(list2, na.rm=TRUE)
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the min of all elements in list1 minlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the min of all elements in list2 removing NAs minlist(list2, na.rm=TRUE)
This is just a wrapper of funlist
using
prod
prodlist(alist, na.rm = FALSE)
prodlist(alist, na.rm = FALSE)
alist |
a list |
na.rm |
logical indicating whether missing values should be removed |
the product
Gaston Sanchez
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the product of all elements in list1 prodlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the prod of all elements in list2 removing NAs prodlist(list2, na.rm=TRUE)
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the product of all elements in list1 prodlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the prod of all elements in list2 removing NAs prodlist(list2, na.rm=TRUE)
This is just a wrapper of funlist
using sum
sumlist(alist, na.rm = FALSE)
sumlist(alist, na.rm = FALSE)
alist |
a list |
na.rm |
logical indicating whether missing values should be removed |
the sum
Gaston Sanchez
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the sum of all elements in list1 sumlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the sum of all elements in list2 removing NAs sumlist(list2, na.rm=TRUE)
# say you have some list list1 = list(1:5, runif(3), rnorm(4)) # get the sum of all elements in list1 sumlist(list1) # say you have missing data list2 = list(c(1:4, NA), runif(3), rnorm(4)) # get the sum of all elements in list2 removing NAs sumlist(list2, na.rm=TRUE)
Package designed for working with vectors and lists of vectors, mainly for turning them into other indexed data structures.
Create a dummy matrix based on the elements of a vector. Each column in the produced matrix is a dummy indicator.
vector_to_dummy(avector)
vector_to_dummy(avector)
avector |
a numeric vector |
A matrix of dummy variables
Gaston Sanchez
list_to_dummy
,
factor_to_dummy
# let's say you have a list like this num_vec = c(2, 3, 1, 4) # get dummy matrix vector_to_dummy(num_vec)
# let's say you have a list like this num_vec = c(2, 3, 1, 4) # get dummy matrix vector_to_dummy(num_vec)