Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
相对于每组中的值的比例(通过dplyr)_R_Dplyr - Fatal编程技术网

相对于每组中的值的比例(通过dplyr)

相对于每组中的值的比例(通过dplyr),r,dplyr,R,Dplyr,我有一组时间序列,我想根据它们在特定时间间隔内的值来调整它们。这样,每个系列在当时都将为1.0,并按比例变化 我不知道如何使用dplyr来实现这一点 下面是一个使用for循环的工作示例: library(dplyr) data = expand.grid( category = LETTERS[1:3], year = 2000:2005) data$value = runif(nrow(data)) # the first time point in the series base

我有一组时间序列,我想根据它们在特定时间间隔内的值来调整它们。这样,每个系列在当时都将为1.0,并按比例变化

我不知道如何使用dplyr来实现这一点

下面是一个使用for循环的工作示例:

library(dplyr)

data = expand.grid(
  category = LETTERS[1:3],
  year = 2000:2005)
data$value = runif(nrow(data))

# the first time point in the series
baseYear = 2002

# for each category, divide all the values by the category's value in the base year
for(category in as.character(levels(factor(data$category)))) {
  data[data$category == category,]$value = data[data$category == category,]$value / data[data$category == category & data$year == baseYear,]$value[[1]]
}
编辑:修改问题,使基准时间点不可索引。有时“时间”列实际上是一个因子,它不一定是有序的。

类似于这样:

data %>% 
  group_by(category) %>% 
  mutate(value=value/value[1]) %>%
  arrange(category,year)
结果:

#   category year     value
#1         A 2000 1.0000000
#2         A 2001 0.2882984
#3         A 2002 1.5224308
#4         A 2003 0.8369343
#5         A 2004 2.0868684
#6         A 2005 0.2196814
#7         B 2000 1.0000000
#8         B 2001 0.5952027

在dplyr中首先使用
first
,确保使用
order\u by

data %>% 
  group_by(category) %>% 
  mutate(value = value / first(value, order_by = year))

此解决方案与@thelatemail非常相似,但我认为它的差异足够大,可以得到自己的答案,因为它根据以下条件选择索引:

data %>%
    group_by(category) %>%
    mutate(value = value/value[year == baseYear])

#   category  year      value
#...     ...   ...       ...
#7         A  2002 1.00000000
#8         B  2002 1.00000000
#9         C  2002 1.00000000
#10        A  2003 0.86462789
#11        B  2003 1.07217943
#12        C  2003 0.82209897

(数据输出已被截断。要复制这些结果,
set.seed(123)
在创建
Data
时)

另一种方法是先使用
然后再使用
排序。使用
order\u by
,每个类别的最短年份应位于第一行<代码>变化(分组依据(数据,类别),输出=订单依据(年份,值/第一个(值))%%>%排列(类别)
谢谢!如果我想按年中位数进行缩放,而索引是不起作用的呢?@sharoz你是说
mutate(value=value/median(year))
@DavidRobinson我指的是中位数的值year@sharoz:啊,明白了。您可以使用
approx
函数,该函数基于另一个向量(线性)插值一个向量。尝试
data%>%group\u by(category)%%>%arrange(category,year)%%>%mutate(value=value/approx(year,value,median(year))$y)
谢谢!如果我想按年中位数或索引不起作用的场景进行缩放,该怎么办?
mutate(valute=value/median(value))
使用
group_by
选择中位数应接管的子集。这是媒体值。我想问的是,如果它以年中位数(或某一特定年份)的值来衡量,会怎么样。