Skip to content

Instantly share code, notes, and snippets.

@salmoni
Created April 8, 2014 15:26
Show Gist options
  • Save salmoni/10142534 to your computer and use it in GitHub Desktop.
Save salmoni/10142534 to your computer and use it in GitHub Desktop.
Grouped descriptives
"""
Grouped descriptives
(c) 2013, Alan James Salmoni
"""
def Count(data):
return len(data)
def Sum(data):
val = 0
for element in data:
val = val + element
return val
def Mean(data):
return Sum(data) / float(Count(data))
class variable(list):
def __init__(self):
self.name = ''
def GetGroups(groupingvars):
groups = []
k = len(groupingvars)
N = len(groupingvars[0])
for idx in range(N):
row = [var[idx] for var in groupingvars]
if row not in groups:
groups.append(row)
return groups
def ExtractGroupsData(group, groupingvars, variable):
N = len(variable)
data = []
for idx in range(N):
row = [element[idx] for element in groupingvars]
if group == row:
data.append(variable[idx])
return data
def GroupedDescriptives(groups, groupingvars, variables, stats):
groupnames = ["Group 1","Group 2"]
scorenames = ["Score 1","Score 2"]
k = len(groups)
table = '<h3>Describe</h3>\n<table>\n'
for column in groupnames:
table += '<th>%s</th>'%column
for stat in stats:
table += '<th>%s</th>'%(stat)
table += '\n'
table += '</table>\n'
return table
def GroupDescriptives(groups, groupingvars, variables, stats):
groupnames = ["Group 1","Group 2"]
scorenames = ["Score 1","Score 2"]
table = '<h3>Descriptives</h3>\n<table border="1">\n'
table += '\t<tr><th>Variable</th>'
for groupname in groupnames:
table += '<th>%s</th>'%(groupname)
for stat in stats:
table += '<th>%s</th>'%(stat)
table += '</tr>\n'
k = len(groups)
for idx, var in enumerate(variables):
table += '\t<tr>'
#for varname in scorenames:
table += '<td rowspan="%d">%s</td>'%(k, scorenames[idx])
for group in groups:
for element in group:
table += '<td>%s</td>'%element
data = ExtractGroupsData(group, groupingvars, var)
if "Count" in stats:
val = Count(data)
table += '<td>%s</td>'%str(val)
if "Sum" in stats:
val = Sum(data)
table += '<td>%s</td>'%str(val)
if "Mean" in stats:
val = Mean(data)
table += '<td>%s</td>'%str(val)
table += '</tr>\n'
table += '</tr>\n'
table += '</table>\n'
return table
if __name__ == "__main__":
a = ["group 1","group 1","group 1","group 1","group 2","group 2","group 2","group 2"]
b = ["Part 1","Part 1","Part 2","Part 2","Part 1","Part 1","Part 2","Part 2"]
c = [1,2,3,2,1,3,2,4]
d = [7,8,9,10,12,9,6,7]
stats = ["Count","Sum","Mean"]
groups = GetGroups([a,b])
print GroupDescriptives(groups, [a,b], [c,d], stats)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment