如何根据条件向 table 添加行?
How to add rows to a table based on a condition?
我想创建一个 table,我可以在其中为特定用户授予访问报告特定页面的权限
想象一下我有一个像这样的 table:
我有另一个 table,其中我有每个报告页面的名称:
我想要一个 table,其中我有用户和他们有权访问的所有页面,具体取决于他们的组。第1组可以看到所有页面,但是第2组只能看到团队页面:
最好的选择是在 DAX 代码中完成,但我认为使用 R 可能更容易。在此先感谢!
tidyverse
包为您提供了操作数据帧的简单工具。您可以创建 3 个变量(订单、销售、团队)记录每个页面的访问权限(例如 1 或 0)使用 case_when
和组条件,然后 pivot_longer
这些变量,最后只保留具有 filter
.
访问权限的行
library(tidyverse)
Group <- c(1,2,1,2,2)
User <- c("alex","pablo","carlos","pepe","paula") %>% paste0("@gmail.com")
df <- data.frame(Group, User)
df2 <- df %>%
mutate(Orders = case_when(Group==1 ~ 1,
Group==2 ~ 0),
Sales = Orders,
Team = 1) %>%
pivot_longer(cols = c(Orders, Sales, Team), names_to = "Page") %>%
filter(value == 1) %>%
select(-value)
输出
> df
Group User
1 1 alex@gmail.com
2 2 pablo@gmail.com
3 1 carlos@gmail.com
4 2 pepe@gmail.com
5 2 paula@gmail.com
> df2
# A tibble: 9 x 3
Group User Page
<dbl> <chr> <chr>
1 1 alex@gmail.com Orders
2 1 alex@gmail.com Sales
3 1 alex@gmail.com Team
4 2 pablo@gmail.com Team
5 1 carlos@gmail.com Orders
6 1 carlos@gmail.com Sales
7 1 carlos@gmail.com Team
8 2 pepe@gmail.com Team
9 2 paula@gmail.com Team
一个想法即可,
library(dplyr)
library(tidyr)
df %>%
mutate(page = toString(df1$page_name)) %>%
separate_rows(page, sep = ', ') %>%
mutate(page = replace(page, Group == 2 & page != 'team', NA)) %>%
na.omit()
# A tibble: 9 x 3
Group User page
<dbl> <chr> <chr>
1 1 A orders
2 1 A sales
3 1 A team
4 2 B team
5 1 C orders
6 1 C sales
7 1 C team
8 2 D team
9 2 E team
数据
dput(df)
structure(list(Group = c(1, 2, 1, 2, 2), User = c("A", "B", "C",
"D", "E")), class = "data.frame", row.names = c(NA, -5L))
dput(df1)
structure(list(page_name = c("orders", "sales", "team")), class = "data.frame", row.names = c(NA,
-3L))
使用 fuzzyjoin
包的另一个想法:
数据
users <- data.frame(
Group = c("1","2","1","2","2"),
User = c("alex","pablo","carlos","pepe","paula")
)
Group User
1 1 alex
2 2 pablo
3 1 carlos
4 2 pepe
5 2 paula
然后您可以向页面数据框添加一列,告诉允许访问每个类别的组:
pagename <- data.frame(
Page = c("Order","Sales","Team"),
Allowed = c("1","1","1|2")
)
Page Allowed
1 Order 1
2 Sales 1
3 Team 1|2
最后使用 fuzzyjoin::regex_left_join
:
users |>
fuzzyjoin::regex_left_join(pagename,
by = c(Group = "Allowed")) |>
dplyr::select(-Allowed)
输出
Group User Page
1 1 alex Order
2 1 alex Sales
3 1 alex Team
4 2 pablo Team
5 1 carlos Order
6 1 carlos Sales
7 1 carlos Team
8 2 pepe Team
9 2 paula Team
我想创建一个 table,我可以在其中为特定用户授予访问报告特定页面的权限
想象一下我有一个像这样的 table:
我有另一个 table,其中我有每个报告页面的名称:
我想要一个 table,其中我有用户和他们有权访问的所有页面,具体取决于他们的组。第1组可以看到所有页面,但是第2组只能看到团队页面:
最好的选择是在 DAX 代码中完成,但我认为使用 R 可能更容易。在此先感谢!
tidyverse
包为您提供了操作数据帧的简单工具。您可以创建 3 个变量(订单、销售、团队)记录每个页面的访问权限(例如 1 或 0)使用 case_when
和组条件,然后 pivot_longer
这些变量,最后只保留具有 filter
.
library(tidyverse)
Group <- c(1,2,1,2,2)
User <- c("alex","pablo","carlos","pepe","paula") %>% paste0("@gmail.com")
df <- data.frame(Group, User)
df2 <- df %>%
mutate(Orders = case_when(Group==1 ~ 1,
Group==2 ~ 0),
Sales = Orders,
Team = 1) %>%
pivot_longer(cols = c(Orders, Sales, Team), names_to = "Page") %>%
filter(value == 1) %>%
select(-value)
输出
> df
Group User
1 1 alex@gmail.com
2 2 pablo@gmail.com
3 1 carlos@gmail.com
4 2 pepe@gmail.com
5 2 paula@gmail.com
> df2
# A tibble: 9 x 3
Group User Page
<dbl> <chr> <chr>
1 1 alex@gmail.com Orders
2 1 alex@gmail.com Sales
3 1 alex@gmail.com Team
4 2 pablo@gmail.com Team
5 1 carlos@gmail.com Orders
6 1 carlos@gmail.com Sales
7 1 carlos@gmail.com Team
8 2 pepe@gmail.com Team
9 2 paula@gmail.com Team
一个想法即可,
library(dplyr)
library(tidyr)
df %>%
mutate(page = toString(df1$page_name)) %>%
separate_rows(page, sep = ', ') %>%
mutate(page = replace(page, Group == 2 & page != 'team', NA)) %>%
na.omit()
# A tibble: 9 x 3
Group User page
<dbl> <chr> <chr>
1 1 A orders
2 1 A sales
3 1 A team
4 2 B team
5 1 C orders
6 1 C sales
7 1 C team
8 2 D team
9 2 E team
数据
dput(df)
structure(list(Group = c(1, 2, 1, 2, 2), User = c("A", "B", "C",
"D", "E")), class = "data.frame", row.names = c(NA, -5L))
dput(df1)
structure(list(page_name = c("orders", "sales", "team")), class = "data.frame", row.names = c(NA,
-3L))
使用 fuzzyjoin
包的另一个想法:
数据
users <- data.frame(
Group = c("1","2","1","2","2"),
User = c("alex","pablo","carlos","pepe","paula")
)
Group User
1 1 alex
2 2 pablo
3 1 carlos
4 2 pepe
5 2 paula
然后您可以向页面数据框添加一列,告诉允许访问每个类别的组:
pagename <- data.frame(
Page = c("Order","Sales","Team"),
Allowed = c("1","1","1|2")
)
Page Allowed
1 Order 1
2 Sales 1
3 Team 1|2
最后使用 fuzzyjoin::regex_left_join
:
users |>
fuzzyjoin::regex_left_join(pagename,
by = c(Group = "Allowed")) |>
dplyr::select(-Allowed)
输出
Group User Page
1 1 alex Order
2 1 alex Sales
3 1 alex Team
4 2 pablo Team
5 1 carlos Order
6 1 carlos Sales
7 1 carlos Team
8 2 pepe Team
9 2 paula Team