Description Usage Arguments Value Examples
View source: R/padis_general_functions.R
Merge One Data Frame Into Several Data Frames
1 2 | merge_multiple_df(data_from, id_var, data_list_to, merge_down = TRUE,
select_variables = NULL)
|
data_from |
The data frame from which to merge |
id_var |
The id or group-variable to merge by |
data_list_to |
A list of data frames into which the data frame |
merge_down |
Logical, should the |
select_vars |
A character vector of the variables that should be selected from the data frame |
A list containing data frames with the same length as data_list_to
.
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 | ## simulate data
# long data
data_long_1 <- data.frame(PAR.ID = letters[1:10],
var1 = sample(1:10, 10),
var2 = sample(1:10, 10))
data_long_2 <- data.frame(PAR.ID = sort(rep(letters[1:10], 3)),
var1 = sort(rep(1:10, 3)),
var2 = sort(rep(11:20, 3)))
data_long_2_er <- data.frame(PAR.ID = sort(rep(letters[1:10], 3)),
var1 = sort(rep(1:10, 3)),
var2 = sort(rep(11:20, 3)))
data_long_2_er[3, "var2"] <- 12 ## add a variable that is not the same as the other variables
data_long_3 <- data.frame(PAR.ID = rep(letters[1:10], 4),
var1 = sample(1:40, 40),
var2 = sample(1:40, 40))
# short data
data_short_2 <- data.frame(PAR.ID = rep(letters[1:10], 1),
var1 = sample(1:40, 10),
var2 = sample(1:40, 10))
data_short_3 <- data.frame(PAR.ID = rep(letters[1:10], 1),
var1 = sample(1:40, 10),
var2 = sample(1:40, 10))
## merge down, i. e. duplicate rows when going from a higher data set (e.g .level-2) to a lower data set (e.g. level-1)
merge_multiple_df(data_long_1, id_var="PAR.ID", list(data_long_2, data_long_3), merge_down = TRUE)
## merge up, i. e. make the longer data frame short first and merge then
merge_multiple_df(data_from = data_long_2, id_var="PAR.ID", data_list_to = list(data_short_2, data_short_3), merge_down = FALSE)
## merge up, i. e. make the longer data frame short first and merge then and give a warning if duplicates arise
merge_multiple_df(data_from = data_long_2_er, id_var="PAR.ID", data_list_to = list(data_short_2, data_short_3), merge_down = FALSE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.