Mercurial > repos > prog > lcmsmatching
comparison todf.R @ 0:3afe41d3e9e7 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit bb4d3e23d99828bfee16d31d794c49a17313ec2f
| author | prog |
|---|---|
| date | Mon, 11 Jul 2016 09:12:03 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3afe41d3e9e7 |
|---|---|
| 1 source('tolst.R') | |
| 2 | |
| 3 # Convert a list of key/value lists or a list of objects into a data frame. Each key becomes a column. | |
| 4 # x The object to convert to data frame. Either a list of key/value lists, or a list of objects. | |
| 5 # rm_na_col If true, remove all columns that contain only NA values. | |
| 6 todf <- function(x, rm_na_col = FALSE) { | |
| 7 | |
| 8 df <- data.frame() | |
| 9 | |
| 10 # x not null ? | |
| 11 if ( ! is.null(x) && length(x) > 0) { | |
| 12 | |
| 13 # fill data frame | |
| 14 for (i in 1:length(x)) { | |
| 15 lst <- if (typeof(x[[i]]) == 'S4') tolst(x[[i]]) else x[[i]] | |
| 16 for (k in names(lst)) { | |
| 17 v <- x[[i]][[k]] | |
| 18 df[i , k] <- if (length(v) > 1) paste0(v, collapse = ';') else v | |
| 19 } | |
| 20 } | |
| 21 | |
| 22 # remove NA columns | |
| 23 if (rm_na_col) { | |
| 24 drop <- character() | |
| 25 for (col in names(df)) | |
| 26 if (all(is.na(df[[col]]))) | |
| 27 drop <- c(drop, col) | |
| 28 if (length(drop) > 0) | |
| 29 df <- df[, !(names(df) %in% drop)] | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 return(df) | |
| 34 } |
