2021年5月4日星期二

Set colnames from one list of dataframes to match another list of dataframes

I have two list of dataframes which I want to combine, however, they have slightly different colnames and I can not rbind them because of it.

I want the second list to match the names of the first list and I am attempting to update the names of the second list so that I can rbind. Ultimately the end product will be for presentation purposes.

Small section of my two lists of dataframes

l1 <- list(Fe = structure(list(Determination_No = 1:6, `2` = c(55.94,   55.7, 56.59, 56.5, 55.98, 55.93), `3` = c(56.83, 56.54, 56.18,   56.5, 56.51, 56.34), `4` = c(56.39, 56.43, 56.53, 56.31, 56.47,   56.35), `5` = c(56.32, 56.29, 56.31, 56.32, 56.39, 56.32), `7` = c(56.48,   56.4, 56.54, 56.43, 56.73, 56.62), `8` = c(56.382, 56.258, 56.442,   56.258, 56.532, 56.264), `10` = c(56.3, 56.5, 56.2, 56.5, 56.7,   56.5), `12` = c(56.11, 56.46, 56.1, 56.35, 56.36, 56.37), Overall = c("",   "", "", "", "", "")), class = "data.frame", row.names = c(NA,   -6L)), SiO2 = structure(list(Determination_No = 1:6, `2` = c(7.63,   7.65, 7.73, 7.67, 7.67, 7.67), `3` = c(7.84, 7.69, 7.59, 7.77,   7.74, 7.64), `4` = c(7.67, 7.74, 7.62, 7.81, 7.66, 7.8), `5` = c(7.91,   7.84, 7.96, 7.87, 7.84, 7.92), `7` = c(7.77, 7.83, 7.76, 7.78,   7.65, 7.74), `8` = c(7.936, 7.685, 7.863, 7.838, 7.828, 7.767  ), `10` = c(7.872684992, 7.851291827, 7.872684992, 7.722932832,   7.680146501, 7.615967003), `12` = c(7.64, 7.71, 7.71, 7.65, 7.82,   7.68), Overall = c("", "", "", "", "", "")), class = "data.frame", row.names = c(NA,   -6L)), Al2O3 = structure(list(Determination_No = 1:6, `2` = c(2.01,   2.02, 2.03, 2.01, 2.02, 2), `3` = c(2.01, 2.01, 2, 2.02, 2.02,   2.03), `4` = c(2, 2.03, 1.99, 2.01, 2.01, 2.01), `5` = c(2.02,   2.02, 2.05, 2.03, 2.02, 2.03), `7` = c(1.88, 1.9, 1.89, 1.88,   1.88, 1.87), `8` = c(2.053, 2.044, 2.041, 2.038, 2.008, 2.02),       `10` = c(2.002830415, 2.021725042, 2.021725042, 1.983935789,       2.002830415, 2.021725042), `12` = c(2.09, 2.05, 1.96, 2.09,       2.06, 2.02), Overall = c("", "", "", "", "", "")), class = "data.frame", row.names = c(NA,   -6L)))    l2 <- list(Fe = structure(list(V1 = c("Count", "Min", "Max", "Median",   "Mean", "Std Dev", "Coeff. Variation", "Dev. From Cert Mean",   "95% Confidence Interval"), `2` = c("0", "   NA", "   NA", "   NA",   "  NaN", "   NA", "  NA", "  NaN", ""), `3` = c("6", "56.18",   "56.83", "56.50", "56.48", "0.218", "0.39", " 0.13", ""), `4` = c("6",   "56.31", "56.53", "56.41", "56.41", "0.080", "0.14", " 0.01",   ""), `5` = c("6", "56.29", "56.39", "56.32", "56.33", "0.034",   "0.06", "-0.15", ""), `7` = c("6", "56.40", "56.73", "56.51",   "56.53", "0.125", "0.22", " 0.22", ""), `8` = c("6", "56.26",   "56.53", "56.32", "56.36", "0.116", "0.20", "-0.09", ""), `10` = c("6",   "56.20", "56.70", "56.50", "56.45", "0.176", "0.31", " 0.08",   ""), `12` = c("6", "56.10", "56.46", "56.36", "56.29", "0.150",   "0.27", "-0.21", ""), V10 = c("42", "56.10", "56.83", "56.41",   "56.41", "0.153", "0.27", "", "0.08")), class = "data.frame", row.names = c("LabsampSum",   "LabMinSummary", "LabMaxSummary", "LabMedianSummary", "LabMeanSummary",   "lab.SDSummary", "cv.summmary", "LabDevMean.Summary", "analyte.CI.Summary"  )), SiO2 = structure(list(V1 = c("Count", "Min", "Max", "Median",   "Mean", "Std Dev", "Coeff. Variation", "Dev. From Cert Mean",   "95% Confidence Interval"), `2` = c("6", "7.63", "7.73", "7.67",   "7.67", "0.033", "0.44", "-1.09", ""), `3` = c("6", "7.59", "7.84",   "7.72", "7.71", "0.091", "1.18", "-0.55", ""), `4` = c("6", "7.62",   "7.81", "7.70", "7.72", "0.079", "1.02", "-0.48", ""), `5` = c("6",   "7.84", "7.96", "7.89", "7.89", "0.048", "0.61", " 1.75", ""),       `7` = c("6", "7.65", "7.83", "7.76", "7.76", "0.060", "0.77",       " 0.01", ""), `8` = c("6", "7.68", "7.94", "7.83", "7.82",       "0.086", "1.10", " 0.84", ""), `10` = c("6", "7.62", "7.87",       "7.79", "7.77", "0.111", "1.43", " 0.19", ""), `12` = c("6",       "7.64", "7.82", "7.70", "7.70", "0.065", "0.84", "-0.68",       ""), V10 = c("48", "7.59", "7.96", "7.74", "7.76", "0.097",       "1.25", "", "0.06")), class = "data.frame", row.names = c("LabsampSum",   "LabMinSummary", "LabMaxSummary", "LabMedianSummary", "LabMeanSummary",   "lab.SDSummary", "cv.summmary", "LabDevMean.Summary", "analyte.CI.Summary"  )), Al2O3 = structure(list(V1 = c("Count", "Min", "Max", "Median",   "Mean", "Std Dev", "Coeff. Variation", "Dev. From Cert Mean",   "95% Confidence Interval"), `2` = c("6", "2.00", "2.03", "2.01",   "2.01", "0.010", "0.52", "-0.16", ""), `3` = c("6", "2.00", "2.03",   "2.01", "2.01", "0.010", "0.52", "-0.16", ""), `4` = c("6", "1.99",   "2.03", "2.01", "2.01", "0.013", "0.66", "-0.49", ""), `5` = c("6",   "2.02", "2.05", "2.02", "2.03", "0.012", "0.58", " 0.50", ""),       `7` = c("0", "  NA", "  NA", "  NA", " NaN", "   NA", "  NA",       "  NaN", ""), `8` = c("6", "2.01", "2.05", "2.04", "2.03",       "0.017", "0.82", " 0.78", ""), `10` = c("6", "1.98", "2.02",       "2.01", "2.01", "0.015", "0.77", "-0.45", ""), `12` = c("0",       "  NA", "  NA", "  NA", " NaN", "   NA", "  NA", "  NaN",       ""), V10 = c("36", "1.98", "2.05", "2.01", "2.02", "0.016",       "0.77", "", "0.01")), class = "data.frame", row.names = c("LabsampSum",   "LabMinSummary", "LabMaxSummary", "LabMedianSummary", "LabMeanSummary",   "lab.SDSummary", "cv.summmary", "LabDevMean.Summary", "analyte.CI.Summary"  )))    

I have tried

l3 <- lapply(l2, setnames(l2,colnames(l1)))    

I get the following error message Error in setnames(l2, colnames(l1)) : x is not a data.table or data.frame

This doesn't make sense to me as I set both lists to have dataframes with the following code

l2 <- lapply(l2, as.data.frame)  

what am I missing for this error to occur? or Can i just coerce the the l2 to take on the names of l1?

thanks

https://stackoverflow.com/questions/67394693/set-colnames-from-one-list-of-dataframes-to-match-another-list-of-dataframes May 05, 2021 at 11:08AM

没有评论:

发表评论