Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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
使用data.table包或其他解决方案对数据帧进行子集和重新组合[R]_R_Data.table - Fatal编程技术网

使用data.table包或其他解决方案对数据帧进行子集和重新组合[R]

使用data.table包或其他解决方案对数据帧进行子集和重新组合[R],r,data.table,R,Data.table,我对R很陌生,有一个关于使用其中一个变量的范围值在两个数据帧之间进行子集和重新组合的问题。我有两个数据帧,如下所示: x y [1,] 79.00 19.63 [2,] 79.01 19.58 [3,] 79.02 19.57 [4,] 79.03 19.58 [5,] 79.04 19.60 [6,] 79.05 19.65 [7,] 79.06

我对R很陌生,有一个关于使用其中一个变量的范围值在两个数据帧之间进行子集和重新组合的问题。我有两个数据帧,如下所示:

        x         y                         
 [1,] 79.00     19.63
 [2,] 79.01     19.58
 [3,] 79.02     19.57
 [4,] 79.03     19.58
 [5,] 79.04     19.60
 [6,] 79.05     19.65
 [7,] 79.06     19.67
 [8,] 79.07     19.70
 [9,] 79.08     19.67
[10,] 79.09     19.72

我的目的是将两者结合起来,如下所示:

     id           x       y
7G005-1010-10   79,01   19,58
7G005-1010-10   79,02   19,57
7G005-1010-10   79,03   19,58
7G005-1010-10   79,04   19,6
7G005-1010-10   79,05   19,65
7G005-1010-10   79,06   19,7
7G100-0001-10   79,02   19,57
     ...         ...     ...
正如您在我的数据帧输出中看到的,我尝试使用
data.table
包来找到解决问题的方法

好的,如果有人能告诉我如何处理它(有或没有
data.table

先谢谢你


很抱歉英文不好。

这在
数据表中是不可能的。这是一个很好的实施方法。您可以尝试package
xts
,因为我认为它具有此操作

数据中的一条长而笨重的路径(未测试)。表如下所示。假设您的第一个表是
P
,包含范围的第二个表是
R

setkey(P,x)
# sort by x and mark as sorted so future queries can use binary search on P

from = P[J(R$min_x),which=TRUE]
# Lookup each min_x in the key of P, returning the location. J stands for Join.

to = P[J(R$max_x),which=TRUE]
# Lookup each max_x in the key of P, returning the location.

len = to-from+1
# vectorized for each item the length to[i]-from[i]+1

i = unlist(mapply("seq.int",from,to,SIMPLIFY=FALSE))
# for each item the sequence from[i]:to[i], then concat them all into one vector

cbind(rep(R$id,len), P[i])
# use len to expand the items of R to match what they match to in P

那么,您正试图根据x所处的范围获取id?如果这是你想要的,那么范围重叠!第一个id与第二个重叠,第二个id也与第三个重叠。您是如何处理的?感谢您提出了一个非常明确的问题,包括输入和期望的输出!这太棒了。:)使用
data.table
version>=v1.9.8(于2016年11月25日发布到CRAN),您可以使用
DT1[DT2,on=(x>=min_x,xSorry,感谢您的延迟,非常感谢您的建议。我很快就会尝试。感谢您againI尝试了我们的代码,但我有一个问题。我在我的帖子中使用了类似的data.table(如第一个).当我使用设置键函数“Erreur dans setkeyv(P,x)”运行代码时,我收到了此消息:列“x”不能强制为整数而不丢失分数数据。“我查找了data.table软件包的手册,但找不到它。如果您能启发我的话。谢谢。祝您好运。@mat您需要从R-Forge repo安装升级到v1.8.1。很抱歉,我应该提到这一点。请遵循lin主页上的k。允许浮点('数字')in keys在v1.8.1中是新的。请参阅最近的演示文稿。非常感谢您的提示,您的代码工作得非常好!我不想利用您的好意,但您能解释一下您的代码吗?我不了解所有的细节。正如我所说,我是R的新手,我希望提高我在该领域的知识。再一次,非常感谢您的帮助回答并感谢data.table这一出色的软件包。致以最诚挚的问候
setkey(P,x)
# sort by x and mark as sorted so future queries can use binary search on P

from = P[J(R$min_x),which=TRUE]
# Lookup each min_x in the key of P, returning the location. J stands for Join.

to = P[J(R$max_x),which=TRUE]
# Lookup each max_x in the key of P, returning the location.

len = to-from+1
# vectorized for each item the length to[i]-from[i]+1

i = unlist(mapply("seq.int",from,to,SIMPLIFY=FALSE))
# for each item the sequence from[i]:to[i], then concat them all into one vector

cbind(rep(R$id,len), P[i])
# use len to expand the items of R to match what they match to in P