我很难生成使用xtable包的LaTex格式表。我试图做三件事:
没有第一列只是编号的行。
让列为固定宽度。
使用longtable环境,因为它是一个大表。
以下是我尝试导出为LaTex表格的对象:
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有一个align参数,但是当我使用时(从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")
,对吗?
答案 0 :(得分:1)
要修复错误,请在align
字符串中使用两个反斜杠,如下所示:
xtable(df, align=c("l", "l", "l", "p\\{5cm\\}"))
您的数据框没有行名称,默认情况下在打印xtable
对象时包含这些行名称,因此它使用行号。为避免这种情况,请在include.rownames=FALSE
函数中使用print()
。
print(xtable(df, align=c("l", "l", "l", "p\\{5cm\\}")),
include.rownames=FALSE,
tabular.environment="longtable")