-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumstats_by_trt.Rmd
195 lines (143 loc) · 4.72 KB
/
sumstats_by_trt.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: "R Notebook"
output: html_notebook
---
```{r}
load("hw2_list.rda")
```
```{r}
calculate_stats <- function(bp_list, stat = "mean") {
#get `stat` param from function
stat_arg <- get(stat)
#initialize return list
sumstats <- list()
# get names
for (i in names(bp_list)) {
df <- bp_list[[i]]
by_trt_stats <-c()
# get numeric columns
num_vars <- names(df)[sapply(df, is.numeric)]
# calculate stats for numeric variables
for (j in num_vars) {
col_stat <- stat_arg(df[[j]])
stat_name <- paste(stat, j, sep = "_")
by_trt_stats[stat_name] <- col_stat
}
# store vector in the list by group name
sumstats[[i]] <- as.data.frame(t(by_trt_stats))
}
# return list of data frames
return(sumstats)
}
tst_mean_def<-calculate_stats(bp_list)
```
### create a nested List from df_names as levels/parent and numeric vars and sumstats
```{r}
attributes(bp_list)
typeof(bp_list)
str(bp_list)
summary(bp_list) # bp_List is a list containing 2 data frames
is.vector(bp_list)
is.data.frame(bp_list[[1]])
names(bp_list)
get_df_names <- function(bp_list, stat = "mean"){
stat__ <- get(stat)
df_names <- list()
return_list <- list()
for(item in names(bp_list)){
df_names <- item
numeric_var_names <- vector()
df_stats <- vector()
for (j in bp_list){
numeric_var_names <- (names(j)[sapply(j,is.numeric)])
df_stats <- c(stat__(j$pre_bp), stat__(j$post_bp), stat__(j$diff_bp))
}
return_list <- append(return_list, list(df_names, numeric_var_names, df_stats))
}
return(return_list)
}
dfnamestst <- get_df_names(bp_list)
```
### Just create the 6 element list, but provide names for each stat in the list
```{r}
hobbies <- c("reading", "hiking", "coding")
hobby_string <- paste(hobbies, collapse = "_")
print(hobby_string) # Output: "reading, hiking, coding"
get_var_string <- function(bp_list, stat = "mean"){
stat__ <- get(stat)
df_names <- names(bp_list)
return_df <- data.frame()
for(i in seq_along(bp_list)){
df_stats <- c()
df <- bp_list[[i]]
df_name <- df_names[i]
num_vars <- names(df)[sapply(df, is.numeric)]
# df_stats <- c(stat__(i$pre_bp), stat__(i$post_bp), stat__(i$diff_bp))
for(col in num_vars){
col_stat <- stat__(df[[col]])
print(col_stat)
var_string <- paste(df_name, col, sep= "_")
df_stats <- append(df_stats, col_stat)
}
return_df <- rbind(return_df,col_stat)
}
return(return_df)
}
x <- get_var_string(bp_list)
create_named_list <- function(bp_list, stat = "mean"){
stat__ <- get(stat)
var_string <- vector()
for(i in bp_list){
var_string <- vector()
var_string <- paste((names(i)[sapply(i,is.numeric)]), sep = "_")
print(var_string)
}
}
x2 <- create_named_list(bp_list)
# get_df_names <- function(bp_list, stat = "mean"){
# stat__ <- get(stat)
# df_names <- list()
# return_list <- list()
# for(item in names(bp_list)){
# df_names <- item
# numeric_var_names <- vector()
# df_stats <- vector()
# for (j in bp_list){
# numeric_var_names <- (names(j)[sapply(j,is.numeric)])
# df_stats <- c(stat__(j$pre_bp), stat__(j$post_bp), stat__(j$diff_bp))
# }
# return_list <- append(return_list, list(df_names, numeric_var_names, df_stats))
# }
#
# return(return_list)
# }
# dfnamestst <- get_df_names(bp_list)
```
### What am I trying to do?
- iterate through a list of dataframes (bp_list), extracting:
- names of dataframes ("treatment" and "placebo")
- names of numeric variables
- summarize numeric columns
- label the things to make sense
- return output across trt, pbo
- return a list of dataframes containing summary stats for numeric columns
- dataframes for treatment regimen (trt, pbo)
- meaningfully labeled?
### What is the issue?
- flow control. I really hate nesting for loops if there is a faster alternative (I don't know it)
- addressing elements of:
- lists
- dataframes inside of lists
```{r}
names(bp_list)
bp_list[[1]]
bp_list[1] #don't use these
bp_list$treatment #don't use these
length(bp_list)
for (item in bp_list){
#R just knows bp_list is a list of 2 items
# print(bp_list[item]) #why doesn't this return $treatment like it does above?
print(item) #why does this give an invalid subscript for type 'list' error
# print(bp_list$item) #why does this return NULL, when outside of a loop it returns a dataframe?
}
```