2

您好,我有以下 data.frame(附加)。我想添加一个带有归一化计数的附加列N = N/sum(N)。我以前的 data.frame 没有日期列,并且能够使用

oo[, N.norm := N/sum(N), by=Operator]

我试图将 Date 添加到 by 函数

oo[, N.norm := N/sum(N), by=Operator,Date]

但收到错误消息

Error in `[.data.frame`(oo, , `:=`(N.norm, N/sum(N)), by = Operator, Date) : 
  unused argument(s) (by = Operator)

例如,对于“2013 年 1 月”月份的操作员“A”,我有N每个ROI_SCore= c("Good","OK","Poor","Crap") 的计数。我想为该组合(A 和 2013 年 1 月)加总 N 并将计数N除以sum(N)

另一方面,任何人都可以向我提供有关在 R 中操作 data.frames 的体面介绍吗

structure(list(Operator = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("A", 
"D", "J", "L", "M"), class = "factor"), ROI_Score = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L), .Label = c("Crap", "Good", "OK", "Poor"), class = "factor"), 
    Date = c("Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013", "Apr 2013", "Feb 2013", "Jan 2013", "Mar 2013", 
    "May 2013"), N = c(0, 0, 0, 0, 0, 1, 2, 15, 1, 5, 3, 2, 3, 
    1, 0, 3, 0, 5, 5, 1, 0, 0, 0, 1, 0, 14, 17, 16, 8, 7, 5, 
    10, 6, 1, 5, 24, 27, 31, 16, 15, 0, 0, 0, 0, 0, 26, 24, 20, 
    11, 18, 3, 4, 17, 3, 2, 20, 36, 12, 21, 9, 0, 0, 0, 0, 0, 
    3, 12, 5, 12, 4, 0, 0, 3, 4, 0, 29, 37, 41, 25, 10, 0, 0, 
    0, 0, 0, 9, 9, 15, 17, 3, 6, 4, 5, 4, 1, 14, 13, 9, 15, 9
    )), .Names = c("Operator", "ROI_Score", "Date", "N"), row.names = c(NA, 
100L), class = "data.frame")

我不确定数据是 data.frame 还是 data.table 格式。这是我的代码,改编自 Arun 给出的解决方案(重塑/重塑数据框以创建标准化条形图和饼图

df <- data.frame(read.csv("/misc/jaguar_data/report/system/db_fs/roi_scores.csv"))
#Get date into nice structure for faceting
df$Date = strftime(strptime(df$Date,f="%d/%m/%Y"), "%b %Y")
dt <- data.table(df)
ops <- as.character(unique(dt$Operator))
scr <- as.character(unique(dt$ROI_Score))
dts <- unique(dt$Date)

oo <- setkey(dt[, .N, by="Operator,ROI_Score,Date"], Operator,
ROI_Score,Date)[CJ(ops, scr,dts)][is.na(N), N:= 0L]

oo[, N.norm := N/sum(N), by=Operator]
4

2 回答 2

4

您的代码(几乎)完美。两个小问题。

1:您正在使用data.table语法,因此应该是 aoo而不是 a 。只需使用:data.framedata.table

 library(data.table)  
 oo <- data.table(oo)

2:当by与多列一起使用时,请确保将列包裹在list(..)或包裹在一个逗号分隔的字符串中。例子

 oo[, N.norm := N/sum(N), by=list(Operator,Date)]

 # - or - #
 oo[, N.norm := N/sum(N), by="Operator,Date"]

编辑:如果您希望除以每个组的每个总数OperatorDate那么您的代码应该如上。相反,如果您想除以整个数据的总数,则使用

 oo[, N.norm := N/sum(DT$N), by=list(Operator,Date)]

修复这两件事并完全按照您的方式使用其他所有内容:

     Operator ROI_Score     Date  N    N.norm
  1:        A      Crap Apr 2013  0 0.0000000
  2:        A      Crap Feb 2013  0 0.0000000
  3:        A      Crap Jan 2013  0 0.0000000
  4:        A      Crap Mar 2013  0 0.0000000
  5:        A      Crap May 2013  0 0.0000000
 ---                                         
 96:        M      Poor Apr 2013 14 0.4827586
 97:        M      Poor Feb 2013 13 0.5000000
 98:        M      Poor Jan 2013  9 0.3103448
 99:        M      Poor Mar 2013 15 0.4166667
100:        M      Poor May 2013  9 0.6923077

编辑2:

只是一个注释。通常,如果您在[括号内使用表达式],尤其是通过引用分配运算符:=,那么您的对象应该是一个data.table.

如果您看到错误,例如

 Error in `[.data.frame`( _<your object name>_, ...

那么这可能是因为(a)您的对象不是 data.table 或(b)您忘记加载 data.table package

于 2013-05-20T12:32:06.230 回答
1

我不认为你可以用这个数据集做你想做的事。原因如下:

install.packages("plyr")
library("plyr")
str(tmp) # this is your data
count(tmp, vars = c("Operator", "ROI_Score"))

给出了这个:

   Operator ROI_Score freq
1         A      Crap    5
2         A      Good    5
3         A        OK    5
4         A      Poor    5
5         D      Crap    5
6         D      Good    5
7         D        OK    5
8         D      Poor    5
9         J      Crap    5
10        J      Good    5
11        J        OK    5
12        J      Poor    5
13        L      Crap    5
14        L      Good    5
15        L        OK    5
16        L      Poor    5
17        M      Crap    5
18        M      Good    5
19        M        OK    5
20        M      Poor    5

并且包括Date使每个值都是唯一的,因此所有值都为 1。

使用 a data.frame,您想要的原则上可以通过以下方式获得:

ans <- aggregate(N ~ Operator + ROI_Score + Date, data = tmp, FUN = sum)

然后将函数更改为您想要的(除以 100,条目数?)。但我不确定这是你想要的。

编辑

由于您想要按操作员和日期划分的每个评级类别的百分比,我会先进行子集化,然后再聚合:

tmp2 <- subset(tmp, Operator == "A")
ans2 <- aggregate(N ~ ROI_Score, data = tmp2, FUN = sum)
ans2$N.norm <- ans2$N/sum(ans2$N)

给出:

  ROI_Score  N    N.norm
1      Crap  0 0.0000000
2      Good 24 0.5106383
3        OK  9 0.1914894
4      Poor 14 0.2978723
于 2013-05-20T11:15:30.937 回答