我有一个df,看起来或多或少是这样的:
await member.send(`You have been kicked from \`${message.guild.name}\`. Reason: \`${reason}\``)
.catch(err => message.channel.send(`⚠ Unable to alert ${member} of reason.`));
await member.kick(reason);
await message.channel.send(` ${member} has been kicked!`);
console.log(`${message.author.tag} kicked ${member.user.tag} from '${message.guild.name}'.`);
我正在尝试绘制这样的图形:
表示每个组(A,B,C)与上一年的变化(%)。 问题是,有时先例年份是-1,有时是-2或-3等...
要做这样的图,我需要计算滞后值,然后计算百分比差如下:
year <- c(2000,2000,2000,2002,2002,2002,2003,2003,2003,2006,2006,2006)
type <- c("A","B","C","A","B","C","A","B","C","A","B","C")
value <- c(71,80,99,79,61,62,56,88,50,85,56,85)
df <- data.frame(year, type, value)
rm(year, type, value)
我的问题分为2个:
您如何正确计算滞后值?
有没有一种快速的方法来计算与上一年相比的百分比变化?
答案 0 :(得分:0)
我们可以按“滞后”分组来完成此操作,然后进行区别
decryptTest(input: string) {
let key = "secret";
return crypto.AES.decrypt(input, key, {
iv: 'aTestIvValueKey',
mode: crypto.mode.CBC,
padding: crypto.pad.Pkcs7
}).toString();
}
library(dplyr)
df %>%
group_by(type) %>%
mutate(lag = value - lag(value),
perc = lag/lag(value))
# A tibble: 12 x 5
# Groups: type [3]
# year type value lag perc
# <dbl> <fct> <dbl> <dbl> <dbl>
# 1 2000 A 71 NA NA
# 2 2000 B 80 NA NA
# 3 2000 C 99 NA NA
# 4 2002 A 79 8 0.113
# 5 2002 B 61 -19 -0.238
# 6 2002 C 62 -37 -0.374
# 7 2003 A 56 -23 -0.291
# 8 2003 B 88 27 0.443
# 9 2003 C 50 -12 -0.194
#10 2006 A 85 29 0.518
#11 2006 B 56 -32 -0.364
#12 2006 C 85 35 0.7
答案 1 :(得分:0)
考虑以R为基础的ave
,用diff
计算 lag 并计算百分比变化:
df <- cbind.data.frame(year, type, value)
df <- within(df, {
lag <- ave(value, type, FUN=function(x) c(NA, diff(x)))
prev <- ave(value, type, FUN=function(x) c(NA, x[1:length(x)-1]))
pct <- ifelse(is.na(prev), NA, lag / prev)
rm(prev)
})
df
# year type value pct lag
# 1 2000 A 71 NA NA
# 2 2000 B 80 NA NA
# 3 2000 C 99 NA NA
# 4 2002 A 79 0.1126761 8
# 5 2002 B 61 -0.2375000 -19
# 6 2002 C 62 -0.3737374 -37
# 7 2003 A 56 -0.2911392 -23
# 8 2003 B 88 0.4426230 27
# 9 2003 C 50 -0.1935484 -12
# 10 2006 A 85 0.5178571 29
# 11 2006 B 56 -0.3636364 -32
# 12 2006 C 85 0.7000000 35