![](/img/trans.png)
[英]How to calculate percentage of variable that meets certain criteria using group_by and summarise
[英]How to multiplying a string variable n-times by the value of another variable, inside a summarise group_by function?
我對dplyr軟件包的group_by內部的summary函數有問題。
數據框如下所示:
df <- read.table(text="id groups times strings
1 1 a 1 xxx
2 2 a 2 zzz
3 3 a 1 yyy
4 4 b 1 xxx
5 5 b 1 yyy
6 6 b 1 xxx
7 7 c 3 yyy
8 8 c 1 xxx
9 9 c 1 xxx
10 10 c 1 yyy", stringsAsFactors=FALSE)
我需要對變量“ groups”進行group_by以獲得如下所示的數據框:
view(group.df)
id2 groups2 c.string
1 1 a 'xxx','zzz','zzz','yyy'
2 2 b 'xxx','yyy','xxx'
3 3 c 'yyy','yyy','yyy','xxx','xxx','yyy'
str(group.df)
'data.frame': 3 obs. of 3 variables:
$ id2 : int 1 2 3
$ groups2 : chr "a" "b" "c"
$ c.string: chr "'xxx','zzz','zzz','yyy'" "'xxx','yyy','xxx'"
"'yyy','yyy','yyy','xxx','xxx','yyy'"
我的問題是,僅當“ df $ times”大於1時,我不知道如何編寫嵌套在group_by中的summary函數以將字符串變量“ df $ strings”乘以“ df $ times”的值。
謝謝您的幫助。
似乎您只是忽略id列,所以這應該可以工作
df %>%
group_by(groups) %>%
summarize(c.string=paste(sQuote(rep(strings, times)), collapse=","))
# groups c.string
# <chr> <chr>
# 1 a ‘xxx’,‘zzz’,‘zzz’,‘yyy’
# 2 b ‘xxx’,‘yyy’,‘xxx’
# 3 c ‘yyy’,‘yyy’,‘yyy’,‘xxx’,‘xxx’,‘yyy’
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.