# [R] Function inside Sweave

Silvano Cesar da Costa silvano at uel.br
Wed Jan 8 19:23:33 CET 2014

```Hi,

I have a data set involving 25 Questions (Q1, Q2, ... , Q25), 100
Disciplina and 5 series. The variables are:

ALUNO DISCIPLINA SERIE TURMA Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 ... Q25

I want to create tables associating each of the 25 questions to the
Disciplina.
Something like:

tab1 = table(DISCIPLINA, Q1)
tab1.prop = round(100*prop.table(tab1, 1),  2)
tab1.txt = xtable(tab1.prop, align="l|rrrrr", label='Q1',
caption=c("Apresentação da proposta de programa a ser desenvolvida na
disciplina", "Q1"))
print(tab1.txt, format.args=list(big.mark = ".", decimal.mark = ","),
caption.placement='top', table.placement='H')

With the help of a friend, was created the following function:

require(xtable)

DISCIPLINA <- rep(c("A", "B", "C", "D", "E"), 5)
for (i in 1:25){
aux <- paste("Q", i, sep="")
assign(aux, sample(rep(seq(1:5),5)))
}

cria.tabela <- function(Questao){
Questao1 <- get(Questao)
tab1 <- table(DISCIPLINA, Questao1)
tab1.prop = round(100*prop.table(tab1, 1),  2)
tab1.txt = xtable(tab1.prop, align="l|rrrrr", label=Questao,
caption=paste(Questao))
print(tab1.txt, format.args=list(big.mark = ".", decimal.mark = ","),
caption.placement='top', table.placement='H')
}

geral <- function(){
for (i in 1:25){
x<-paste("Q", i, sep="")
cria.tabela(x)
}
}

geral()

I need to change the variable DISCIPLINA created in the function by the
variable DISCIPLINA in my dataset, but not working.

require(foreign)
require(xtable)

tail(serie_1)
attach(serie_1)

How can I do this?

Thanks a lot,

---------------------------------------------
Silvano Cesar da Costa