Mercurial > repos > prog > lcmsmatching
comparison todf.R @ 0:e66bb061af06 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
author | prog |
---|---|
date | Tue, 12 Jul 2016 12:02:37 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e66bb061af06 |
---|---|
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 } |