具有多变量的prop.table

时间:2016-02-23 19:27:44

标签: r survey surveymonkey

我正在寻找重新创建一个由SurveyMonkey填充的表(例如:http://imgur.com/kt3d00g) - 左边是问题(例如,因子X有多重要?)然后离散变量是总是一样的:根本不是,有点......等等。

采用格式数据的最佳方法是什么(列#s是问题并以结果prop.table结束?:

function getToken(){
    document.cookie = "path=/; csrftoken = {% csrf_token %}";
    //alert($.cookie('csrftoken'));
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'api_url', false);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
    xhr.send(JSON.stringify({"username":document.getElementById("login").value,
                "password":document.getElementById("password").value}));

    xhr.onreadystatechange = function(){ // <-- you need this..

        if (xhr.status != 200) {
            alert( "Введите корректную комбинацию логина и пароля!" );

                }
        else {
            alert( xhr.responseText );
            window.location.href = "http://www.google.com";
            document.cookie = "token = " + JSON.parse(xhr.responseText).key;
        }
   }
} 

1 个答案:

答案 0 :(得分:4)

首先

Tidy your data。假设您的原始数据集位于名为data.frame的{​​{1}}中:

df
library(tidyr)

df.tidy <- gather(df, question, result)

prop.table(table(df.tidy$question, df.tidy$result))
#     Extremely Moderately Not at all   Slightly       Very
# Q1 0.04166667 0.04166667 0.08333333 0.04166667 0.04166667
# Q2 0.04166667 0.04166667 0.00000000 0.04166667 0.12500000
# Q3 0.12500000 0.00000000 0.04166667 0.00000000 0.08333333
# Q4 0.04166667 0.08333333 0.04166667 0.08333333 0.00000000

如果您需要&#34;提问&#34;百分比,您需要在# Additional stuff to look at to check your understanding... # # df.tidy # table(df.tidy$question, df.tidy$result) # prop.table(table(df.tidy$question, df.tidy$result), margin = 1) # prop.table(table(df.tidy$question, df.tidy$result), margin = 2) 的调用中使用margin = 1 - 请参阅prop.table()