根据连续调查问题的函数迭代 R 脚本
下面的函数非常适合我的目的。显示效果很棒。现在我的问题是我需要能够对适合其他模式的其他变量多次重复执行此操作。
在此示例中,我输出了“q4a”的结果,我希望能够针对遵循以下模式的问题序列执行此操作:q4 < a-z>或q< 4-10>>< a - z >,自动地。
有没有某种方法可以迭代此操作,以便指定的变量(在本例中为 q4a)每次都会更改?
这是我的职能:
require(reshape) # Using it for melt
require(foreign) # Using it for read.spss
d1 <- read.spss(...) ## Read in SPSS file
attach(d1,warn.conflicts=F) ## Attach SPSS data
q4a_08 <- d1[,grep("q4a_",colnames(d1))] ## Pull in everything matching q4a_X
q4a_08 <- melt(q4a_08) ## restructure data for post-hoc
detach(d1)
q4aaov <- aov(formula=value~variable,data=q4a) ## anova
提前致谢!
The function below works perfectly for my purpose. The display is wonderful. Now my problem is I need to be able to do it again, many times, on other variables that fit other patterns.
In this example, I've output results for "q4a", I would like to be able to do it for sequences of questions that follow patterns like: q4 < a - z > or q < 4 - 10 >< a - z >, automagically.
Is there some way to iterate this such that the specified variable (in this case q4a) changes each time?
Here's my function:
require(reshape) # Using it for melt
require(foreign) # Using it for read.spss
d1 <- read.spss(...) ## Read in SPSS file
attach(d1,warn.conflicts=F) ## Attach SPSS data
q4a_08 <- d1[,grep("q4a_",colnames(d1))] ## Pull in everything matching q4a_X
q4a_08 <- melt(q4a_08) ## restructure data for post-hoc
detach(d1)
q4aaov <- aov(formula=value~variable,data=q4a) ## anova
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否是您正在寻找的,但要生成问题列表:
然后将分析的内部部分转换为一个将问题前缀作为参数的函数:
现在 - 我不确定您的问题在哪里'q4a' 变量来自(如
aov(..., data=q4a)
中使用的那样) - 所以不确定如何处理该位。但希望这会有所帮助。将两者放在一起您可以使用
sapply()
将analyzeQuestion
函数应用于我们自动生成的每个前缀。Not sure if this is what you are looking for, but to generate the list of questions:
And then you turn your inner part of the analysis into a function that takes the question prefix as a parameter:
Now - I'm not sure where your 'q4a' variable is coming from (as used in the
aov(..., data=q4a)
- so not sure what to do about that bit. But hopefully this helps.To put the two together you can use
sapply()
to apply theanalyzeQuestion
function to each of the prefixes that we automagically generated.我建议融合整个数据集,然后将变量拆分为其组成部分。然后您可以更轻松地使用子集来查看(例如)第四个问题:
subset(molten, q = 4)
。I would recommend melting the entire dataset and then splitting
variable
into its component pieces. Then you can more easily use subset to look at (e.g.) just question four:subset(molten, q = 4)
.