Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(magrittr)
- library(dplyr)
- DT <- fread('
- ID 性別 年齡 號碼
- 1 F 23 2
- 1 F 30 2
- 1 M 32 2
- 2 M 32 1
- 2 F 23 1
- 3 M 56 1
- 3 F 23 1
- 3 M 18 1
- 4 M 12 4
- 4 F 32 4
- 4 M 65 4
- 4 F 45 4
- 4 M 42 4
- ') %>% tbl_dt(FALSE)
- select_rows_f <- function(scheme, DT){
- switch(scheme,
- A = {
- outDT = DT %>% arrange(ID, 年齡) %>%
- mutate(selectRowID = round(號碼*(0.5+1e-6))) %>%
- # +1e-6是因為 round(0.5)是0,要比0.5大一點點才會進位
- group_by(ID) %>% summarise(性別 = 性別[unique(selectRowID)],
- 年齡 = 年齡[unique(selectRowID)], 號碼 = 號碼[unique(selectRowID)])
- ## use summarise_each 另外兩個例子,可以自己改XD
- # outDT = DT %>% arrange(ID, 年齡) %>%
- # group_by(ID) %>% summarise_each(funs(.[unique(round(號碼*(0.5+1e-6)))]))
- },
- B = {
- outDT = DT %>% arrange(ID, desc(年齡)) %>%
- mutate(selectRowID = round(號碼*0.9)) %>%
- group_by(ID) %>% summarise(性別 = 性別[unique(selectRowID)],
- 年齡 = 年齡[unique(selectRowID)], 號碼 = 號碼[unique(selectRowID)])
- },
- C = {
- outDT = DT %>% arrange(ID, desc(年齡)) %>%
- mutate(selectRowID = 號碼) %>%
- group_by(ID) %>% summarise(性別 = 性別[unique(selectRowID)],
- 年齡 = 年齡[unique(selectRowID)], 號碼 = 號碼[unique(selectRowID)])
- })
- return(outDT)
- }
- select_rows_f('A', DT)
- # Source: local data table [4 x 4]
- #
- # ID 性別 年齡 號碼
- # (int) (chr) (int) (int)
- # 1 1 F 23 2
- # 2 2 F 23 1
- # 3 3 M 18 1
- # 4 4 F 32 4
- select_rows_f('B', DT)
- # Source: local data table [4 x 4]
- #
- # ID 性別 年齡 號碼
- # (int) (chr) (int) (int)
- # 1 1 F 30 2
- # 2 2 M 32 1
- # 3 3 M 56 1
- # 4 4 F 32 4
- select_rows_f('C', DT)
- # Source: local data table [4 x 4]
- #
- # ID 性別 年齡 號碼
- # (int) (chr) (int) (int)
- # 1 1 F 30 2
- # 2 2 M 32 1
- # 3 3 M 56 1
- # 4 4 F 32 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement