我正在尝试合并两个数据帧,以便按其索引号匹配称为export async function sendMessage({userID, chatRoomID, message}): Promise<Message>{
// check if user is in that chat room
const user = await User.findOne(
{where: {id: userID},
include: [
{
model: ChatRoom,
where: {id: chatRoomID},
required: true
},
]
});
// if user is in that chat room, create the message
if(user?.chatRooms){
return Message.create({message, seen: false, chatRoomID, userID});
}
throw new Error(`User ${userID} is not in chat room ${chatRoomID}`);
}
的向量中的值。我正在寻找一种直观的vlookup函数。
date(date_id) BETWEEN date_add('year', -1, date_trunc('year', current_date)) AND date_add('day', -1, date_trunc('year', current_date))
我想要的是一个看起来像这样的数据框
vec
到目前为止,我能够进行第一次合并
vec=c(-2,-5)
vec2=c(11,17)
y1=as.data.frame(cbind(0:(length(c(vec))-1)*2+1,c(vec)))
V1 V2
1 1 -2
2 3 -5
y2=as.data.frame(cbind(0:(length(c(m$coefficients))-1)*2+2,c(vec2)))
V1 V2
1 2 11
2 4 15
x=as.data.frame(1:4,names="V1"); names(x)="V1"
V1
1 1
2 2
3 3
4 4
但是不能用
V1 V2
1 1 -2
2 2 11
3 3 -5
4 4 17
答案 0 :(得分:1)
您可以先rbind
y1
和y2
,然后再merge
和x
。
merge(x, rbind(y1, y2), by = 'V1')
# V1 V2
#1 1 -2
#2 2 11
#3 3 -5
#4 4 15
使用dplyr
的相同逻辑:
library(dplyr)
bind_rows(y1, y2) %>% inner_join(x, by = 'V1')
答案 1 :(得分:0)
另一个选择是rbindlist
中的data.table
library(data.table)
rbindlist(list(y1, y2)[x, on = .(V1)]