作者celestialgod (天)
看板R_Language
標題Re: [問題] 指定group
時間Sun Aug 16 08:51:27 2015
我不知道這樣有沒有快多少... 參考看看
library(data.table) # setnames
library(plyr) # dlply, laply, mapvalues
library(dplyr) # mutate, summarise, group_by
library(magrittr) # %>%, %$%
dt <- data.table(
series = c(1,2,3,4,5,6,1,1,2,2,2,2,2),
cate = c(2,3,4,2,2,5,1,2,2,3,4,4,4)
)
## method 1 (應該會較慢,而且程式比較醜)
maptable = dlply(dt, .(series), table) %>% laply(function(x){
as.integer(colnames(x)[which.max(x)])
}) %>% cbind(as.integer(names(.)), .)
dt2 = dt %>% mutate(cate = mapvalues(series, maptable[,1], maptable[,2]))
## method 2
-maptable = dt %>% group_by(series, cate) %>% summarise(n()) %>%
group_by(series) %>% summarise(maxCate = max(cate))
dt2 = dt %>% mutate(cate = mapvalues(series, maptable$series,
maptable$maxCate))
## 把上面連在一起的版本 (沒有maptable這個暫存變數)
dt2 = dt %>% group_by(series, cate) %>% summarise(n()) %>%
group_by(series) %>% summarise(maxCate = max(cate)) %>%
setnames("series", "maxSeries") %$%
mutate(dt, cate = mapvalues(series, maxSeries, maxCate))
: 例如
: series cate
: [1,] 1 2
: [2,] 2 3
: [3,] 3 4
: [4,] 4 2
: [5,] 5 2
: [6,] 6 5
: [7,] 1 1
: [8,] 1 2
: [9,] 2 2
: [10,] 2 3
: [11,] 2 4
: [12,] 2 4
: [13,] 2 4
: 在series中1出現三次,其cate分別為2,2,1 頻率最高的為2
: 想將所有series為1的族群 其cate欄位接指定為2
: 又例如 series中為2的族群 其cate 分別為 3,2,3,4,4,4 頻率最高的4
: 想將所有series為2的族群 其cate欄位皆指定為4
: 請問除了用for loop外有其他方法嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.27.107
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1439686290.A.B89.html
1F:推 lambking: 謝謝你! 有快很多 08/16 18:17
2F:→ lambking: 另外想請問如果是series中有missing data 08/16 18:18
3F:→ lambking: 能針對missing data 保留其原本cate 值嗎 08/16 18:19
在maptable產生之後加上na.omit,然後加上控制項去控制NA不要覆蓋到原本的cate就好
PS: na.omit是把有包含NA的那一個ROW移除,是base的函數
dt <- data.table(
series = c(1,2,3,4,5,6,1,1,2,2,2,2,2,NA,NA,NA),
cate = c(2,3,4,2,2,5,1,2,2,3,4,4,4,3,3,1)
)
dt2 = dt %>% group_by(series, cate) %>% summarise(n()) %>%
group_by(series) %>% summarise(maxCate = max(cate)) %>%
setnames("series", "maxSeries") %>% na.omit %$%
mutate(dt, cate = ifelse(is.na(series), cate,
mapvalues(series, maxSeries, maxCate)))
# series cate
# 1: 1 2
# 2: 2 4
# 3: 3 4
# 4: 4 2
# 5: 5 2
# 6: 6 5
# 7: 1 2
# 8: 1 2
# 9: 2 4
#10: 2 4
#11: 2 4
#12: 2 4
#13: 2 4
#14: NA 3
#15: NA 3
#16: NA 1
4F:推 lambking: 謝謝! 了解了! 08/16 20:59
5F:→ lambking: 但在實際執行時 會出現以下錯誤訊息 08/16 21:01
6F:→ lambking: Error in n() : This function should not be called 08/16 21:01
7F:→ lambking: directly 08/16 21:03
8F:→ lambking: 查到是因為plyr 和dplyr皆有summarise功能 08/16 21:04
我的版本沒問題
你可以指定dplyr:::summarise
9F:推 lambking: 謝謝, 但改完之後反而出現setnames的問題 08/16 22:42
10F:→ lambking: Error in setnames(., "series", "maxSeries") : 08/16 22:42
11F:→ lambking: Items of 'old' not found in column names: series 08/16 22:42
12F:→ lambking: 請問你用的是哪一個版本呢? 謝謝 08/16 22:43
我都沒有問題...
我用的R: Revolution R Open 3.2.0
我的session information:
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950
[2] LC_CTYPE=Chinese (Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] magrittr_1.5 dplyr_0.4.2 plyr_1.8.3 data.table_1.9.4
loaded via a namespace (and not attached):
[1] lazyeval_0.1.10 R6_2.0.1 assertthat_0.1 parallel_3.2.0
[5] DBI_0.3.1 tools_3.2.0 reshape2_1.4.1 Rcpp_0.12.0
[9] stringi_0.4-1 stringr_1.0.0 chron_2.3-45
13F:推 lambking: 謝謝 我再試試看 08/17 00:14
14F:→ lambking: 另外想請問如果series 和 cate 都是 character 08/17 00:15
這個應該不用改太多,你自己改一下就好了
※ 編輯: celestialgod (123.205.27.107), 08/17/2015 09:23:53