每组n和n-2行之间的差异

时间:2018-04-29 15:58:22

标签: r

我想确定计算每组n和n-2行之间差异的方法。让我们假设以下是我的数据:

Month, Laptop, Sales
Jan, HP, 1000
Feb, HP, 2000
Mar, HP, 1300
April, HP, 5000
Jan, Samsung, 1200
Feb, Samsung, 2500
Mar, Samsung, 1100
April, Samsung, 4500

现在,我需要输出如下所示,我在每个n和n-2行之间取差异,并根据每月数据查找每个组的结果。

Month, Laptop, Sales, difference
Jan, HP, 1000 , NA
Feb, HP, 2000 , NA
Mar, HP, 1300 , 300
April, HP, 5000 , 3000,
Jan, Samsung, 1200 , NA
Feb, Samsung, 2500 , NA
Mar, Samsung, 1100 , -100
April, Samsung, 4500 , 2000

4 个答案:

答案 0 :(得分:4)

使用dplyr::lag功能,您可以

df <- df %>%
  group_by(Laptop) %>%
  mutate(difference = Sales - lag(Sales, 2))
df

# # A tibble: 8 x 4
# # Groups:   Laptop [2]
#   Month Laptop  Sales difference
#   <chr> <chr>   <int>      <int>
# 1 Jan   HP       1000         NA
# 2 Feb   HP       2000         NA
# 3 Mar   HP       1300        300
# 4 April HP       5000       3000
# 5 Jan   Samsung  1200         NA
# 6 Feb   Samsung  2500         NA
# 7 Mar   Samsung  1100       -100
# 8 April Samsung  4500       2000

数据

t <- "Month, Laptop, Sales
Jan, HP, 1000
Feb, HP, 2000
Mar, HP, 1300
April, HP, 5000
Jan, Samsung, 1200
Feb, Samsung, 2500
Mar, Samsung, 1100
April, Samsung, 4500"

df <- read.table(text = t, header = T, sep = ",", strip.white = TRUE)

答案 1 :(得分:2)

可以使用diff函数和dplyr来获得所需的输出。

lag = 2
df %>% group_by(Laptop) %>%
  mutate(difference = c(rep(NA,lag), diff(Sales, lag)))

# # A tibble: 8 x 4
# # Groups: Laptop [2]
#   Month Laptop  Sales difference
#   <chr> <chr>   <int>      <int>
# 1 Jan   HP       1000         NA
# 2 Feb   HP       2000         NA
# 3 Mar   HP       1300        300
# 4 April HP       5000       3000
# 5 Jan   Samsung  1200         NA
# 6 Feb   Samsung  2500         NA
# 7 Mar   Samsung  1100      - 100
# 8 April Samsung  4500       2000

数据:

df <- read.table(text =
"Month, Laptop, Sales
Jan, HP, 1000
Feb, HP, 2000
Mar, HP, 1300
April, HP, 5000
Jan, Samsung, 1200
Feb, Samsung, 2500
Mar, Samsung, 1100
April, Samsung, 4500",
header = TRUE, sep = ",", strip.white = TRUE, stringsAsFactors = FALSE)

答案 2 :(得分:0)

您可以使用n()来访问dplyr中该组的最后一个行号,以及它在data.table中的等效.N

library(dplyr)    
df %>%
  group_by(Laptop) %>%
  mutate(difference =  c(NA,NA,Sales[3:n()]) - c(NA,NA,Sales[1:(n()-2)]) )

# A tibble: 8 x 4
# Groups:   Laptop [2]
   Month  Laptop Sales difference
  <fctr>  <fctr> <int>      <int>
1    Jan      HP  1000         NA
2    Feb      HP  2000         NA
3    Mar      HP  1300        300
4  April      HP  5000       3000
5    Jan Samsung  1200         NA
6    Feb Samsung  2500         NA
7    Mar Samsung  1100       -100
8  April Samsung  4500       2000

使用data.table

library(data.table)

setDT(df)[,.(difference =  c(NA,NA,Sales[3:.N]) - c(NA,NA,Sales[1:(.N-2)]) ), by = Laptop]

    Laptop difference
1:      HP         NA
2:      HP         NA
3:      HP        300
4:      HP       3000
5: Samsung         NA
6: Samsung         NA
7: Samsung       -100
8: Samsung       2000

答案 3 :(得分:0)

在基地R:

df$difference <- unlist(tapply(df$Sales,df$Laptop,function(x) c(NA,NA,diff(x,2))))

#   Month  Laptop Sales difference
# 1   Jan      HP  1000         NA
# 2   Feb      HP  2000         NA
# 3   Mar      HP  1300        300
# 4 April      HP  5000       3000
# 5   Jan Samsung  1200         NA
# 6   Feb Samsung  2500         NA
# 7   Mar Samsung  1100       -100
# 8 April Samsung  4500       2000