Repository navigation
Expand file tree
/
Copy pathcalc_composite_attributes.R
More file actions
60 lines (45 loc) · 1.71 KB
/
Copy pathcalc_composite_attributes.R
File metadata and controls
60 lines (45 loc) · 1.71 KB
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
library(magrittr)
args = commandArgs(trailingOnly = TRUE)
if (length(args) != 1) {
stop("the calc_composite_attributes.R script requires exactly one argument: name of run config file ... thank you. goodbye.")
}
source(args[1])
run_name = gsub(".R", "", args[1])
entities = read.csv(file.path(data_base_dir,
"flat.csv"),
stringsAsFactors = FALSE)
print(head(entities))
if (!exists("composite_attributes")) {
file.copy(file.path(data_base_dir,
"flat.csv"),
file.path(data_base_dir,
"flat_w_composites.csv"),
overwrite = TRUE)
} else {
# composite attributes
for(j in 1:length(composite_attributes)) {
attribute_name = gsub("=.*", "", composite_attributes[j]) %>%
gsub(" ","", .)
print(attribute_name)
attribute_expression = gsub(".*=", "", composite_attributes[j]) %>%
gsub(" ","", .)
print("attribute expression is")
print(attribute_expression)
print("names entities is")
print(names(entities))
entities[,attribute_name] = parse(text = attribute_expression) %>%
eval(envir = entities)
}
# commenting out this column sorting now -- better if columns are already provided in desired order in flat.csv
#entities = entities[,sort(names(entities))]
#entity_label = entities$entity_label
#entities$entity_label = NULL
#entity_group = entities[,group_var]
#entities[,group_var] = NULL
#entities %<>% cbind(entity_label, entity_group, .)
#names(entities)[2] = group_var
write.csv(entities,
file.path(data_base_dir,
"flat_w_composites.csv"),
row.names = FALSE)
}