Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Complex Experimental Design - Qualtrics Wide to Long Melt Example
Qualtrics.SurveyEngine.addOnload(function()
{
jQuery('#QID1013 input.radio').prop( "checked", true );
jQuery(document).ready(function(){
jQuery('#QID1013 input.radio').prop( "checked", true );
})
});
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 25 10:03:56 2017
@author: Robin van Emden [robin@pwy.nl]
"""
# pandas: imports our CSV and manipulates DataFrame df
# re: enables the use of regular expressions to selectively replace values
import pandas
import re
# string join function, with "##" as the seperator
def sjoin(x): return '##'.join(x[x.notnull()].astype(str))
# load the CSV into a dataframe, exclude header discription
df = pandas.read_csv("data.csv",low_memory=False,skiprows=[1]);
# load the two CSV header rows into a dataframe
df_header = pandas.read_csv("data.csv",nrows=2);
# remove the parentheses from the column names
df = df.rename(columns=lambda x: re.sub(r"\(.*\)","",x))
df_header = df_header.rename(columns=lambda x: re.sub(r"\(.*\)","",x))
# merge the columns that have the same name
df = df.groupby(level=0, axis=1).apply(lambda x: x.apply(sjoin, axis=1))
df_header = df_header.groupby(level=0, axis=1).apply(lambda x: x.apply(sjoin, axis=1))
# remove duplicate descriptions from second row (column descriptions)
#df.iloc[:1] = df.iloc[:1].replace({'##.*': ''}, regex=True)
df_header.iloc[:1] = df_header.iloc[:1].replace({'##.*': ''}, regex=True)
# cleaning up leftover parentheses in second row (column descriptions)
#df.iloc[:1] = df.iloc[:1].replace({'\(.*\)': ''}, regex=True)
df_header.iloc[:1] = df_header.iloc[:1].replace({'\(.*\)': ''}, regex=True)
# lets split C1 to C6 to make them easier to parse later on
for i in range(1, 7):
df['C'+str(i)+'_lbl'], df['C'+str(i)+'_img'] = df['C'+str(i)].str.split('/', 1).str
for i in range(1, 7):
df_header['C'+str(i)+'_lbl'], df_header['C'+str(i)+'_img'] = df_header['C'+str(i)].str.split('/', 1).str
# do some reordering to move the condition values to the front
tofront = ['C1_lbl','C1_img','C2_lbl','C2_img','C3_lbl','C3_img','C4_lbl','C4_img','C5_lbl','C5_img','C6_lbl','C6_img','RAND_LABELS','RAND_IMG_1_TEXT','RAND_IMG_2_TEXT','RAND_IMG_3_TEXT','RAND_IMG_4_TEXT','RAND_IMG_5_TEXT', 'RAND_IMG_6_TEXT','RAND_LABEL']
df = df[ [c for c in tofront if c in df] + [c for c in df if c not in tofront]]
tofront = ['C1_lbl','C1_img','C2_lbl','C2_img','C3_lbl','C3_img','C4_lbl','C4_img','C5_lbl','C5_img','C6_lbl','C6_img','RAND_LABELS','RAND_IMG_1_TEXT','RAND_IMG_2_TEXT','RAND_IMG_3_TEXT','RAND_IMG_4_TEXT','RAND_IMG_5_TEXT', 'RAND_IMG_6_TEXT','RAND_LABEL']
df_header = df_header[ [c for c in tofront if c in df] + [c for c in df if c not in tofront]]
df_header = df_header.fillna('')
df_header = df_header[:1]
filename = "data__MELTED.csv";
df_header.to_csv(filename,float_format='%.6f');
with open(filename, 'a') as f:
df.to_csv(f,header=False,float_format='%.6f');
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
var reorder = "123456".shuffle()
Qualtrics.SurveyEngine.addOnload(function()
{
jQuery('#QID1013 input.radio').prop( "checked", true );
jQuery(document).ready(function(){
jQuery('#QID1013 input.radio').prop( "checked", true );
})
jQuery('#QID1019 .InputText').eq(0).val(reorder.charAt(0));
jQuery('#QID1019 .InputText').eq(1).val(reorder.charAt(1));
jQuery('#QID1019 .InputText').eq(2).val(reorder.charAt(2));
jQuery('#QID1019 .InputText').eq(3).val(reorder.charAt(3));
jQuery('#QID1019 .InputText').eq(4).val(reorder.charAt(4));
jQuery('#QID1019 .InputText').eq(5).val(reorder.charAt(5));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.