Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Commit

Permalink
Added bindRows() alias that accepts data frames as varargs (relates…
Browse files Browse the repository at this point in the history
… to #51)
  • Loading branch information
Holger Brandl committed Apr 17, 2018
1 parent 89be260 commit 9c1878d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/main/kotlin/krangl/Extensions.kt
Expand Up @@ -612,19 +612,24 @@ fun DataFrame.printDataClassSchema(
}


/** Concatenate a list of data-frame by row. */
fun List<DataFrame>.bindRows(): DataFrame { // add options about NA-fill over non-overlapping columns
return bindRows(*this.toTypedArray())
}


/** Concatenate a list of data-frame by row. */
fun bindRows(vararg dataFrames: DataFrame): DataFrame { // add options about NA-fill over non-overlapping columns
// todo more column model consistency checks here
// note: use fold to bind with non-overlapping column model

val bindCols = mutableListOf<DataCol>()

// val totalRows = map { it.nrow }.sum()

for (colName in this.firstOrNull()?.names ?: emptyList()) {
val colDataCombined: Array<*> = bindColData(colName)
for (colName in dataFrames.firstOrNull()?.names ?: emptyList()) {
val colDataCombined: Array<*> = bindColData(dataFrames.toList(), colName)

when (this.first()[colName]) {
when (dataFrames.first()[colName]) {
is DoubleCol -> DoubleCol(colName, colDataCombined.map { it as Double? })
is IntCol -> IntCol(colName, colDataCombined.map { it as Int? })
is StringCol -> StringCol(colName, colDataCombined.map { it as String? })
Expand All @@ -642,14 +647,14 @@ fun bindCols(left: DataFrame, right: DataFrame): DataFrame { // add options abou
return SimpleDataFrame(left.cols.toMutableList().apply { addAll((right as SimpleDataFrame).cols) })
}

private fun List<DataFrame>.bindColData(colName: String): Array<*> {
val totalRows = map { it.nrow }.sum()
private fun bindColData(dataFrames: List<DataFrame>, colName: String): Array<*> {
val totalRows = dataFrames.map { it.nrow }.sum()

val arrayList = Array<Any?>(totalRows, { 0 })

var iter = 0

forEach {
dataFrames.forEach {
it[colName].values().forEach {
arrayList[iter++] = it
}
Expand Down

0 comments on commit 9c1878d

Please sign in to comment.