Data:
k1 <-
structure(list(`3280.2000` = structure(c(0, 1.363, 0, 4.198,
1.097, 1.669, 0, 1.098, 0), .Dim = c(3L, 3L), .Dimnames = list(
c("3280.3500", "3280.400", "3280.4000"), c("3280.3500", "3280.400",
"3280.4000"))), `160.2000` = structure(c(0.935, 0.929, 1.1,
1.152), .Dim = c(2L, 2L), .Dimnames = list(
c("160.2200", "160.2300"), c("160.2200", "160.2300")))), .Names = c("3280.2000", "160.2000"))
#k1 Looks like
# $`3280.2000`
# 3280.3500 3280.400 3280.4000
# 3280.3500 0.000 4.198 0.000
# 3280.400 1.363 1.097 1.098
# 3280.4000 0.000 1.669 0.000
#
# $`160.2000`
# 160.2200 160.2300
# 160.2200 0.935 1.100
# 160.2300 0.929 1.152
k2 <-
structure(list(`3280.2000` = structure(c(0, 1.363, 0, 4.198,
1.097, 1.669, 0, 1.098, 0), .Dim = c(3L, 3L), .Dimnames = list(
c("400","3500", "4000"), c("400", "3500",
"4000"))), `160.2000` = structure(c(0.935, 0.929, 1.152,
0.903), .Dim = c(2L, 2L), .Dimnames = list(c("2200", "2300"
), c("2200", "2300")))), .Names = c("3280.2000", "160.2000"
))
#k2 Looks like
# $`3280.2000`
# 400 3500 4000
# 400 0.000 4.198 0.000
# 3500 1.363 1.097 1.098
# 4000 0.000 1.669 0.000
#
# $`160.2000`
# 2200 2300
# 2200 0.935 1.152
# 2300 0.929 0.903
Situation: I have a list of matrices that look as described above. I have matrices of different sizes e.g. (3x3, 2x2, etc) in lists. Each list and each matrix is perfectly similar except for the naming and sorting. I have k=2 (k1 and k2 above) lists I am trying to merge together in a summary table.
Objective: Retrieve data and names from each list, then merge to create summary table.
Possible solutions:
a) Since the matrices are different sizes it makes the most sense to reshape each matrix in each list from square to column. Melt.list() is a good candidate.
b) Change names of rows & columns in matrix. Since the 3280
is in the name of the object, it doesn't need to be in the name of the matrix row/columns. Thus row and column could provide a unique ID which could be successfully melted.
Problems:
a) Melt (or melt.list) truncates my variable names. This upsets me (X1 and X2 below should be 7-8 digits).
# X1 X2 value L1
# 1 3280.35 3500.00 0.000 3280.2000
# 2 3280.40 3500.00 1.363 3280.2000
# 3 3280.40 3500.00 0.000 3280.2000
# etc
b) I can change the colnames for a matrix, but I can't figure out how to do it for multiple matrices in an array. When I try this, I get a list of names (no more values!) rather than an update to the names.
#this works
colnames(k1[[1]]) <- gsub("*.*\\.", "", colnames(k1[[1]]))
#this does not - returns names where values should be!
k1 <- lapply(k1, function (x) {colnames(x) <- gsub("*.*\\.", "", colnames(x))}
Question: How can I either: a) prevent melt from truncating my names or b) change dimnames on a list of matrices?
Lists, names, frames, strata: how many ways can I fix my data?
Searched: reshape2 documentation, plyr documentation, [r] rename matrix columns; column renaming, changing row and column names, how to reshape dataframe, and plenty of others
Thanks in advance.