对于数据框中的每一行,循环遍历另一个数据框
For each row in a dataframe, loop through another dataframe
我需要遍历数据帧,读取三列的值(2 个时间戳和 1 个标签)。然后,对于这行三个值,我需要与第二个数据帧的每一行进行比较,以查看 A) 标签是否匹配,以及 B) 第二个数据帧中的时间戳是否在当前行的两个时间戳之间。如果该行确实符合这两个条件,则应将其保存到数据框/向量中以供进一步处理。
我已经尝试了很多版本的 x_apply 函数,结合 for 循环(用于 'second' 迭代)。下面是我的问题的一个非常简化的版本,我在其中创建了两个小数据框并尝试建立所需的循环。该值应保存到 'x' - 虽然当我在循环中打印(x)时显示此值,但在应用函数完成后 'x' 声称为 NULL。每次调用该函数时,它似乎都会被重置。考虑到我的要求,您是否有其他/更好方法的想法?我不需要使用 apply 本身。非常感谢您!
label <- c("p1", "p1", "p2")
value_1 <- c(8,4,2)
value_2 <- c(10,6,9)
df1 <- data.frame(label, value_1, value_2)
label <- c("p1", "p2", "p2")
value_3 <- c(8,8,8)
df2 <- data.frame(label, value_3)
x = NULL
small_function <- function(value_1, value_2, label) {
for(i in 1:nrow(df2[df2$label == label,])) {
print(i)
x <- append(x, i)
print(x)
}
}
apply(df1, 1, function(x,y,z) small_function(df1$value_1, df1$value_2, df1$label))
x
更新:一个带有时间日期的示例,导致我的错误 "Expecting a single value"。
label <- c("p1", "p1", "p2")
value_1 <- c(as.POSIXct(1482645600, origin="1970-01-01"),as.POSIXct(1482745600, origin="1970-01-01"),as.POSIXct(1482845600, origin="1970-01-01"))
value_2 <- c(as.POSIXct(1582645600, origin="1970-01-01"),as.POSIXct(1582745600, origin="1970-01-01"),as.POSIXct(1582845600, origin="1970-01-01"))
df1 <- data.frame(label, value_1, value_2)
label <- c("p1", "p2", "p2")
value_3 <- c(as.POSIXct(1582645100, origin="1970-01-01"),as.POSIXct(1582745200, origin="1970-01-01"),as.POSIXct(1582845300, origin="1970-01-01"))
df2 <- data.frame(label, value_3)
df_merge = merge(df1, df2, c("label"), suffixes = c(".df1",".df2"))
setDT(df_merge)
str(df_merge)
a <- df_merge[between(value_3, value_1, value_2), ]
这是您要找的吗?
library(data.table)
setDT(df1)
setDT(df2)
setkey(df1, label)
setkey(df2, label)
df1[df2] # here i merge both the data.table
df3[between(value_3, value_1, value_2), ] # apply the condition
# label value_1 value_2 value_3
#1: p1 8 10 8
#2: p2 2 9 8
#3: p2 2 9 8
数据中有一些日期:
# ensure the dates are in proper formats( i had simulated some sample data with dates. just sharing the last 2 steps output)
df3$value_1 = as.Date(df3$value_1, format= "%d/%m/%Y")
df3$value_2 = as.Date(df3$value_2, format= "%d/%m/%Y")
df3$value_3 = as.Date(df3$value_3, format= "%d/%m/%Y")
# df3
# label value_1 value_2 value_3
#1: p1 2016-03-10 2016-03-20 2016-03-15
#2: p1 2016-06-17 2016-06-19 2016-03-15
#3: p2 2016-09-10 2016-09-20 2016-06-21
#4: p2 2016-09-10 2016-09-20 2016-09-12
df3[between(value_3, value_1, value_2), ]
# label value_1 value_2 value_3
#1: p1 2016-03-10 2016-03-20 2016-03-15
#2: p2 2016-09-10 2016-09-20 2016-09-12
这是一个非常简短的基础 R 解决方案,如果这是您正在寻找的:
dfr <- merge(df1, df2, by="label", all=FALSE)
subset(dfr, value_3 > value_1 & value_3 < value_2)
我需要遍历数据帧,读取三列的值(2 个时间戳和 1 个标签)。然后,对于这行三个值,我需要与第二个数据帧的每一行进行比较,以查看 A) 标签是否匹配,以及 B) 第二个数据帧中的时间戳是否在当前行的两个时间戳之间。如果该行确实符合这两个条件,则应将其保存到数据框/向量中以供进一步处理。
我已经尝试了很多版本的 x_apply 函数,结合 for 循环(用于 'second' 迭代)。下面是我的问题的一个非常简化的版本,我在其中创建了两个小数据框并尝试建立所需的循环。该值应保存到 'x' - 虽然当我在循环中打印(x)时显示此值,但在应用函数完成后 'x' 声称为 NULL。每次调用该函数时,它似乎都会被重置。考虑到我的要求,您是否有其他/更好方法的想法?我不需要使用 apply 本身。非常感谢您!
label <- c("p1", "p1", "p2")
value_1 <- c(8,4,2)
value_2 <- c(10,6,9)
df1 <- data.frame(label, value_1, value_2)
label <- c("p1", "p2", "p2")
value_3 <- c(8,8,8)
df2 <- data.frame(label, value_3)
x = NULL
small_function <- function(value_1, value_2, label) {
for(i in 1:nrow(df2[df2$label == label,])) {
print(i)
x <- append(x, i)
print(x)
}
}
apply(df1, 1, function(x,y,z) small_function(df1$value_1, df1$value_2, df1$label))
x
更新:一个带有时间日期的示例,导致我的错误 "Expecting a single value"。
label <- c("p1", "p1", "p2")
value_1 <- c(as.POSIXct(1482645600, origin="1970-01-01"),as.POSIXct(1482745600, origin="1970-01-01"),as.POSIXct(1482845600, origin="1970-01-01"))
value_2 <- c(as.POSIXct(1582645600, origin="1970-01-01"),as.POSIXct(1582745600, origin="1970-01-01"),as.POSIXct(1582845600, origin="1970-01-01"))
df1 <- data.frame(label, value_1, value_2)
label <- c("p1", "p2", "p2")
value_3 <- c(as.POSIXct(1582645100, origin="1970-01-01"),as.POSIXct(1582745200, origin="1970-01-01"),as.POSIXct(1582845300, origin="1970-01-01"))
df2 <- data.frame(label, value_3)
df_merge = merge(df1, df2, c("label"), suffixes = c(".df1",".df2"))
setDT(df_merge)
str(df_merge)
a <- df_merge[between(value_3, value_1, value_2), ]
这是您要找的吗?
library(data.table)
setDT(df1)
setDT(df2)
setkey(df1, label)
setkey(df2, label)
df1[df2] # here i merge both the data.table
df3[between(value_3, value_1, value_2), ] # apply the condition
# label value_1 value_2 value_3
#1: p1 8 10 8
#2: p2 2 9 8
#3: p2 2 9 8
数据中有一些日期:
# ensure the dates are in proper formats( i had simulated some sample data with dates. just sharing the last 2 steps output)
df3$value_1 = as.Date(df3$value_1, format= "%d/%m/%Y")
df3$value_2 = as.Date(df3$value_2, format= "%d/%m/%Y")
df3$value_3 = as.Date(df3$value_3, format= "%d/%m/%Y")
# df3
# label value_1 value_2 value_3
#1: p1 2016-03-10 2016-03-20 2016-03-15
#2: p1 2016-06-17 2016-06-19 2016-03-15
#3: p2 2016-09-10 2016-09-20 2016-06-21
#4: p2 2016-09-10 2016-09-20 2016-09-12
df3[between(value_3, value_1, value_2), ]
# label value_1 value_2 value_3
#1: p1 2016-03-10 2016-03-20 2016-03-15
#2: p2 2016-09-10 2016-09-20 2016-09-12
这是一个非常简短的基础 R 解决方案,如果这是您正在寻找的:
dfr <- merge(df1, df2, by="label", all=FALSE)
subset(dfr, value_3 > value_1 & value_3 < value_2)