I created two list objects in R (input1 and input2). However, their internal structure is different.
Using dput() for the first object in both lists produces the following outputs:
dput(input1[[1]][1:3,]) results in:
structure(c(284.190191328526, 275.720694586635, 277.151402309537,
251.027558222413, 260.092776268721, 261.799371317029, 259.013641774654,
268.970000296831, 270.491042807698, 281.670583188534, 277.505417019129,
278.831141069531, 265.090487375855, 259.860835596919, 259.975585192442,
283.792229965329, 278.662678897381, 279.944456294179, 282.146671935916,
279.153416529298, 280.239875465631, 270.852381959558, 270.976897478104,
271.836298704147, 243.119602054358, 248.854640349746, 250.075380727649,
283.831293657422, 277.869197651744, 279.148533567786, 0, 0, 0
), dim = c(3L, 11L), dimnames = list(NULL, c("", "", "", "",
"", "", "", "", "", "", "gt")))
and dput(input2[[1]][1:3,]):
structure(list(V1 = c(0.811678855928824, -0.393041852260828,
-0.189534693137453), V2 = c(-0.497530141536052, 0.619553997264613,
0.829853435191207), V3 = c(-0.568109172725924, 0.638689412940523,
0.823053198833147), V4 = c(0.518601021781598, 0.0287837764943511,
0.184686920803166), V5 = c(0.918844876165946, 0.0304596134725843,
0.0499526607866051), V6 = c(0.554767280188424, 0.0108778468779691,
0.146785487281578), V7 = c(0.401937763318916, 0.0974408960297253,
0.207963820942947), V8 = c(0.220547766452583, 0.235472184321063,
0.338479931178033), V9 = c(-0.280738982094278, 0.521088406634542,
0.691762606023399), V10 = c(0.648670753892295, -0.0320875459881486,
0.113988354559463), gt = c(0, 0, 0)), row.names = c(NA, 3L), class = "data.frame")
My question: How can I change the internal structure of input2? Both lists should have the same internal structure. Thanks in advance!
>Solution :
Except for the row names, this gets you pretty close (both are matrices with the same dimensions and the same column names):
m2B <- m2 |> setNames(colnames(m1)) |> as.matrix()
(assuming that m1 is the element from the first list and m2 from the second list: you may need to use a for loop/lapply()/purrr::map/whatever to do this for every element)
If you’re willing to take two lines:
m2B <- as.matrix(m2)
dimnames(m2B) <- dimnames(m1)