我正在尝试使用与旧数据框中的列值匹配的值列表来创建新的数据框。同样对于新数据框,我想保留用于匹配的值列表中的顺序。这是我要实现的示例:
#A list of values used for matching
time.new <- c(2, 3, 4, 3, 4, 5, 4, 5, 6)
#The old data frame which I would match on the column of **time.old**
old <- data.frame(time.old=1:10, y=rnorm(10))
time.old y
1 0.20320
2 -0.74696
3 -0.73716
4 -0.61959
5 1.12733
6 2.58322
7 -0.08138
8 -0.10436
9 -0.13081
10 -1.20050
#Here is the expected new data frame
time y
2 -0.74696
3 -0.73716
4 -0.61959
3 -0.73716
4 -0.61959
5 1.12733
4 -0.61959
5 1.12733
6 2.58322
尝试使用dplyr的left_join。首先,将time.new转换为数据框的列:
library(tidyverse)
time.new <- c(2, 3, 4, 3, 4, 5, 4, 5, 6)
#The old data frame which I would match on the column of **time.old**
old <- data.frame(time.old=1:10, y=rnorm(10))
time.new <- data.frame(time=time.new)
new_dataframe <- left_join(time.new, old, by=c("time"="time.old"))
在基本R中使用合并:
merge(x = time.new, y = old, by.x = "time", by.y="time.old", all.x = TRUE)
如果要保留time.new的顺序,则需要在数据中添加一个辅助行号列,合并,按行号排序并删除id列:
time.new <- c(2, 3, 4, 3, 4, 5, 4, 5, 6)
old <- data.frame(time.old=1:10, y=rnorm(10))
time.new <- data.frame(id = 1:length(time.new), time=time.new)
new_dataframe <- merge(x = time.new, y = old, by.x = "time", by.y="time.old", all.x = TRUE)
new_dataframe <- new_dataframe[order(new_dataframe$id), ]
new_dataframe$id <- NULL
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句