Hey,
This was an earlier query
answered by @martinjhnhadley
But today, the same code is giving an error
**Error: Column a
is of unsupported class data.frame **
df <- data.frame(a = runif(10, 2.0, 7.5),
b = runif(10, 5.0, 9.5),
c = runif(10, 1.0, 9.5),
d = runif(10, 2.0, 9.1),
e = runif(10, 3.0, 9.2),
f = runif(10, 4.0, 9.3),
g = runif(10, 5.0, 9.4),
h = runif(10, 6.0, 9.5),
i = runif(10, 7.0, 9.6),
j = runif(10, 8.0, 9.7),
k = runif(10, 9.0, 9.8),
l = runif(10, 1.0, 9.9))
col_windows_row_sums <- function(data, window_size = 3){
col_indices <- 1:ncol(data)
split(col_indices, ceiling(seq_along(col_indices)/window_size)) %>%
map(~{
data[,.x] %>%
as_tibble() %>%
mutate(row_sum = rowSums(.)) %>%
mutate_all(vars(. / row_sum)) %>%
select(-row_sum)
}) %>%
bind_cols() %>%
set_names(colnames(data))
}
df %>%
col_windows_row_sums()