cut.columns: Exclude unnecessary columns from exports

Description Usage Arguments Value Author(s) See Also Examples

View source: R/cut_columns.R

Description

This function cuts off columns, which are not needed for further data analysis. It also checks, whether all columns are present in the dataframe under processing. The function is able to process German and English names of columns. Exact list of correct names should be provided in the configuration file.

Usage

1
cut.columns(x, config)

Arguments

x

Member of R class value.class (usually contains a field @table with dataframe)

config

Configuration object with processing rules for different export types

Value

Returns object x of R class class(x) with cutted columns.

Author(s)

Diana Omelianchyk

See Also

read.file, read.in, preprocess, {append.tables

Examples

 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
##-- General function cut.columns --

cut.columns <- function(x, config, value.class) {
  x <- as.data.frame(x)
  
  if (dim(x)[1] > 0) {
    if (sum(!(is.na(x[,1]))) > 0) {
      config.columns <-
        grep("columns", names(config[[value.class]]), value = TRUE)
      if (length(config.columns) == 1) {
        columns.en <- config[[value.class]][[config.columns]]
        columns.de <- config[[value.class]][[config.columns]]
      } else if (length(config.columns) == 2) {
        columns.en <- config[[value.class]][[config.columns[1]]]
        columns.de <- config[[value.class]][[config.columns[2]]]
      } else {
        stop("As for now, script can only handle english or german names!")
      }
      
      if (check.names(x, columns.en)) {
        x <- x[, columns.en]
      } else if (check.names(x, columns.de)) {
        x <- x[, columns.de]
      } else {
        stop(
          paste('Columns', setdiff(columns.en, names(x)), 'from configuration file are not present in data. Fix this and return.')
        )
      }
    } else {
      x <- data.frame()
    }
  }
  new(value.class, table = x)
}

##-- Subfunction check.names --

check.names <- function(x, columns) {
  x <- as.data.frame(x)
  is.correct <- TRUE
  for (column in columns)
  {
    if (!(column 
      is.correct <- FALSE
      break
    }
  }
  is.correct

omelyanchikd/merger documentation built on July 13, 2017, 6:24 p.m.