- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尽管此细节当然是特定于应用程序的,但本着SO的精神,我试图将其尽可能地笼统!基本问题是,当一个data.frame具有特定日期而另一个data.frame具有日期范围时,如何按日期合并data.frames。其次,该问题询问如何处理给定变量的多个观测值,以及如何将其包含在最终输出data.frame中。我敢肯定,其中一些是标准的,但是经过充分的搜索后发现很少。
我尝试合并的mre对象如下。
# 'Speeches' data.frame
structure(list(Name = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("BBB",
"AAA"), class = "factor"), Date = structure(c(12543, 12404, 12404,
12404, 12373, 12362, 12345, 12320, 12207, 15450, 15449, 15449,
15449, 15449, 15449, 15449, 15449, 15448, 15448, 15448), class = "Date")), .Names = c("Name",
"Date"), row.names = c("1", "1.1", "1.2", "1.3", "1.4", "1.5",
"1.6", "1.7", "1.8", "2", "2.1", "2.2", "2.3", "2.4", "2.5",
"2.6", "2.7", "2.8", "2.9", "2.10"), class = "data.frame")
# 'History' data.frame
structure(list(Name = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("BBB", "AAA"), class = "factor"),
Role = structure(c(1L, 2L, 3L, 3L, 3L, 4L, 1L, 2L, 3L, 3L,
3L, 3L, 4L), .Label = c("Political groups", "National parties",
"Member", "Substitute", "Vice-Chair", "Chair", "Vice-President",
"Quaestor", "President", "Co-President"), class = "factor"),
Value = structure(c(10L, 12L, 6L, 3L, 8L, 4L, 9L, 11L, 1L,
7L, 1L, 2L, 5L), .Label = c("a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o"), class = "factor"),
Role.Start = structure(c(12149, 12149, 12150, 12150, 12152,
12150, 14439, 14439, 14441, 14503, 15358, 15411, 14441), class = "Date"),
Role.End = structure(c(12618, 12618, 12618, 12618, 12538,
12618, 15507, 15507, 15357, 15507, 15410, 15507, 15357), class = "Date")), .Names = c("Name",
"Role", "Value", "Role.Start", "Role.End"), row.names = c(NA,
13L), class = "data.frame")
structure(list(Name = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("BBB",
"AAA"), class = "factor"), Date = structure(c(12543, 12404, 12404,
12404, 12373, 12362, 12345, 12320, 12207, 15450, 15449, 15449,
15449, 15449, 15449, 15449, 15449, 15448, 15448, 15448), class = "Date"),
`Political groups` = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("i",
"j"), class = "factor"), `National parties` = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("k", "l"), class = "factor"),
Member.1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("f",
"g"), class = "factor"), Member.2 = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("b", "c"), class = "factor"), Member.3 = structure(c(NA,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), .Label = "h", class = "factor"), Substitute = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), .Label = "d", class = "factor")), .Names = c("Name",
"Date", "Political groups", "National parties", "Member.1", "Member.2",
"Member.3", "Substitute"), row.names = c("1", "1.1", "1.2", "1.3",
"1.4", "1.5", "1.6", "1.7", "1.8", "2", "2.1", "2.2", "2.3",
"2.4", "2.5", "2.6", "2.7", "2.8", "2.9", "2.10"), class = "data.frame")
最佳答案
更新:在v1.9.3 +中,现在实现了重叠联接。这是一种特殊情况,其中Date
的开始和结束Speeches
相同。我们可以使用foverlaps()
完成此操作,如下所示:
require(data.table) ## 1.9.3+
setDT(Speeches)
setDT(History)
Speeches[, `:=`(Date2 = Date, id = .I)]
setkey(History, Name, Role.Start, Role.End)
ans = foverlaps(Speeches, History, by.x=c("Name", "Date", "Date2"))[, Date2 := NULL]
ans = ans[order(id, Value)][, N := 1:.N, by=list(Name, Date, Role, id)]
ans = dcast.data.table(ans, id+Name+Date ~ Role+N, value.var="Value")
data.table
的方式。它使用两个滚动连接。
require(data.table) ## 1.9.2+
dt1 = as.data.table(Speeches)
dt2 = as.data.table(History)
# first rolling join - to get end indices
setkey(dt2, Name, Role.Start)
tmp1 = dt2[dt1, roll=Inf, which=TRUE]
# second rolling join - to get start indices
setkey(dt2, Name, Role.End)
tmp2 = dt2[dt1, roll=-Inf, which=TRUE]
# generate dt1's and dt2's corresponding row indices
idx = tmp1-tmp2+1L
idx1 = rep(seq_len(nrow(dt1)), idx)
idx2 = data.table:::vecseq(tmp2, idx, sum(idx))
dt1[, id := 1:.N] ## needed for casting later
# subset using idx1 and idx2 and bind them colwise
ans = cbind(dt1[idx1], dt2[idx2, -1L, with=FALSE])
# a little reordering to get the output correctly (factors are a pain!)
ans = ans[order(id,Value)][, N := 1:.N, by=list(Name, Date, Role, id)]
# finally cast them.
f_ans = dcast.data.table(ans, id+Name+Date ~ Role+N, value.var="Value")
id Name Date Political groups_1 National parties_1 Member_1 Member_2 Member_3 Substitute_1
1: 1 AAA 2004-05-05 j l c f NA d
2: 2 AAA 2003-12-18 j l c f h d
3: 3 AAA 2003-12-18 j l c f h d
4: 4 AAA 2003-12-18 j l c f h d
5: 5 AAA 2003-11-17 j l c f h d
6: 6 AAA 2003-11-06 j l c f h d
7: 7 AAA 2003-10-20 j l c f h d
8: 8 AAA 2003-09-25 j l c f h d
9: 9 AAA 2003-06-04 j l c f h d
10: 10 BBB 2012-04-20 i k b g NA NA
11: 11 BBB 2012-04-19 i k b g NA NA
12: 12 BBB 2012-04-19 i k b g NA NA
13: 13 BBB 2012-04-19 i k b g NA NA
14: 14 BBB 2012-04-19 i k b g NA NA
15: 15 BBB 2012-04-19 i k b g NA NA
16: 16 BBB 2012-04-19 i k b g NA NA
17: 17 BBB 2012-04-19 i k b g NA NA
18: 18 BBB 2012-04-18 i k b g NA NA
19: 19 BBB 2012-04-18 i k b g NA NA
20: 20 BBB 2012-04-18 i k b g NA NA
GenomicRanges
包来完成此操作,该包可以很好地处理Ranges,尤其是当您需要除range之外还需要附加列以(
Name
)连接时。您可以从
here安装它。
require(GenomicRanges)
require(data.table)
dt1 <- as.data.table(Speeches)
dt2 <- as.data.table(History)
gr1 = GRanges(Rle(dt1$Name), IRanges(as.numeric(dt1$Date), as.numeric(dt1$Date)))
gr2 = GRanges(Rle(dt2$Name), IRanges(as.numeric(dt2$Role.Start), as.numeric(dt2$Role.End)))
olaps = findOverlaps(gr1, gr2, type="within")
idx1 = queryHits(olaps)
idx2 = subjectHits(olaps)
# from here, you can do exactly as above
dt1[, id := 1:.N]
...
...
dcast.data.table(ans, id+Name+Date ~ Role+N, value.var="Value")
关于r - 范围联接data.frames-R中的日期范围/间隔的特定日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23371747/
最近,我在 XCode 上的调试器停止正常工作,并给出了以下错误: 此帧内的前一帧(gdb 无法展开超过此帧) 我已经浏览过 SO 的另一个线程,谈论丢失的符号。我通过删除符号目录并让它从 iPod
当我在设备上遇到断点时,出现以下调试器错误: 来自调试器的错误:此帧内的上一帧(gdb 无法展开超过此帧) 当应用程序遇到断点时,就会发生这种情况。如果我点击调试器中的继续按钮,它会愉快地继续,直到下
如何在 WinRT XAML 应用程序中操作框架的历史记录? 用户将从我的中心页面开始,他们可以在其中选择一个现有项目以转到其编辑屏幕,或者他们可以选择“新项目”。 “新项目”将引导他们完成一个简短的
2 帧,我希望第 2 帧“覆盖”/更新第 1 帧。基本上,其中 Table1-colB-value = Table2-oldB-value,用 Table2-newB-value 覆盖 Table1-
我正在尝试逐帧动画,但它给了我一个强制关闭,我不确定为什么它给我一个强制关闭。在我看来一切都很好。 这是我的代码,我希望有人能帮忙吗?提前致谢。 动画测试.java import android.ap
这是我的主课。 package pomsystem; public class POMSystem { public static void main(String[] args) {
如果为TRUE,我想从函数返回一个data.frame,否则使用return(ifelse(condition, mydf, NA)) 返回NA 但是, ifelse 会从 data.frame 中删
我正在处理两个 csv 文件并导入为数据框 df1 和 df2 df1 有 50000 行,df2 有 150000 行。 我想比较(遍历每一行)df2 的“时间”df1,求时间差,返回所有列的值对应
我将许多文件夹中的小文本文件读取到一个列表中。因此,我有一个长度为 n 的列表,其中包含 2 个 data.frames。 这是列表元素 3 的示例(在问题末尾输入) ip_list[[3]] $`d
为了找出数据框 df.a 是否是数据框 df.b 的子集,我做了以下操作: df.a semi_join(df.b, df.a) Joining by: c("x", "y") x y 1 1
在某些情况下,出于实现原因,我有一个只包含一个列的 data.frame df=as.data.frame(alpha=1:15) 如果我现在使用 df[-1, ] 它返回一个向量,但我想将它保留为一
Windows 8.1 商店应用中的 Frame.Navigate() 和 this.Frame.Navigate() 有什么区别? 这有什么区别 Frame.Navigate(typeof(Logi
我有两个 data.frames (df1, df2),我想用 df1$V2 的值替换 P1-P10 列中的字母值,但保留前两列df2. df1 = data.frame(V1=LETTERS, V2
有没有更好的方法来制作与现有 data.frame 具有相同维度、列名和行名的空白 data.frame? BAM<-read.table(~/myfile) # 10 rows and 10 co
我有一个列表 data.frame s。例如 set.seed(1) my_list result.df id var_p var_m var_d var_a
假设我有两个数据框 df1 和 df2 如下 Df1 Id Price Profit Month 10 5 2 1 10 5 3 2 10 5 2
我将创建一个网站,除了它自己的内容之外,它还将链接(在 iframe 中)到世界最大的报纸网站,如纽约时报、金融时报和其他一些网站。 但是我遇到了框架许可的问题。例如,纽约时报向我展示了一个错误 Lo
假设我有一个 data.frame: df x x A 1 10 2 20 3 30 从文档中(参见 ?"[" )您可以找到: If drop=TRUE the result is coer
我有一个想要克隆的现有 data.frame,但没有其中的值。 IE。我希望新框架具有与现有框架相同的列名称和类型。原始帧的大小可能为 GB 量级,因此复制和删除数据似乎不是正确的方法,并且迭代现有列
给定两个在列名称/数据类型方面相同的数据帧,其中某些列唯一标识行,是否有一种有效的函数/方法可以让一个数据帧“更新”另一个数据帧? 例如,在下面的例子中,原始和替换由'Name'和'Id'标识>。 g
我是一名优秀的程序员,十分优秀!