首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xtable:使用对齐参数添加额外列和出错

xtable:使用对齐参数添加额外列和出错
EN

Stack Overflow用户
提问于 2015-04-14 14:11:00
回答 1查看 449关注 0票数 2

我很难用xtable包生成我想要的LaTex格式表。我想做三件事:

  1. 没有第一列只是编号行。
  2. 使列的宽度固定。
  3. 使用长表环境,因为它是一个大表。

下面是我试图作为LaTex表导出的对象:

代码语言:javascript
复制
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),就会得到一个四列表,其中第一列只是对行编号。我怎么能不这么做呢?

编辑:我已经被告知链接解决方案中有一个错误。我已经把它修好了,但我还是会犯同样的错误。

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

代码语言:javascript
复制
xtable(df, align=c("l","l","l","p\{5cm\}"))

我收到一个错误:

代码语言:javascript
复制
\{ is an urecognized escape in character string starting ""p\{".

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

一旦我把前两个问题弄清楚了,我相信这个命令就是print(df, tabular.environment = "longtable"),对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-14 15:13:23

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

代码语言:javascript
复制
xtable(df, align=c("l", "l", "l", "p\\{5cm\\}"))

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

代码语言:javascript
复制
print(xtable(df, align=c("l", "l", "l", "p\\{5cm\\}")),
      include.rownames=FALSE,
      tabular.environment="longtable")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29629628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档