问题描述:

I'm having a hard time generating the LaTex format table I want using the xtable package. There are three things I'm trying to do:

  1. Not have the first column be the simply the numbered rows.

  2. Have the columns be a fixed width.

  3. Use the longtable environment because it is a large table.

Here is the object I'm trying to export as a 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))

If I simply use the command xtable(df), I get a four column table where the first column is simply numbering the rows. How can I not do this?

EDIT: I've been informed of a typo in the linked solution. I've fixed it here, but I still get the same error.

I know xtable has an align argument, but when I use (copied from this solution)

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

I get an error:

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

I've tried using a double backslash, but that doesn't work either.

Once I get the first two issues straightened out, I believe the command is simply print(df, tabular.environment = "longtable"), correct?

网友答案:

To fix the error, use two backslashes in the align string, like so:

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

Your data frame doesn't have row names, which are included by default when printing an xtable object, so it uses row numbers. To avoid this, use include.rownames=FALSE in the print() function.

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