我已经可以在R和Tableau Prep中进行此操作,但是我希望所有空白单元格都在每个类别的末尾。不在顶部。 Excel VBA可能是我最好的选择。但是我对VBA并不熟悉。
我在R中获得宽表的代码: data_wide <-传播(数据集,部门,“描述(唯一)”)
但是,我最后没有空白单元格。
这里是我的R数据集:
structure(list(Number = c(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),
Theme = c("Summer", "Summer", "Summer", "Summer", "Summer",
"Summer", "Summer", "Summer", "Summer", "Summer", "Summer",
"Summer", "Summer", "Summer", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Fall", "Fall", "Fall", "Fall",
"Fall", "Fall"), Description = c("A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
"R", "S", "T", "U", "V", "W", "X", "Y", "Z"), Sector = c("APPLE",
"APPLE", "APPLE", "ORANGE", "ORANGE", "ORANGE", "ORANGE",
"ORANGE", "BANANA", "BANANA", "CHERRY", "CHERRY", "CHERRY",
"APPLE", "APPLE", "ORANGE", "BANANA", "CHERRY", "CHERRY",
"APPLE", "APPLE", "ORANGE", "ORANGE", "ORANGE", "CHERRY",
"CHERRY")), row.names = c(NA, -26L), class = c("tbl_df",
"tbl", "data.frame"))
答案 0 :(得分:2)
要使spread
工作,您需要进行一些预处理。为每个Sector
创建一个行号变量,删除Number
列,然后删除spread
。
library(dplyr)
df %>%
group_by(Sector) %>%
mutate(row = row_number()) %>%
select(-Number) %>%
tidyr::spread(Sector, Description) %>%
select(-row)
# A tibble: 13 x 5
# Theme APPLE BANANA CHERRY ORANGE
# <chr> <chr> <chr> <chr> <chr>
# 1 Fall NA NA Y NA
# 2 Fall U NA Z V
# 3 Fall NA NA NA W
# 4 Fall NA NA NA X
# 5 Summer A I K D
# 6 Summer B J L E
# 7 Summer C NA M F
# 8 Summer N NA NA G
# 9 Summer NA NA NA H
#10 Winter NA Q NA NA
#11 Winter NA NA R NA
#12 Winter O NA S NA
#13 Winter T NA NA P
或者更短一点的是将Number
列本身更改为行索引
df %>%
group_by(Sector) %>%
mutate(Number = row_number()) %>%
tidyr::spread(Sector, Description)