根据连续调查问题的函数迭代 R 脚本

发布于 2024-08-04 10:10:23 字数 657 浏览 8 评论 0原文

下面的函数非常适合我的目的。显示效果很棒。现在我的问题是我需要能够对适合其他模式的其他变量多次重复执行此操作。

在此示例中,我输出了“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-08-11 10:10:23

不确定这是否是您正在寻找的,但要生成问题列表:

> gsub('^', 'q', gsub(' ', '', 
    apply(expand.grid(1:10,letters),1,
           function(r) paste(r, sep='', collapse='')
         )))
  [1] "q1a"  "q2a"  "q3a"  "q4a"  "q5a"  "q6a"  "q7a"  "q8a"  "q9a"  "q10a"
 [11] "q1b"  "q2b"  "q3b"  "q4b"  "q5b"  "q6b"  "q7b"  "q8b"  "q9b"  "q10b"
 [21] "q1c"  "q2c"  "q3c"  "q4c"  "q5c"  "q6c"  "q7c"  "q8c"  "q9c"  "q10c"
 [31] "q1d"  "q2d"  "q3d"  "q4d"  "q5d"  "q6d"  "q7d"  "q8d"  "q9d"  "q10d"
 [41] "q1e"  "q2e"  "q3e"  "q4e"  "q5e"  "q6e"  "q7e"  "q8e"  "q9e"  "q10e"
 [51] "q1f"  "q2f"  "q3f"  "q4f"  "q5f"  "q6f"  "q7f"  "q8f"  "q9f"  "q10f"
 [61] "q1g"  "q2g"  "q3g"  "q4g"  "q5g"  "q6g"  "q7g"  "q8g"  "q9g"  "q10g"
 [71] "q1h"  "q2h"  "q3h"  "q4h"  "q5h"  "q6h"  "q7h"  "q8h"  "q9h"  "q10h"
 [81] "q1i"  "q2i"  "q3i"  "q4i"  "q5i"  "q6i"  "q7i"  "q8i"  "q9i"  "q10i"
 [91] "q1j"  "q2j"  "q3j"  "q4j"  "q5j"  "q6j"  "q7j"  "q8j"  "q9j"  "q10j"
 ...

然后将分析的内部部分转换为一个将问题前缀作为参数的函数:

analyzeQuestion <- function (prefix)
{
  q <- d1[,grep(prefix,colnames(d1))] ## Pull in everything matching q4a_X
  q <- melt(q) ## restructure data for post-hoc

  qaaov <- aov(formula=value~variable,data=q4a) ## anova
  return (LTukey(q4aaov,which="",conf.level=0.95)) ## Tukey's post-hoc
}

现在 - 我不确定您的问题在哪里'q4a' 变量来自(如 aov(..., data=q4a) 中使用的那样) - 所以不确定如何处理该位。但希望这会有所帮助。

将两者放在一起您可以使用 sapply()analyzeQuestion 函数应用于我们自动生成的每个前缀。

Not sure if this is what you are looking for, but to generate the list of questions:

> gsub('^', 'q', gsub(' ', '', 
    apply(expand.grid(1:10,letters),1,
           function(r) paste(r, sep='', collapse='')
         )))
  [1] "q1a"  "q2a"  "q3a"  "q4a"  "q5a"  "q6a"  "q7a"  "q8a"  "q9a"  "q10a"
 [11] "q1b"  "q2b"  "q3b"  "q4b"  "q5b"  "q6b"  "q7b"  "q8b"  "q9b"  "q10b"
 [21] "q1c"  "q2c"  "q3c"  "q4c"  "q5c"  "q6c"  "q7c"  "q8c"  "q9c"  "q10c"
 [31] "q1d"  "q2d"  "q3d"  "q4d"  "q5d"  "q6d"  "q7d"  "q8d"  "q9d"  "q10d"
 [41] "q1e"  "q2e"  "q3e"  "q4e"  "q5e"  "q6e"  "q7e"  "q8e"  "q9e"  "q10e"
 [51] "q1f"  "q2f"  "q3f"  "q4f"  "q5f"  "q6f"  "q7f"  "q8f"  "q9f"  "q10f"
 [61] "q1g"  "q2g"  "q3g"  "q4g"  "q5g"  "q6g"  "q7g"  "q8g"  "q9g"  "q10g"
 [71] "q1h"  "q2h"  "q3h"  "q4h"  "q5h"  "q6h"  "q7h"  "q8h"  "q9h"  "q10h"
 [81] "q1i"  "q2i"  "q3i"  "q4i"  "q5i"  "q6i"  "q7i"  "q8i"  "q9i"  "q10i"
 [91] "q1j"  "q2j"  "q3j"  "q4j"  "q5j"  "q6j"  "q7j"  "q8j"  "q9j"  "q10j"
 ...

And then you turn your inner part of the analysis into a function that takes the question prefix as a parameter:

analyzeQuestion <- function (prefix)
{
  q <- d1[,grep(prefix,colnames(d1))] ## Pull in everything matching q4a_X
  q <- melt(q) ## restructure data for post-hoc

  qaaov <- aov(formula=value~variable,data=q4a) ## anova
  return (LTukey(q4aaov,which="",conf.level=0.95)) ## Tukey's post-hoc
}

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 the analyzeQuestion function to each of the prefixes that we automagically generated.

も让我眼熟你 2024-08-11 10:10:23

我建议融合整个数据集,然后将变量拆分为其组成部分。然后您可以更轻松地使用子集来查看(例如)第四个问题: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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文