说我有以下数据,dat1;
width from by
2 1 A
3 1 A
2 2 A
3 2 A
2 1 B
3 1 B
2 2 B
3 2 B
另外还有那个,dat2;
x pos by
4 1 A
5 2 A
7 3 A
3 4 A
2 1 B
4 2 B
3 3 B
5 4 B
假设我想在dat1上创建一个新的列,其中包含来自dat2的滚动和值;
此滚动总和的宽度等于该行中给出的宽度
我们的起始位置相当于该行中的from vector值
我们希望根据行中的哪个级别为A或Bth因子执行
到目前为止,我有我们想要的
rollapply(x = dat2$x, width = dat1$width, FUN = sum, align = "left", data = dat2)
所以我需要将起始位置和因子级别合并到该起始位置。
所以在这个例子中我想得到
width from by RS
2 1 A 9
3 1 A 16
2 2 A 12
3 2 A 15
等
非常感谢任何帮助。感谢
答案 0 :(得分:4)
1)对于Public Function StartupSettings()
On Error GoTo Error_StartupSettings
'Hide menubars
'When used with the Application object,
'the MenuBar property enables you to display a custom menu bar
'throughout the database. However, if you've set the MenuBar
'property for a form or report in the database, the custom menu
'bar of the form or report will be displayed in place of the database's
'custom menu bar whenever the form or report has the focus.
'When the form or report loses the focus, the custom menu bar for the
'database is displayed.
'Application.MenuBar = "mcrBlankMenuBar"
ControlUnlockT DLookup("Code", "tblSecurity", "Section=1"), DLookup("Code", "tblSecurity", "Section=2"), _
DLookup("Code", "tblSecurity", "Section=3"), DLookup("Code", "tblSecurity", "Section=4")
ControlUnlock DLookup("Code", "tblSecurity", "Section=1"), DLookup("Code", "tblSecurity", "Section=2"), _
DLookup("Code", "tblSecurity", "Section=3"), DLookup("Code", "tblSecurity", "Section=4")
'Change the keyboard Move After Enter behavior to Next Field rather
'then next record or dont move
Application.SetOption "Move After Enter", 1
'Hide database window
DoCmd.Echo False
'DoCmd.SelectObject A_MACRO, "mcrOpenMDB2", True
'DoCmd.DoMenuItem 1, 4, 3, , A_MENU_VER20
DoCmd.Echo True
Exit_StartupSettings:
Exit Function
Error_StartupSettings:
MsgBox Err.Description, 16, "Error " & Err & " - StartupSettings"
Resume Exit_StartupSettings
End Function
中的每一行i
,匿名函数将dat2子集设置为dat1
中的by
值并从中挑选出来指示的dat1
行并将它们相加:
x
,并提供:
transform(dat1, RS = sapply(1:nrow(dat1), function(i)
sum(subset(dat2, dat1$by[i] == by)[seq(from[i], length = width[i]), "x"])))
2)另一种方法是计算序列的起始值和宽度,以便在 width from by RS
1 2 1 A 9
2 3 1 A 16
3 2 2 A 12
4 3 2 A 15
5 2 1 B 6
6 3 1 B 9
7 2 2 B 7
8 3 2 B 12
中求和,然后应用:
dat2
,并提供:
st <- match(dat1$by, dat2$by) + dat1$from - 1
w <- dat1$width
Sum <- function(st, w) sum(dat2[seq(st, length = w), "x"])
transform(dat1, RS = mapply(Sum, st, w))
width from by RS
1 2 1 A 9
2 3 1 A 16
3 2 2 A 12
4 3 2 A 15
5 2 1 B 6
6 3 1 B 9
7 2 2 B 7
8 3 2 B 12
和dat1
是:
dat2
修正(1)。添加了(2)。
答案 1 :(得分:0)
另一种选择可能是使用join
和join
。该方法将filter
两个数据帧“by”。然后应用pos
仅考虑from
介于from+width
和x
之间的行。最后取dat1 %>% inner_join(dat2, by = "by") %>%
filter(from <= pos & pos < (from + width) ) %>%
group_by(by, from, width ) %>%
summarise(RS = sum(x)) %>%
select(width, from, by, RS)
# A tibble: 8 x 4
# Groups: by, from [4]
# width from by RS
# <int> <int> <chr> <int>
# 1 2 1 A 9
# 2 3 1 A 16
# 3 2 2 A 12
# 4 3 2 A 15
# 5 2 1 B 6
# 6 3 1 B 9
# 7 2 2 B 7
# 8 3 2 B 12
列的总和。
dat1 <- read.table(text =
"width from by
2 1 A
3 1 A
2 2 A
3 2 A
2 1 B
3 1 B
2 2 B
3 2 B", header = TRUE, stringsAsFactors = FALSE)
dat2 <- read.table(text =
"x pos by
4 1 A
5 2 A
7 3 A
3 4 A
2 1 B
4 2 B
3 3 B
5 4 B", header = TRUE, stringsAsFactors = FALSE)
数据强>
temp.boxplot("price", by="year")