xtable:添加了额外的列并且使用对齐参数时出错

xtable: extra column added and error using align argument

我很难生成 LaTex 格式 table 我想使用 xtable 包。我正在尝试做三件事:

  1. 第一列不是简单的编号行。

  2. 让列的宽度固定。

  3. 使用 longtable 环境因为它是一个大 table.

这是我要导出为 LaTex 的对象 table:

structure(list(Name = structure(c(23L, 20L, 21L, 22L, 14L, 11L, 
12L, 13L, 15L, 5L, 4L, 3L, 2L, 6L, 7L, 8L, 9L, 10L, 17L, 18L, 
19L, 24L, 25L, 28L, 29L, 27L, 26L, 16L, 1L), .Label = c("age.cat", 
"ALQ101", "BMXBMI", "BMXHT", "BMXWT", "CBD070", "CBD090", "CBD110", 
"CBD120", "CBD130", "DMDBORN4", "DMDEDUC2", "DMDMARTL", "DMQMILIZ", 
"INDHHIN2", "is.obese", "PAD680", "PAQ710", "PAQ715", "RIAGENDR", 
"RIDAGEYR", "RIDRETH3", "SEQN", "SMQ020", "SMQ680", "weight.status", 
"WHD140", "WHQ030", "WHQ040"), class = "factor"), Description = structure(c(25L, 
10L, 1L, 9L, 18L, 7L, 8L, 17L, 3L, 29L, 12L, 6L, 11L, 20L, 24L, 
23L, 22L, 21L, 19L, 14L, 13L, 27L, 28L, 15L, 16L, 26L, 5L, 4L, 
2L), .Label = c("Age", "Age Range", "Annual Hld Income", "BMI Based Obesity Status", 
"BMI Based Weight Category", "Body Mass Index", "Country of Birth", 
"Education Level", "Ethnicity", "Gender", "Had at Least 12 Drinks in Past Year", 
"Height (cm)", "Hours Computer Use Per Day", "Hours Watch TV Per Day", 
"How Do You Consider Your Weight", "Like to Weight More, Less, or Same", 
"Marital Status", "Military Service", "Minutes Sedentary Activity Per Day", 
"Money Spent at Supermarket/Grocery Stores", "Money Spent on Carryout/Delivered Foods", 
"Money Spent on Eating Out", "Money Spent on Food at Other Stores", 
"Money Spent on Non-Food Items", "Respondent Sequence Number", 
"Self Reported Greatest Weight (Pounds)", "Smoked at Least 100 Cigarettes in Life", 
"Used Tobacco/Nicotine in Last 5 Days", "Weight (kg)"), class = "factor"), 
    Values = structure(c(1L, 8L, 20L, 10L, 26L, 25L, 7L, 9L, 
    2L, 21L, 23L, 19L, 26L, 17L, 13L, 15L, 16L, 18L, 14L, 6L, 
    5L, 26L, 26L, 12L, 11L, 22L, 24L, 3L, 4L), .Label = c("", 
    "0-4999, 5000-9999, 10000-14999, 15000-19999, 20000-24999, 25000-34999, 35000-44999, 45000-54999, 55000-64999, 65000-74999, Over 20000, Under 20000, 75000-99999, 100000 and over", 
    "0: No, 1: Yes", "20 – 29, 30 – 39, 40 – 49, 50 – 59, 60 – 69, 70 – 79, Over 80", 
    "Less than 1 Hour, 1 Hour, 2 Hours, 3 Hours, 4 Hours, 5 Hours or More, Does Not Use Computer Outside Work/School", 
    "Less than 1 Hour, 1 Hour, 2 Hours, 3 Hours, 4 Hours, 5 Hours or More, Does Not Watch TV", 
    "Less than 9th grade, 9-11, High School Graduate/GED, Some college or AA degree, College Graduate or Above", 
    "Male, Female", "Married, Widowed, Diviced, Separated, Never Married, Living with Partner", 
    "Mexican-American, Other-Hispanic, Non-Hispanic White, Non-Hispanic Black, Non-Hispanic Asian, Other", 
    "More, Less, Same", "Overweight, Underweight, About Right", 
    "Range of Values 0 – 1071", "Range of Values 0 – 1380", "Range of Values 0 – 2142", 
    "Range of Values 0 – 3000", "Range of Values 0 – 5142", "Range of Values 0 – 8142", 
    "Range of Values 12.4 – 82.1", "Range of Values 20 - 80", 
    "Range of Values 3.6 - 216.1", "Range of Values 75 – 550", 
    "Range of values 82 – 204.5", "Underweight, Normal, Overweight, Obese", 
    "United States, Other", "Yes, No"), class = "factor")), .Names = c("Name", 
"Description", "Values"), class = "data.frame", row.names = c(NA, 
-29L))

如果我只使用命令 xtable(df),我会得到四列 table,其中第一列只是对行进行编号。我怎么不能这样做?

编辑:我被告知链接的解决方案中存在拼写错误。我已经在这里修复了它,但我仍然得到同样的错误。

我知道 xtable 有一个对齐参数,但是当我使用(从 this solution 复制)

xtable(df, align=c("l","l","l","p\{5cm\}"))

我得到一个错误:

\{ is an urecognized escape in character string starting ""p\{".

我试过使用双反斜杠,但也没用。

一旦我解决了前两个问题,我相信命令就是 print(df, tabular.environment = "longtable"),正确吗?

要修复错误,请在 align 字符串中使用两个反斜杠,如下所示:

xtable(df, align=c("l", "l", "l", "p\{5cm\}"))

您的数据框没有行名,打印 xtable 对象时默认包含行名,因此它使用行号。为避免这种情况,请在 print() 函数中使用 include.rownames=FALSE

print(xtable(df, align=c("l", "l", "l", "p\{5cm\}")),
      include.rownames=FALSE,
      tabular.environment="longtable")