Skip to content

Commit

Permalink
Internal improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Nov 10, 2024
1 parent b2d5f2b commit a745317
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
6 changes: 3 additions & 3 deletions R/add_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ add_group_id.data.frame <- function(data, ...,
}
}
col_to_add <- add_names(list(ids), .name)
df_modify_cols(data, col_to_add)
df_add_cols(data, col_to_add)
}
#' @rdname add_id
#' @export
Expand Down Expand Up @@ -163,7 +163,7 @@ add_row_id.data.frame <- function(data, ...,
}
}
col_to_add <- add_names(list(row_ids), .name)
df_modify_cols(data, col_to_add)
df_add_cols(data, col_to_add)
}
#' @rdname add_id
#' @export
Expand Down Expand Up @@ -203,5 +203,5 @@ add_consecutive_id.data.frame <- function(data, ...,
.name <- unique_col_name(names(data), "consecutive_id")
}
col_to_add <- add_names(list(ids), .name)
df_modify_cols(data, col_to_add)
df_add_cols(data, col_to_add)
}
37 changes: 24 additions & 13 deletions R/df_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,32 @@ df_row_slice <- function(data, i, reconstruct = TRUE){
out
}
df_add_cols <- function(data, cols){
dplyr::dplyr_col_modify(data, cols)
reconstruct(data, dplyr::dplyr_col_modify(f_ungroup(data), cols))
}

df_modify_cols <- function(data, cols){
out <- unclass(data)
if (!(is.list(cols) && !is.null(names(cols)))){
stop("cols must be a named list")
}
temp <- unclass(cols)
for (col in names(temp)){
out[[col]] <- temp[[col]]
}
class(out) <- class(data)
reconstruct(data, out)
}
# df_modify_cols <- function(data, cols){
# if (!(is.list(cols) && !is.null(names(cols)))){
# stop("cols must be a named list")
# }
# N <- df_nrow(data)
# out <- unclass(data)
# temp <- unclass(cols)
# # temp <- do.call(function(...) cheapr::recycle(..., length = N), cols)
# for (col in names(temp)){
# # out[[col]] <- temp[[col]]
# vec <- temp[[col]]
# if (NROW(vec) != N){
# if (is_df(vec)){
# vec <- cheapr::sset(vec, rep_len(attr(x, "row.names"), N))
# } else {
# vec <- rep_len(vec, N)
# }
# }
# out[[col]] <- vec
# }
# class(out) <- class(data)
# reconstruct(data, out)
# }

# df_add_cols2 <- function(data, cols, check = TRUE){
# if (check){
Expand Down
2 changes: 1 addition & 1 deletion R/f_fill.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ f_fill <- function(data, ..., .by = NULL, .cols = NULL,
}

data_to_fill <- cpp_fill_grouped(data_to_fill, o, sizes, .fill_limit)
df_modify_cols(data, data_to_fill)
df_add_cols(data, data_to_fill)
}

0 comments on commit a745317

Please sign in to comment.