我写了以下代码
gem 'bootstrap-sass', '~> 3.3.6'
gem 'sass-rails', '~> 5.0'
bundle install
x看起来像:
a<-matrix(0,1,nrow = nrow(x))
for(i in 1:nrow(x)){
for(j in 1:nrow(y)){
if((y[j,3] > x[i,2]) & (y[j,2] == x[i,1])){
a[i,]<- y[j,4]
i<- i+1
}
}
和y看起来像
x1 x2
401 4
401 38
401 142...
我希望 y1 y2 y3 y4
1 401 10 22.152
2 401 40 167.986
3 401 70 393.198
4 401 100 923
5 401 120 923
6 401 140 686.712
7 401 160 865.774...
成为:
a
其中22.152
167.986
865.774...
为nrow(y) > nrow(x)
。是否有可能提高效率?
答案 0 :(得分:2)
使用data.table
,我们加入on
&#39; x1&#39;,&#39; y1&#39;来自&#39; x&#39;并且&#39; y&#39;,将i
指定为y3 > x2
,按&#39; x1&#39;分组和&#39; x2&#39;,获取第一行
library(data.table)
setDT(x)[y, on = .(x1 = y2), allow.cartesian = TRUE][y3 > x2, head(.SD, 1) , .(x1, x2)]
# x1 x2 y1 y3 y4
#1: 401 4 1 10 22.152
#2: 401 38 2 40 167.986
#3: 401 142 7 160 865.774
x <- read.table(text = "
x1 x2
401 4
401 38
401 142", header = TRUE)
y <- read.table(text = "
y1 y2 y3 y4
1 401 10 22.152
2 401 40 167.986
3 401 70 393.198
4 401 100 923
5 401 120 923
6 401 140 686.712
7 401 160 865.774", header = TRUE)
答案 1 :(得分:0)
我们可以使用dplyr
合并然后过滤library(dplyr)
left_join(x, y, by = c("x1" = "y2")) %>%
filter(y3 > x2) %>%
arrange(y3) %>%
group_by(x1, x2) %>%
slice(1)
# Source: local data frame [3 x 5]
# Groups: x1, x2 [3]
#
# x1 x2 y1 y3 y4
# <int> <int> <int> <int> <dbl>
# 1 401 4 1 10 22.152
# 2 401 38 2 40 167.986
# 3 401 142 7 160 865.774
x <- read.table(text = "
x1 x2
401 4
401 38
401 142", header = TRUE)
y <- read.table(text = "
y1 y2 y3 y4
1 401 10 22.152
2 401 40 167.986
3 401 70 393.198
4 401 100 923
5 401 120 923
6 401 140 686.712
7 401 160 865.774", header = TRUE)