R - 停止 write.xlsx() 使列名在语法上有效
R - stop write.xlsx() from making column names syntactically valid
我确实有一个关于 R 中 xlsx
包的 write.xlsx()
函数的具体问题。
我确实有一个包含面板数据回归输出的矩阵。为了方便起见,我确实想将这些输出导出到 .xlsx 文件以将其包含在 Word 文档中。但是,当我通过 write.xlsx
导出矩阵时,列名会自动编辑为语法有效的样式,除了点、字母和数字外没有其他符号。所以,矩阵的列向量
table_columns <- c("Estimate (All periods)", "", "s.e. (All periods)", "Estimate (Periods 0 & 1)", "", "s.e. (Periods 0 & 1)")
(""
是空列名称,因为这些列包含 p 值的星号指示符)
变成
Estimate..All.periods.|V2|s.e...All.periods.|Estimate..Periods.0...1.|V5|s.e...Periods.0...1.
有没有办法抑制这种重新格式化,这样我就不需要在输出后编辑 .xlsx 文件?
-编辑-使事情更容易理解:
我有一个矢量 table_rows
c("All periods", " Std. error (All periods)", "Periods 0 & 1", " Std. error (Periods 0 & 1)")
和向量 table_columns
与 c("(1) 1AM - 6AM", "", "(2) 6AM - 10AM", "", "(3) 10AM - 6PM", "", "(4) 6PM - 10PM", "", "(5) 10PM - 1AM", "", "(6) Daily", "")
这些用于设置矩阵的dimnames table
matrix(c(table_1, table_1p, table_2, table_2p, table_3, table_3p, table_4, table_4p, table_5, table_5p, table_6, table_6p), 4, 12, dimnames = list(table4_rows, table4_columns))
其中 table_odd
和 table_oddp
表示包含估计值及其 p 级指标的列向量,table_even
括号中的标准误差。所有向量的类型都是 chr
我 运行 write.xlsx(table, paste0(getwd(), "/results.xlsx"), sheetName = "Results")
将矩阵转换为 .xlsx
在将矩阵传递给 wo write.xlsx
之前,您必须将其转换为数据框。否则 write.xlsx
将使用默认参数完成工作。但是,您需要设置 check.names=F
和 fix.empty.names=F
以禁用语法检查和替换空列名称。
这里有一个 reproducible example - 请 post 以后这样的问题:
library(xlsx)
table_rows <- c("All periods", " Std. error (All periods)", "Periods 0 & 1", " Std. error (Periods 0 & 1)")
table_columns <- c("(1) 1AM - 6AM", "", "(2) 6AM - 10AM", "", "(3) 10AM - 6PM", "", "(4) 6PM - 10PM", "", "(5) 10PM - 1AM", "", "(6) Daily", "")
m <- matrix(seq_len(4*12), 4, 12, dimnames = list(table_rows, table_columns))
# convert matrix to list so that one can use as.data.frame's fix.empty.names=F:
df <- as.data.frame(setNames(lapply(1:ncol(m), function(x) m[, x]), colnames(m)), check.names=F, fix.empty.names = F)
write.xlsx(df, tf <- tempfile(fileext = ".xlsx"))
shell.exec(tf)
我确实有一个关于 R 中 xlsx
包的 write.xlsx()
函数的具体问题。
我确实有一个包含面板数据回归输出的矩阵。为了方便起见,我确实想将这些输出导出到 .xlsx 文件以将其包含在 Word 文档中。但是,当我通过 write.xlsx
导出矩阵时,列名会自动编辑为语法有效的样式,除了点、字母和数字外没有其他符号。所以,矩阵的列向量
table_columns <- c("Estimate (All periods)", "", "s.e. (All periods)", "Estimate (Periods 0 & 1)", "", "s.e. (Periods 0 & 1)")
(""
是空列名称,因为这些列包含 p 值的星号指示符)
变成
Estimate..All.periods.|V2|s.e...All.periods.|Estimate..Periods.0...1.|V5|s.e...Periods.0...1.
有没有办法抑制这种重新格式化,这样我就不需要在输出后编辑 .xlsx 文件?
-编辑-使事情更容易理解:
我有一个矢量 table_rows
c("All periods", " Std. error (All periods)", "Periods 0 & 1", " Std. error (Periods 0 & 1)")
和向量 table_columns
与 c("(1) 1AM - 6AM", "", "(2) 6AM - 10AM", "", "(3) 10AM - 6PM", "", "(4) 6PM - 10PM", "", "(5) 10PM - 1AM", "", "(6) Daily", "")
这些用于设置矩阵的dimnames table
matrix(c(table_1, table_1p, table_2, table_2p, table_3, table_3p, table_4, table_4p, table_5, table_5p, table_6, table_6p), 4, 12, dimnames = list(table4_rows, table4_columns))
其中 table_odd
和 table_oddp
表示包含估计值及其 p 级指标的列向量,table_even
括号中的标准误差。所有向量的类型都是 chr
我 运行 write.xlsx(table, paste0(getwd(), "/results.xlsx"), sheetName = "Results")
将矩阵转换为 .xlsx
在将矩阵传递给 wo write.xlsx
之前,您必须将其转换为数据框。否则 write.xlsx
将使用默认参数完成工作。但是,您需要设置 check.names=F
和 fix.empty.names=F
以禁用语法检查和替换空列名称。
这里有一个 reproducible example - 请 post 以后这样的问题:
library(xlsx)
table_rows <- c("All periods", " Std. error (All periods)", "Periods 0 & 1", " Std. error (Periods 0 & 1)")
table_columns <- c("(1) 1AM - 6AM", "", "(2) 6AM - 10AM", "", "(3) 10AM - 6PM", "", "(4) 6PM - 10PM", "", "(5) 10PM - 1AM", "", "(6) Daily", "")
m <- matrix(seq_len(4*12), 4, 12, dimnames = list(table_rows, table_columns))
# convert matrix to list so that one can use as.data.frame's fix.empty.names=F:
df <- as.data.frame(setNames(lapply(1:ncol(m), function(x) m[, x]), colnames(m)), check.names=F, fix.empty.names = F)
write.xlsx(df, tf <- tempfile(fileext = ".xlsx"))
shell.exec(tf)