[英]How to assign one dataframe column's value to be the same as another column's value in r?
[英]how to compare dates in different data frames and assign values to a same column in one dataframe of a same column in another dataframe?
我有一個示例數據框,如下所示
Dataframe1。
general_id date
6 2000-01-02 16:57:13
2 2000-01-02 19:26:13
3 2000-01-04 13:30:13
2 2000-01-04 19:03:13
7 2000-01-06 16:32:13
Dataframe2。
general_id date
1 2000-01-02 16:57:12
1 2000-01-06 16:57:12
1 2000-01-02 19:26:12
1 2000-01-02 19:26:12
1 2000-01-04 13:30:12
1 2000-01-04 13:30:12
1 2000-01-04 19:03:12
1 2000-01-04 19:03:12
1 2000-01-06 16:32:12
數據幀的兩個日期列只有第二個差異。 我想比較兩個數據框的日期列,並將general_id
列的值分配給Dataframe2中的general_id
列
date1 <- Dataframe1$date-dsecond(1)
date2 <- Dataframe1$date
if(date1==date2){
dataframe2$general_id=dataframe1$general_id
}
但我得到這個錯誤,
In if (date1 == date2) the condition has length > 1 and only the first element will be used
期望的輸出是:
Dataframe1
general_id date
6 2000-01-02 16:57:13
2 2000-01-02 19:26:13
3 2000-01-04 13:30:13
2 2000-01-04 19:03:13
7 2000-01-06 16:32:13
Dataframe2
general_id date
6 2000-01-02 16:57:12
6 2000-01-06 16:57:12
2 2000-01-02 19:26:12
2 2000-01-02 19:26:12
3 2000-01-04 13:30:12
3 2000-01-04 13:30:12
2 2000-01-04 19:03:12
2 2000-01-04 19:03:12
7 2000-01-06 16:32:12
7 2000-01-06 16:32:12
您要在此處執行的操作稱為join
,特別是您希望left_join
df2與df1,因此您將所有行保留在df2中,然后添加df1中的匹配列。
要了解有關連接以及如何在R中使用它們的更多信息,請閱讀此相關問題: 如何連接(合並)數據框架(內部,外部,左側,右側)
這里的復雜因素是date
列關閉了一秒鍾。 要使用它,我們只需要在加入之前使用lubridate::dseconds
修改date
。
首先,我們把你的數據並確保date
格式為POSIXct
使用lubridate::as_datetime
所以我們可以使用它作為一個日期。
這將根據您的數據為我們提供以下數據框:
df1 <- structure(list(general_id = c(6L, 2L, 3L, 2L, 7L), date = structure(c(946832233,
946841173, 946992613, 947012593, 947176333), class = c("POSIXct",
"POSIXt"), tzone = "UTC")), row.names = c(NA, -5L), class = "data.frame")
df2 <- structure(list(general_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), date = structure(c(946832232, 947177832, 946841172, 946841172,
946992612, 946992612, 947012592, 947012592, 947176332), class = c("POSIXct",
"POSIXt"), tzone = "UTC")), row.names = c(NA, -9L), class = "data.frame")
現在我們可以進行join
,但請注意我們使用dplyr::mutate
修改連接中的df1$date
:
library(dplyr)
left_join(df2, mutate(df1, date = date - lubridate::dseconds(1)), by = 'date')
general_id.x date general_id.y
1 1 2000-01-02 16:57:12 6
2 1 2000-01-06 16:57:12 NA
3 1 2000-01-02 19:26:12 2
4 1 2000-01-02 19:26:12 2
5 1 2000-01-04 13:30:12 3
6 1 2000-01-04 13:30:12 3
7 1 2000-01-04 19:03:12 2
8 1 2000-01-04 19:03:12 2
9 1 2000-01-06 16:32:12 7
如您所見,我們已從df1
添加了相應的general_id
列。 然后我們可以刪除general_id.x
並根據需要重命名general_id.y
。 請注意,第2行返回NA
因為它在df1
中沒有匹配(時間匹配,但日期不同)
以下代碼檢查日期列中日期之間的時間差小於2秒。 為了使它僅在一個方向上恰好匹配1秒的時間差,改變which
語句。
for (i in 1:nrow(Dataframe2)) {
corresponding_row <- which(abs(as.POSIXct(Dataframe1$date)-as.POSIXct(Dataframe2$date[i]))<2)
message('row ', i, ' of Dataframe2 corresponds to row ', corresponding_row, ' of Dataframe1')
Dataframe2$id[i] <- ifelse(length(corresponnding_row), Dataframe1$id[corresponding_row], NA)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.