【问题标题】:A function allowing the user to choose beween a list of outputs允许用户在输出列表之间进行选择的功能
【发布时间】:2016-11-16 12:21:51
【问题描述】:

刚开始 R,我被要求创建一个函数,允许其用户获取一个矩阵,其中包含一些定量变量的描述性统计数据。这不是太难,但我不知道如何让用户选择他想要的..

Descriptives=function(w=2,y=stats.data.quant.col) {

    ## w = decimals (2 by default)
    ## y = stats.data.quant.col (by default) OR stats.data.quant.row

    data.quant=data[,sapply(data, function(x) is.numeric(x) | is.integer(x))]

    stats.data.quant=apply(data.quant,2, function(x) c(Nobs=length(x),Moyenne=mean(x),Mediane=median(x),Variance=var(x),Ecart.type=sd(x),Etendue=max(x)-min(x),Coefficient.variation=sd(x)/mean(x),Deciles=quantile(x,seq(0.1,0.9,0.1))))

    stats.data.quant.row=round(stats.data.quant,w)

    stats.data.quant.col=round(t(stats.data.quant),w)

    y

}

谁能给我一个提示?我现在很迷茫

【问题讨论】:

    标签: r


    【解决方案1】:

    您需要以交互方式读取用户输入。方法如下:

    cat("Which of these fine output versions would you like? ")
    cat("Version A: Statistical results")
    cat("Version B: Pretty plots")
    x <- readLines(file("stdin"),1)print(x)
    if(x == "A"){
       # your code goes here
    } else{
       # default output
    }
    

    您也可以使用scan 函数代替readLines

    【讨论】:

    • 我刚看到你的回答,但是谢谢,我最终使用的代码似乎容易多了
    • @Krisprolls 当然。对不起,它没有早点给你。如果您认为这将是未来读者的合适答案,请单击复选标记。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 2018-06-27
    • 2013-07-25
    相关资源
    最近更新 更多