如何根据另一个数据框中的值在一个数据框中定义计算? [英] How to define calculations in a data frame depending on values from another data frame?
问题描述
在第一行[1,]以秒为单位的时间中,每一行都是从不同位置收集的样本。数据帧A的示例:
#V1 V2 V3 V4
#[1,] 26.4 26.5 26.6 26.7
#[2,] -15 -5 2 3
#[3,] 6 -7 5 8
#[4,] 9 4 4 -2
在另一个数据帧(B)中,我存储了应该开始对A中的每一行进行计算的时间。数据框B:
#时间
#[1,] 26.4
#[2,] 26.6
#[3,] 26.5
让我们简化一下,系数是数据的总和在一个地方(数据框A)收集数据,具体取决于收集时间(数据框B)。对于上面的示例,计算应如下所示:
sum1 = -15 +(-5)+ 2 + 3
sum2 = 5 + 8
sum3 = 4 + 4 +(-2)
我想存储在新数据框中的计算结果如下:
#Sum
#[1,] -15
#[2,] 13
#[3,] 6
如何根据存储在第二个数据帧中的值在两个数据帧之间链接计算?
使用 sapply
进行迭代的解决方案,具体取决于时间集合:
#原始表中的时间
foo<-df1 [1,]
#时间从表B中删除
时间<-c(26.4,26.6,26.5)
#从原始表
中删除时间行
df1 <-df1 [-1,]
#遍历并选择foo> = time
sapply(1:length(time),function(x)
sum(df1 [x,which(foo> = time [x])])
)
#[1] -15 13 6
I have to calculate a coefficient based on a dataset stored in a data frame (A) of size 4936 obs. x 1025 var.
In a first row [1,] time in seconds is presented, each row is a sample collected from a different place. A sample of the data frame A:
# V1 V2 V3 V4
# [1,] 26.4 26.5 26.6 26.7
# [2,] -15 -5 2 3
# [3,] 6 -7 5 8
# [4,] 9 4 4 -2
In another data frame (B) I stored the time from which I should start my calculations for each row in A. An example of the data frame B:
# time
# [1,] 26.4
# [2,] 26.6
# [3,] 26.5
Let's simplify that the coefficient is a sum of the data collected in one place (data frame A), depending on the time of their collection (data frame B). For the example above, the calculation should work like this:
sum1=-15+(-5)+2+3
sum2=5+8
sum3=4+4+(-2)
The results of the calculations I would like to store in a new data frame, which would look like this:
# Sum
# [1,] -15
# [2,] 13
# [3,] 6
How to link the calculations between the two data frames depending on a value stored in the second data frame?
Solution using sapply
to iterate and select columns depending on time of collection:
# Time from original table
foo <- df1[1, ]
# Time from table B
time <- c(26.4, 26.6, 26.5)
# Remove time row from original table
df1 <- df1[-1, ]
# Iterate over and select columns with foo >= time
sapply(1:length(time), function(x)
sum(df1[x, which(foo >= time[x])])
)
# [1] -15 13 6
这篇关于如何根据另一个数据框中的值在一个数据框中定义计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!