I have 2 data frames I am trying to use modifylist
to. However, modifylist
only takes a named list. In my data I have the "original - list_A" which is named and the "new - list_B" which is what I want to merge to list_A
.
If
list_A
contains as"Error" then
list_Bwill contain the correct data, however, the lists are different sizes. (
List_B <= list_A`). I originally wanted to use something like:modifyList(list_A, list_B)
Which does not modify the list since list_B
does not have list names.
TO illustrate this;
list_C = list_B
names(list_C) = names(list_A)[1:6]
modifyList(list_A, list_C)
But this did not work.
What I want to do is to merge the 2 lists together, to give me a list of size 7 (just as in list_A
but this time the elm4
and elm6
filled in to containt he data frames found in list_B
.
The final list should contain only data frames.
Data:
# Named list
list_A = list(
elm1 = data.frame(a = c("A1", "A2", "A3"), b = c("B1", "B2", "B3"), c = c("C1", "C2", "C3")),
elm2 = data.frame(a = c("A11", "A22", "A33"), b = c("B11", "B22", "B33"), c = c("C11", "C22", "C33")),
elm3 = data.frame(a = c("A111", "A222", "A333"), b = c("B111", "B222", "B333"), c = c("C111", "C222", "C333")),
elm4 = as.character("Error"),
elm5 = data.frame(a = c("A11111", "A22222", "A33333"), b = c("B11111", "B22222", "B33333"), c = c("C11111", "C22222", "C33333")),
elm6 = as.character("Error"),
elm7 = data.frame(a = c("A1111111", "A2222222", "A3333333"), b = c("B1111111", "B2222222", "B3333333"), c = c("C1111111", "C2222222", "C3333333"))
)
# Unnamed list of different length
list_B = list(
NULL,
NULL,
NULL,
data.frame(a = c("A1111", "A2222", "A3333"), b = c("B1111", "B2222", "B3333"), c = c("C1111", "C2222", "C3333")), # elm4
NULL,
data.frame(a = c("A111111", "A222222", "A333333"), b = c("B111111", "B222222", "B333333"), c = c("C111111", "C222222", "C333333")) # elm6
# elm7 is missing since the list ends at the last data.frame in this list
)
Expected output:
$elm1
a b c
1 A1 B1 C1
2 A2 B2 C2
3 A3 B3 C3
$elm2
a b c
1 A11 B11 C11
2 A22 B22 C22
3 A33 B33 C33
$elm3
a b c
1 A111 B111 C111
2 A222 B222 C222
3 A333 B333 C333
$elm4
a b c
1 A1111 B1111 C1111
2 A2222 B2222 C2222
3 A3333 B3333 C3333
$elm5
a b c
1 A11111 B11111 C11111
2 A22222 B22222 C22222
3 A33333 B33333 C33333
$elm6
a b c
1 A111111 B111111 C111111
2 A222222 B222222 C222222
3 A333333 B333333 C333333
$elm7
a b c
1 A1111111 B1111111 C1111111
2 A2222222 B2222222 C2222222
3 A3333333 B3333333 C3333333
or
list(
elm1 = data.frame(a = c("A1", "A2", "A3"), b = c("B1", "B2", "B3"), c = c("C1", "C2", "C3")),
elm2 = data.frame(a = c("A11", "A22", "A33"), b = c("B11", "B22", "B33"), c = c("C11", "C22", "C33")),
elm3 = data.frame(a = c("A111", "A222", "A333"), b = c("B111", "B222", "B333"), c = c("C111", "C222", "C333")),
elm4 = data.frame(a = c("A1111", "A2222", "A3333"), b = c("B1111", "B2222", "B3333"), c = c("C1111", "C2222", "C3333")), # elm4
#elm4 = as.character("Error"),
elm5 = data.frame(a = c("A11111", "A22222", "A33333"), b = c("B11111", "B22222", "B33333"), c = c("C11111", "C22222", "C33333")),
elm6 = data.frame(a = c("A111111", "A222222", "A333333"), b = c("B111111", "B222222", "B333333"), c = c("C111111", "C222222", "C333333")), # elm6
#elm6 = as.character("Error"),
elm7 = data.frame(a = c("A1111111", "A2222222", "A3333333"), b = c("B1111111", "B2222222", "B3333333"), c = c("C1111111", "C2222222", "C3333333"))
)
CodePudding user response:
Perhaps just find the elements where list_A
is "Error", and assign those to the elments from list_B
?
m <- which(list_A=="Error")
list_A[m] <- list_B[m]