Различия
Здесь показаны различия между двумя версиями данной страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
utilities [2015/09/04 11:06] 127.0.0.1 external edit |
utilities [2020/02/19 14:28] (текущий) f0ma |
||
---|---|---|---|
Строка 5: | Строка 5: | ||
rowApply <- function(fun,tbl,vec) data.frame(mapply(fun, tbl, vec, SIMPLIFY = F)); | rowApply <- function(fun,tbl,vec) data.frame(mapply(fun, tbl, vec, SIMPLIFY = F)); | ||
- | selectNames <- function(tbl,namevec) { | + | selectNames <- function(tbl, namevec, saveNamesColumn = TRUE) { |
rtbl <- tbl; | rtbl <- tbl; | ||
for ( n in namevec ) if (!n %in% names(rtbl)) rtbl[[n]] <- 0.0; | for ( n in namevec ) if (!n %in% names(rtbl)) rtbl[[n]] <- 0.0; | ||
for ( n in names(rtbl) ) if (!n %in% namevec) rtbl[[n]] <- NULL; | for ( n in names(rtbl) ) if (!n %in% namevec) rtbl[[n]] <- NULL; | ||
- | return(rtbl[,namevec]); | + | if ((!saveNamesColumn) || ('Name' %in% namevec) || (!'Name' %in% names(rtbl))){ |
+ | return(rtbl[,namevec]); | ||
+ | } else { | ||
+ | rtbl <- rtbl[,namevec]; | ||
+ | rtbl <- cbind(data.frame(Name = rtbl$Name), rtbl); | ||
+ | return(rtbl); | ||
+ | } | ||
} | } | ||
+ | occupation_r <- function(row, target_col, target_max, source_cols) | ||
+ | { | ||
+ | for (c in source_cols) | ||
+ | { | ||
+ | if(as.numeric(row[[target_col]]) >= as.numeric(target_max)) break; | ||
+ | | ||
+ | row[[target_col]] = as.numeric(row[[target_col]]) + as.numeric(row[[c]]); | ||
+ | | ||
+ | if(as.numeric(row[[target_col]]) >= as.numeric(target_max)) | ||
+ | { | ||
+ | row[[c]] = as.numeric(row[[target_col]]) - as.numeric(target_max); | ||
+ | row[[target_col]] = as.numeric(target_max); | ||
+ | break; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | row[[c]] = as.numeric(0.0); | ||
+ | } | ||
+ | } | ||
+ | return (row) | ||
+ | } | ||
+ | |||
+ | occupation <- function(data, target_col, target_max, source_cols) | ||
+ | { | ||
+ | v = apply(data, 1, occupation_r, target_col=target_col, target_max=target_max, source_cols=source_cols); | ||
+ | return(data.frame(t(v))); | ||
+ | } | ||
</code> | </code> |