计算每组不同值的观察结果,并为每个值添加新的计数列

时间:2017-03-28 14:21:51

标签: r data.table

与此问题类似Count number of observations/rows per group and add result to data frame但不完全相同。

我想改变这个

/usr/bin/curl  \
--header "Content-type: application/json" \
--request POST \
--data '{"u_id": "$NOTIFY_SERVICEPROBLEMID", "u_date_time" : "$NOTIFY_SHORTDATETIME", \
"u_state" : "$NOTIFY_SERVICESHORTSTATE", "u_host" : "$NOTIFY_HOSTNAME", "u_address" : "$NOTIFY_HOSTADDRESS", \
"u_description" : "$NOTIFY_HOSTALIAS/$NOTIFY_SERVICEDESC is $NOTIFY_SERVICESTATE"}' \
https://my-rest_api

进入这个

   group id_in_group letter
1:     A          A1   alef
2:     A          A2    bet
3:     A          A3    bet
4:     B          B1   alef
5:     B          B2   alef
6:     B          B3  gimel

3 个答案:

答案 0 :(得分:4)

或者没有任何额外的库,您只需使用表:

table(df$group,df$letter)

由于您似乎使用data.table,您还可以使用dcast()

dcast(df, group~letter,length)

答案 1 :(得分:3)

我使用dplyrtidyr执行的典型任务。

library(dplyr)
library(tidyr)
df <- data.frame(group = c("A", "A", "A", "B", "B", "B"),
             id_in_group = c("A1", "A2", "A3", "B1", "B2", "B3"),
             letter = c("alef", "bet", "bet", "alef", "alef", "gimel"))

df %>% 
  group_by(group, letter) %>% 
  summarise(n = n()) %>% 
  spread(letter, n, fill = 0)

答案 2 :(得分:-1)

使用data.table

library(data.table)

dt <- data.table(group=c(rep('A',3),rep('B',3)),
           id_in_group=c(paste0('A',seq(1,3,1)),paste0('B',seq(1,3,1))),
           letter=c('alef','bet','bet','alef','alef','gimel'))

dt[,aleph:=ifelse(like(letter,"a"),1,0)][
  ,bet:=ifelse(like(letter,"bet"),1,0)][
  ,gimal:=ifelse(like(letter,"gimel"),1,0)]
dt[,':='(id_in_group=NULL,letter=NULL)][,lapply(.SD,sum),by=group]