簡體   English   中英

根據列表和數據框列之間的值匹配創建新數據框

[英]Create new data frame based on the match of values between a list and a column of data frame

我正在嘗試使用與舊數據框中列的值匹配的值列表來創建新數據框。 同樣對於新數據框,我想保留用於匹配的值列表中的順序。 這是我想要實現的示例:

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM