reformatting code to select needed df for analysis
This commit is contained in:
parent
a6cbaab40a
commit
b72c4df796
7 changed files with 243 additions and 102 deletions
|
@ -13,18 +13,25 @@ getwd()
|
|||
#====================
|
||||
source("read_data.R")
|
||||
|
||||
#============================
|
||||
# Data to use: Important step
|
||||
#============================
|
||||
# select df to use
|
||||
my_data = fp_adults
|
||||
|
||||
# clear unnecessary variables
|
||||
rm(all_df)
|
||||
rm(all_df, adult_df, fp_adults_na)
|
||||
|
||||
########################################################################
|
||||
|
||||
#=========
|
||||
# sam
|
||||
#=========
|
||||
sam_regex = regex(".*_sam[1-3]{1}$", ignore_case = T)
|
||||
sam_cols_i = str_extract(colnames(adult_df), sam_regex) # not boolean
|
||||
#sam_cols_b = colnames(adult_df)%in%sam_cols_i # boolean
|
||||
sam_cols_i = str_extract(colnames(my_data), sam_regex) # not boolean
|
||||
#sam_cols_b = colnames(my_data)%in%sam_cols_i # boolean
|
||||
|
||||
sam_cols = colnames(adult_df)[colnames(adult_df)%in%sam_cols_i]
|
||||
sam_cols = colnames(my_data)[colnames(my_data)%in%sam_cols_i]
|
||||
|
||||
# this contains log columns + daysamp_samXX: omitting these
|
||||
sam_regex_log_days = regex("log|day.*_sam[1-3]{1}$", ignore_case = T, perl = T)
|
||||
|
@ -48,7 +55,7 @@ cat("Extracting SAM cols + metadata_cols")
|
|||
|
||||
if ( length(sam_cols_to_extract) == length(meta_data_cols) + length(sam_cols_clean) ){
|
||||
cat("Extracing", length(sam_cols_to_extract), "columns for sam")
|
||||
sam_df = adult_df[, sam_cols_to_extract]
|
||||
sam_df = my_data[, sam_cols_to_extract]
|
||||
}else{
|
||||
cat("FAIL: length mismatch"
|
||||
, "Expeceted to extract:", length(meta_data_cols) + length(sam_cols_clean), "columns"
|
||||
|
@ -61,10 +68,10 @@ colnames_sam_df = colnames(sam_df); colnames_sam_df
|
|||
# serum
|
||||
#=========
|
||||
serum_regex = regex(".*_serum[1-3]{1}$", ignore_case = T)
|
||||
serum_cols_i = str_extract(colnames(adult_df), serum_regex) # not boolean
|
||||
#serum_cols_b = colnames(adult_df)%in%serum_cols_i # boolean
|
||||
serum_cols_i = str_extract(colnames(my_data), serum_regex) # not boolean
|
||||
#serum_cols_b = colnames(my_data)%in%serum_cols_i # boolean
|
||||
|
||||
serum_cols = colnames(adult_df)[colnames(adult_df)%in%serum_cols_i]
|
||||
serum_cols = colnames(my_data)[colnames(my_data)%in%serum_cols_i]
|
||||
|
||||
# this contains log columns + dayserump_serumXX: omitting these
|
||||
serum_regex_log_days = regex("log|day.*_serum[1-3]{1}$", ignore_case = T, perl = T)
|
||||
|
@ -88,7 +95,7 @@ cat("Extracting SERUM cols + metadata_cols")
|
|||
|
||||
if ( length(serum_cols_to_extract) == length(meta_data_cols) + length(serum_cols_clean) ){
|
||||
cat("Extracing", length(serum_cols_to_extract), "columns for serum")
|
||||
serum_df = adult_df[, serum_cols_to_extract]
|
||||
serum_df = my_data[, serum_cols_to_extract]
|
||||
}else{
|
||||
cat("FAIL: length mismatch"
|
||||
, "Expeceted to extract:", length(meta_data_cols) + length(serum_cols_clean), "columns"
|
||||
|
@ -101,10 +108,10 @@ colnames_serum_df = colnames(serum_df); colnames_serum_df
|
|||
# npa
|
||||
#=========
|
||||
npa_regex = regex(".*_npa[1-3]{1}$", ignore_case = T)
|
||||
npa_cols_i = str_extract(colnames(adult_df), npa_regex) # not boolean
|
||||
#npa_cols_b = colnames(adult_df)%in%npa_cols_i # boolean
|
||||
npa_cols_i = str_extract(colnames(my_data), npa_regex) # not boolean
|
||||
#npa_cols_b = colnames(my_data)%in%npa_cols_i # boolean
|
||||
|
||||
npa_cols = colnames(adult_df)[colnames(adult_df)%in%npa_cols_i]
|
||||
npa_cols = colnames(my_data)[colnames(my_data)%in%npa_cols_i]
|
||||
|
||||
# this contains log columns + daynpap_npaXX: omitting these
|
||||
npa_regex_log_days = regex("log|day|vl_samptime|ct.*_npa[1-3]{1}$", ignore_case = T, perl = T)
|
||||
|
@ -128,7 +135,7 @@ cat("Extracting NPA cols + metadata_cols")
|
|||
|
||||
if ( length(npa_cols_to_extract) == length(meta_data_cols) + length(npa_cols_clean) ){
|
||||
cat("Extracing", length(npa_cols_to_extract), "columns for npa")
|
||||
npa_df = adult_df[, npa_cols_to_extract]
|
||||
npa_df = my_data[, npa_cols_to_extract]
|
||||
}else{
|
||||
cat("FAIL: length mismatch"
|
||||
, "Expeceted to extract:", length(meta_data_cols) + length(npa_cols_clean), "columns"
|
||||
|
@ -166,21 +173,21 @@ for (i in extra_cols){
|
|||
}
|
||||
}
|
||||
tail(colnames_check_f)
|
||||
# write file?
|
||||
quick_check = as.data.frame(cbind(metadata_all$mosaic
|
||||
, metadata_all$adult
|
||||
, metadata_all$age
|
||||
, metadata_all$obesity
|
||||
, metadata_all$obese2
|
||||
))
|
||||
colnames(quick_check) = c("mosaic", "adult", "age", "obesity", "obese2")
|
||||
|
||||
##########################################################################
|
||||
# LF data
|
||||
# LF data
|
||||
##########################################################################
|
||||
cols_to_omit = c("adult", "obese2"
|
||||
, "height", "height_unit", "weight"
|
||||
, "weight_unit", "visual_est_bmi", "bmi_rating")
|
||||
cols_to_omit = c("adult"
|
||||
#, "obese2"
|
||||
#, "height", "height_unit", "weight"
|
||||
#, "weight_unit", "visual_est_bmi", "bmi_rating"
|
||||
)
|
||||
|
||||
pivot_cols = meta_data_cols
|
||||
# subselect pivot_cols
|
||||
pivot_cols = meta_data_cols[!meta_data_cols%in%cols_to_omit];pivot_cols
|
||||
ncols_omitted = table(meta_data_cols%in%cols_to_omit)[[2]]
|
||||
ncols_omitted
|
||||
|
||||
#==============
|
||||
# lf data: sam
|
||||
|
@ -198,11 +205,11 @@ pivot_cols = meta_data_cols
|
|||
# subselect pivot_cols
|
||||
pivot_cols = meta_data_cols[!meta_data_cols%in%cols_to_omit];pivot_cols
|
||||
|
||||
if (length(pivot_cols) == length(meta_data_cols) - length(cols_to_omit)){
|
||||
if (length(pivot_cols) == length(meta_data_cols) - ncols_omitted){
|
||||
cat("PASS: pivot cols successfully extracted")
|
||||
}else{
|
||||
cat("FAIL: length mismatch! pivot cols could not be extracted"
|
||||
, "\nExpected length:", length(meta_data_cols) - length(cols_to_omit)
|
||||
, "\nExpected length:", length(meta_data_cols) - ncols_omitted
|
||||
, "\nGot:",length(pivot_cols) )
|
||||
quit()
|
||||
}
|
||||
|
@ -249,11 +256,11 @@ serum_wf = serum_df_adults[wf_cols]
|
|||
pivot_cols = meta_data_cols
|
||||
pivot_cols = meta_data_cols[!meta_data_cols%in%cols_to_omit];pivot_cols
|
||||
|
||||
if (length(pivot_cols) == length(meta_data_cols) - length(cols_to_omit)){
|
||||
if (length(pivot_cols) == length(meta_data_cols) - ncols_omitted){
|
||||
cat("PASS: pivot cols successfully extracted")
|
||||
}else{
|
||||
cat("FAIL: length mismatch! pivot cols could not be extracted"
|
||||
, "\nExpected length:", length(meta_data_cols) - length(cols_to_omit)
|
||||
, "\nExpected length:", length(meta_data_cols) - ncols_omitted
|
||||
, "\nGot:",length(pivot_cols) )
|
||||
quit()
|
||||
}
|
||||
|
@ -296,11 +303,11 @@ npa_wf = npa_df_adults[wf_cols]
|
|||
pivot_cols = meta_data_cols
|
||||
pivot_cols = meta_data_cols[!meta_data_cols%in%cols_to_omit];pivot_cols
|
||||
|
||||
if (length(pivot_cols) == length(meta_data_cols) - length(cols_to_omit)){
|
||||
if (length(pivot_cols) == length(meta_data_cols) - ncols_omitted){
|
||||
cat("PASS: pivot cols successfully extracted")
|
||||
}else{
|
||||
cat("FAIL: length mismatch! pivot cols could not be extracted"
|
||||
, "\nExpected length:", length(meta_data_cols) - length(cols_to_omit)
|
||||
, "\nExpected length:", length(meta_data_cols) - ncols_omitted
|
||||
, "\nGot:",length(pivot_cols) )
|
||||
quit()
|
||||
}
|
||||
|
@ -333,7 +340,7 @@ if (
|
|||
rm(sam_regex, sam_regex_log_days, sam_cols, sam_cols_clean, sam_cols_i, sam_cols_to_extract, sam_cols_to_omit)
|
||||
rm(serum_regex, serum_regex_log_days, serum_cols, serum_cols_clean, serum_cols_i, serum_cols_to_extract, serum_cols_to_omit)
|
||||
rm(npa_regex, npa_regex_log_days, npa_cols, npa_cols_clean, npa_cols_i, npa_cols_to_extract, npa_cols_to_omit)
|
||||
rm(adult_df)
|
||||
rm(my_data)
|
||||
rm(colnames_check)
|
||||
rm(i, j
|
||||
#, expected_cols
|
||||
|
@ -344,3 +351,4 @@ rm(sam_df_adults, serum_df_adults, npa_df_adults)
|
|||
|
||||
# rm df
|
||||
rm(sam_df, serum_df, npa_df)
|
||||
rm(colnames_check_f, fp_adults)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue