我正在尝试使用我的表的第一行作为列名(它是转置表),但第1行中的值不希望显示为列标题,我只是得到一些奇怪的数字而不是。
**我无法手动设置列名,因为它是我的闪亮应用程序的一部分,列的名称和它们的编号不同
以下是示例数据:
=IF(Max(A1:A32)>abs(MIN(A1:A32)),MAX(A1:A32),MIN(A1:A32)
和
data <- structure(list(`1` = structure(c(1L, 30L, 37L, 24L, 32L, 26L), .Names = c("Seq",
"a", "b", "c", "d",
"e"), .Label = c(">1", "0", "0.02", "0.04", "0.05",
"0.06", "0.07", "0.08", "0.09", "0.14", "0.18", "0.20", "0.22",
"0.24", "0.25", "0.28", "0.40", "0.44", "0.47", "0.5", "0.50",
"0.52", "0.56", "0.66", "0.67", "0.88", "1.0", "1.03", "1.17",
"1.42", "1.73", "1018", "2.5", "2.6", "3.0", "3.00", "3.5", "4.0",
"4.5", "555", "702", "703", "995"), class = "factor"), `28` = structure(c(28L,
30L, 34L, 22L, 2L, 27L), .Names = c("Seq",
"a", "b", "c", "d",
"e"), .Label = c(" 523", " 524", "0", "0.00", "0.02", "0.05", "0.08",
"0.09", "0.11", "0.13", "0.20", "0.27", "0.35", "0.39", "0.40",
"0.47", "0.48", "0.49", "0.5", "0.50", "0.55", "0.57", "0.69",
"0.72", "0.83", "0.95", "0.98", "1", "1.0", "1.67", "1.70", "2.0",
"2.5", "3.5", "3.50", "4.0", "5.0", "503", "504", "510", "607",
"723", "725", "726", "729"), class = "factor"), `32` = structure(c(32L,
30L, 36L, 24L, 1L, 27L), .Names = c("Seq",
"a", "b", "c", "d",
"e"), .Label = c(" 586", " 587", "0", "0.00", "0.03", "0.06", "0.07",
"0.08", "0.09", "0.10", "0.12", "0.13", "0.19", "0.20", "0.26",
"0.30", "0.32", "0.35", "0.46", "0.48", "0.49", "0.5", "0.55",
"0.65", "0.67", "0.69", "0.94", "1.0", "1.00", "1.54", "1.74",
"2", "2.5", "2.50", "3.0", "3.5", "3.50", "4.0", "4.5", "464",
"5.0", "551", "552", "553", "554", "571", "572", "573"), class = "factor")), .Names = c("1",
"28", "32"), row.names = c("Seq",
"a", "b", "c", "d",
"e"), class = "data.frame")
正如我在开头提到的(以上代码也显示它)我希望数据的第一行是我的数据框的标题。
结果应该是这样的:
colnames(data) <- data[1, ]
为什么不工作?
感谢您的帮助!
答案 0 :(得分:3)
您需要先将变量转换为字符
data[] <- lapply(data, as.character)
names(data) <- data[1, ]
data <- data[-1,]
data
# >1 1 2
#a 1.42 1.67 1.54
#b 3.5 3.5 3.5
#c 0.66 0.57 0.65
#d 1018 524 586
#e 0.88 0.98 0.94
如果你想保留这些因素,那么
names(data) <- unlist(data[1,])