我正在使用名为“memisic”的软件包来生成我的2000变量调查的代码簿。码本几乎是一个频率表,带有描述和变量名称的措辞。该包提供了一个称为codebook的函数,该函数生成一个codebook对象。问题是我不能在任何地方写这个对象。我试着将它写入文本文件或pdf文件,但它不起作用。
这是生成码本的代码(作者的代码):
library(memisc)
Data <- data.set(
vote = sample(c(1,2,3,8,9,97,99),size=300,replace=TRUE),
region = sample(c(rep(1,3),rep(2,2),3,99),size=300,replace=TRUE),
income = exp(rnorm(300,sd=.7))*2000
)
Data <- within(Data,{
description(vote) <- "Vote intention"
description(region) <- "Region of residence"
description(income) <- "Household income"
wording(vote) <- "If a general election would take place next tuesday,
the candidate of which party would you vote for?"
wording(income) <- "All things taken into account, how much do all
household members earn in sum?"
foreach(x=c(vote,region),{
measurement(x) <- "nominal"
})
measurement(income) <- "ratio"
labels(vote) <- c(
Conservatives = 1,
Labour = 2,
"Liberal Democrats" = 3,
"Don't know" = 8,
"Answer refused" = 9,
"Not applicable" = 97,
"Not asked in survey" = 99)
labels(region) <- c(
England = 1,
Scotland = 2,
Wales = 3,
"Not applicable" = 97,
"Not asked in survey" = 99)
foreach(x=c(vote,region,income),{
annotation(x)["Remark"] <- "This is not a real survey item, of course ..."
})
missing.values(vote) <- c(8,9,97,99)
missing.values(region) <- c(97,99)
})
r=codebook(Data)
所以我的最终目标是将对象R写入pdf / word / excel / text文件。任何这些都会很棒。
答案 0 :(得分:2)
从中获取文本文件的最简单方法是使用capture.output
:
capture.output(r, file="test.txt")
以下是回读R的前几行:
head(readLines("test.txt"))
# [1] "==================================================================================="
# [2] ""
# [3] " vote 'Vote intention'"
# [4] ""
# [5] " \"If a general election would take place next tuesday, the candidate of which"
# [6] " party would you vote for?\""
答案 1 :(得分:0)
使用Write函数可以将代码本直接输出到txt文件:
Write(codebook(Data), file = "datacodebook.txt")