xtable과 함께 라텍스에있는 서브 테이블이있는 테이블을 출력합니다

StackOverflow https://stackoverflow.com//questions/24047217

  •  21-12-2019
  •  | 
  •  

문제

여러 개의 다른 하위 테이블이있는 라텍스 테이블을 만들고 싶습니다.나는 기본적으로 설문 조사에서 답변으로 테이블을 생성하고 싶습니다.

예를 들어 첫 번째 질문에 대한 테이블은 다음과 같습니다.

Q1 <- structure(c(6L, 14L, 20L, 15L, 2L, 3L, 12L, 25L, 7L, 1L, 2L, 
13L, 35L, 10L, 3L), .Dim = c(5L, 3L), .Dimnames = structure(list(
    c("none", "very little", "some", "most", "all"), c("control", 
    "treatment1", "treatment2")), .Names = c("", "")), class = "table")
.

두 번째 질문에 대한 테이블은 다음과 같습니다.

Q2 <- structure(c(39L, 12L, 4L, 1L, 1L, 31L, 13L, 4L, 0L, 0L, 39L, 
20L, 4L, 0L, 0L), .Dim = c(5L, 3L), .Dimnames = structure(list(
    c("A", "B+", "B", "B-", "C"), c("control", "treatment1", 
    "treatment2")), .Names = c("", "")), class = "table")
.

라텍스 출력이

처럼 보이기를 원합니다.
                    Some Title
                    Question 1
             control treatment1 treatment2
  none              6          3          2
  very little      14         12         13
  some             20         25         35
  most             15          7         10
  all               2          1          3
                    Question 2
  A                39         31         39
  B+               12         13         20
  B                 4          4          4
  B-                1          0          0
  C                 1          0          0
.

X 테이블을 사용하여 개별 테이블을 만들 수 있지만 라텍스에서 수동 작업이 필요하면 라텍스를 병합해야합니다.지금 당장 :

print(xtable(Q1), floating = FALSE, only.contents = FALSE, 
      include.rownames = TRUE, include.colnames = TRUE, hline.before = c(1))
.

도움이 되었습니까?

해결책

이 방법은 해결 방법의 약간이지만 찾고있는 것에 가깝습니다.작업 가능한 구조를 얻으려면 Epitools 패키지에서 expand.table로 테이블을 확장하고 테이블 패키지에서 tabular 함수를 사용하여 사용할 수있는 형식을 얻을 필요가있었습니다.이것은 가장 좋거나 가장 효율적인 방법이 아닐 수도 있지만 나중에 라텍스 처리를 피하려고하는 경우 도움이 될 수 있습니다.필터링 및 매트릭스로 변환 한 후 Xtable을 사용하여 다음 표를 생성 할 수 있습니다.

여기에 이미지 설명을 입력하십시오

r 코드

require(xtable)
require(epitools)
require(tables)

Q1 <- structure(c(6L, 14L, 20L, 15L, 2L, 3L, 12L, 25L, 7L, 1L, 2L, 
                  13L, 35L, 10L, 3L), .Dim = c(5L, 3L), .Dimnames = structure(list(
                    c("none", "very little", "some", "most", "all"), c("control", 
                                                                       "treatment1", "treatment2")), .Names = c("", "")), class = "table")

Q2 <- structure(c(39L, 12L, 4L, 1L, 1L, 31L, 13L, 4L, 0L, 0L, 39L, 
                  20L, 4L, 0L, 0L), .Dim = c(5L, 3L), .Dimnames = structure(list(
                    c("A", "B+", "B", "B-", "C"), c("control", "treatment1", 
                                                    "treatment2")), .Names = c("", "")), class = "table")


names(dimnames(Q1)) <- c("Responses", "Conditions")
names(dimnames(Q2)) <- c("Responses", "Conditions")
q1_df <- expand.table(Q1)
q1_df$Question <- "Question 1"
q2_df <- expand.table(Q2)
q2_df$Question <- "Question 2"
df <- rbind(q1_df, q2_df)


tab <- tabular((Factor(Question)*Factor(Responses)+1) ~ (Conditions), data=df)
tab <- tab[tab[,1] > 0,]
tab <- as.matrix(tab[1:nrow(tab) -1,])

print(xtable(tab, caption="Some Title"), include.rownames=F, include.colnames=F, latex.environments="center", comment=F, caption.placement='top', hline.after=c(0, nrow(tab)))
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top