在R中组合特定行数据

在R中组合特定行数据,r,R,嗨,我有一个数据集,其中一行按顺序显示一个项目。一个订单可以有更多的项目,因此数据集中可以有更多的行 数据如下所示: code nr. Of items 1 252222016 5 3 252812016 1 5 252812016 1 6 253012016 1 12 253042016 20 13 253042016 20 15 253052016 1 16 253072016 3 18 253082016

嗨,我有一个数据集,其中一行按顺序显示一个项目。一个订单可以有更多的项目,因此数据集中可以有更多的行

数据如下所示:

    code        nr. Of items
1   252222016   5
3   252812016   1
5   252812016   1
6   253012016   1
12  253042016   20
13  253042016   20
15  253052016   1
16  253072016   3
18  253082016   4
我希望得到一个结果,在这个结果中,我将汇总“项目数量”,其中有相同的“代码”,并将资源写入行中。如果一个订单有更多的项目,我希望在订单的最后一行中写入资源,前面的行将是NAs

我希望最终解决方案的外观如下:

        code    nr. Of items    result
1   252222016   5                 5
3   252812016   1                na
5   252812016   1                 2
6   253012016   1                 1
12  253042016   20               na
13  253042016   20               40
15  253052016   1                 1
16  253072016   3                 3
18  253082016   4                 4

我会感谢你的帮助

我们可以使用
dplyr
包来实现这一点:

library(dplyr)    # load package
df1 %>%
    group_by(code) %>%
    mutate(rownum = 1,
           c_s_rn = cumsum(rownum),
           result = ifelse(c_s_rn == max(c_s_rn), sum(items), NA)) %>%
    select(-rownum, -c_s_rn)

#        code items result
# 1 252222016     5      5
# 2 252812016     1     NA
# 3 252812016     1      2
# 4 253012016     1      1
# 5 253042016    20     NA
# 6 253042016    20     40
# 7 253052016     1      1
# 8 253072016     3      3
# 9 253082016     4      4
还有一个基本的
R
解决方案,使用
lappy
split

df1_2 <- df1
df1_2$rownum <- 1
do.call('rbind',
lapply(split(df1_2, df1_2$code), function(x)
    data.frame(x, 
               result = ifelse(cumsum(x$rownum) == sum(x$rownum), sum(x$items), NA)))
)[,-3]

#                   code items result
# 252222016    252222016     5      5
# 252812016.3  252812016     1     NA
# 252812016.5  252812016     1      2
# 253012016    253012016     1      1
# 253042016.12 253042016    20     NA
# 253042016.13 253042016    20     40
# 253052016    253052016     1      1
# 253072016    253072016     3      3
# 253082016    253082016     4      4

df1_2您可以使用参数
fromLast=TRUE
duplicated
功能,即

library(dplyr)
df %>% 
  group_by(code) %>% 
  mutate(new = replace(cumsum(nr. Of items), duplicated(code, fromLast = TRUE), NA))

#Source: local data frame [9 x 3]
#Groups: code [7]

#       code    nr   new
#      <int> <int> <int>
#1 252222016     5     5
#2 252812016     1    NA
#3 252812016     1     2
#4 253012016     1     1
#5 253042016    20    NA
#6 253042016    20    40
#7 253052016     1     1
#8 253072016     3     3
#9 253082016     4     4
库(dplyr)
df%>%
分组人(代码)%>%
变异(新=替换(累计总数(项目数),重复(代码,fromLast=真),NA))
#来源:本地数据帧[9 x 3]
#组别:代号[7]
#代码nr新
#        
#1 252222016     5     5
#252812016 1不适用
#3 252812016     1     2
#4 253012016     1     1
#5 253042016不适用
#6 253042016    20    40
#7 253052016     1     1
#8 253072016     3     3
#9 253082016     4     4

另一个dplyr选项,使用
ifelse
fromLast=TRUE

library(dplyr)

df1 <- df1 %>% 
  group_by(code) %>% 
  mutate(result = ifelse(duplicated(code, fromLast = TRUE), NA, sum(nr.Of.items)))
库(dplyr)
df1%
分组人(代码)%>%
变异(结果=ifelse(重复(代码,fromLast=TRUE),NA,总和(项数)))

我们可以使用
数据表来实现这一点。将“data.frame”转换为“data.table”(
setDT(df1)
),按“code”分组,
如果行数大于1(
.N>1
),则将
NA
复制一个小于行数的行,并将(
c(
)与
sum
的“items”或
else
返回“items”连接起来。分配(
:=
创建新列“result”的输出

library(data.table)
setDT(df1)[, result := if(.N>1) c(rep(NA, .N-1), sum(items)) else items, by = code]
df1
#        code items result
#1: 252222016     5      5
#2: 252812016     1     NA
#3: 252812016     1      2
#4: 253012016     1      1
#5: 253042016    20     NA
#6: 253042016    20     40
#7: 253052016     1      1
#8: 253072016     3      3
#9: 253082016     4      4

或者,除了使用
if/else
之外,我们还可以在最后一行以外的位置创建NA,并与“items”的
sum
相乘

setDT(df1)[, result := NA^(seq_len(.N) != .N)*sum(items) , by = code]
数据
df1另一种使用
dplyr
包的方法(使用@bounchball的
df1
)是:


对不起,我不太明白这个问题,您想要什么?是
code
订单号吗?那么为什么您有相同的订单号,两个
nr.项目
计数,而不是组合计数?如果您想对每个订单的项目总数求和,保留一些NAs行的目的是什么?这些NAs行包含哪些信息提供?
setDT(df1)[, result := NA^(seq_len(.N) != .N)*sum(items) , by = code]
df1 <- structure(list(
code = c(252222016L, 252812016L, 252812016L, 253012016L, 
         253042016L, 253042016L, 253052016L, 253072016L, 253082016L), 
items = c(5L, 1L, 1L, 1L, 20L, 20L, 1L, 3L, 4L)), 
.Names = c("code", "items"), class = "data.frame", 
row.names = c("1", "3", "5", "6", "12", "13", "15", "16", "18"))
library(dplyr)
df1 %>% group_by(code) %>% mutate(result=ifelse(row_number()==n(),sum(items),NA))