如何从另一列索引的列中提取某些值?

问题描述 投票:1回答:4

我有以下数据,我正在尝试提取cumsum列的最终值。例如,我想从cumsum0.7810417的数据框中获取第8行,然后使用1115cumsum0.7282639 ... 1116等对1117执行相同的操作。

structure(list(WEEK = c(1114L, 1114L, 1114L, 1114L, 1114L, 1114L, 
1114L, 1114L, 1115L, 1115L, 1115L, 1115L, 1115L, 1115L, 1115L, 
1116L, 1116L, 1116L, 1116L, 1116L, 1116L, 1117L, 1117L, 1117L, 
1117L, 1118L, 1118L, 1118L, 1118L, 1118L, 1118L, 1118L, 1119L, 
1119L, 1119L, 1119L, 1119L, 1119L, 1119L, 1119L, 1120L, 1120L, 
1120L, 1120L, 1120L, 1120L, 1120L, 1121L, 1121L, 1121L), price_per_ounce = c(0.124583333333333, 
0.0832638888888889, 0.100972222222222, 0.100972222222222, 0.10375, 
0.0832638888888889, 0.100972222222222, 0.0832638888888889, 0.124583333333333, 
0.0855555555555556, 0.109166666666667, 0.0832638888888889, 0.110972222222222, 
0.10375, 0.110972222222222, 0.124583333333333, 0.0841666666666667, 
0.110972222222222, 0.110972222222222, 0.0832638888888889, 0.110972222222222, 
0.0838888888888889, 0.110972222222222, 0.0832638888888889, 0.110972222222222, 
0.124583333333333, 0.0786111111111111, 0.110972222222222, 0.110972222222222, 
0.10375, 0.110972222222222, 0.0832638888888889, 0.124583333333333, 
0.0925, 0.110972222222222, 0.0832638888888889, 0.110972222222222, 
0.10375, 0.110972222222222, 0.0832638888888889, 0.124583333333333, 
0.0844444444444444, 0.110972222222222, 0.0832638888888889, 0.10375, 
0.110972222222222, 0.0832638888888889, 0.124583333333333, 0.0694444444444444, 
0.110972222222222), Total = c(0.124583333333333, 0.207847222222222, 
0.308819444444444, 0.409791666666667, 0.513541666666667, 0.596805555555556, 
0.697777777777778, 0.781041666666667, 0.124583333333333, 0.210138888888889, 
0.319305555555556, 0.402569444444444, 0.513541666666667, 0.617291666666667, 
0.728263888888889, 0.124583333333333, 0.20875, 0.319722222222222, 
0.430694444444444, 0.513958333333333, 0.624930555555556, 0.0838888888888889, 
0.194861111111111, 0.278125, 0.389097222222222, 0.124583333333333, 
0.203194444444444, 0.314166666666667, 0.425138888888889, 0.528888888888889, 
0.639861111111111, 0.723125, 0.124583333333333, 0.217083333333333, 
0.328055555555556, 0.411319444444444, 0.522291666666667, 0.626041666666667, 
0.737013888888889, 0.820277777777778, 0.124583333333333, 0.209027777777778, 
0.32, 0.403263888888889, 0.507013888888889, 0.617986111111111, 
0.70125, 0.124583333333333, 0.194027777777778, 0.305)), .Names = c("WEEK", 
"price_per_ounce", "Total"), class = c("data.table", "data.frame"
), row.names = c(NA, -50L), .internal.selfref = <pointer: 0x0000000008450788>)

编辑:我将数据帧设置为df < - head(df,100)时获得的结果

structure(list(df = c(0.781041666666667, 0.728263888888889, 0.624930555555556, 
0.389097222222222, 0.723125, 0.820277777777778, 0.70125, 0.658611111111111, 
0.769583333333333, 0.759027777777778, 0.751666666666667, 0.741597222222222, 
0.519930555555556, 0.712152777777778)), .Names = "df", row.names = c(NA, 
-14L), class = "data.frame")

但是,当我在整个数据帧中运行时,我得到以下内容。

structure(list(df = c(220.124649739256, 199.217289598068, 199.774511556463, 
206.738587849235, 205.766197136359, Inf, Inf, Inf, 205.103350187295, 
199.567357907284, 212.900103648094, 200.477169383407, Inf, 203.441435413023
)), .Names = "df", row.names = c(NA, 14L), class = "data.frame")

我不确定为什么现在出现Infvalues,值也发生了显着变化,但我所做的只是跳过这个命令df <- head(df, 100)

这是我正在应用的代码(当我应用下面的所有解决方案时,会发生同样的事情)

Price <- data %>%
  select(WEEK, price_per_ounce)

test <- transform(Price, Total = ave(price_per_ounce, WEEK, FUN = cumsum))

test <- head(test, 100)
#dput(test)

df <- test[, Total[.N], WEEK]$V1
df <- as.data.frame(df)
df <- head(df, 14)
dput(df)
r cumsum
4个回答
3
投票

基地R.

aggregate(df$cumsum, by = list(df$wks), FUN = tail, n = 1)

3
投票

OP的数据集是data.table。使用data.table获取每个'WEEK'元素的最后一行'TOTAL并提取'TOTAL'的方法将是

library(data.table)
df1[, Total[.N], WEEK]$V1
#[1] 0.7810417 0.7282639 0.6249306 0.3890972 0.7231250 0.8202778 0.7012500 0.3050000

如果目的不是提取列,那么我们可以将其子集化并保持为data.table

df1[, .(Total = Total[.N]),  WEEK]

2
投票
tapply(dat$Total, dat$WEEK, tail, 1)
#     1114      1115      1116      1117      1118      1119      1120      1121 
#0.7810417 0.7282639 0.6249306 0.3890972 0.7231250 0.8202778 0.7012500 0.3050000 

说明

dat$Total是我们的原子对象,dat$WEEK我们的分组变量,tail我们的函数被应用,1是传递给它的第一个参数,所以我们得到dat$Total中每个索引的dat$WEEK的最后一个值。


1
投票
library(dplyr)   
df %>% group_by(wks)%>%
filter(row_number()==n())
© www.soinside.com 2019 - 2024. All rights reserved.