I have a problem a little complicate. I have two database BD and BD2 . For every row in BD I want to search in entire BD2 and obtain some info: Sum, Mean, Sd etc. With Sum I succeeded to make it work, but for Mean, Sd, Median I want to obtain the vector and after that apply these functions.
To be clearer, here is the code. For sum it worked. But I want now to save in a vector the values that met the conditions and after calculate Mean , Sd , Median . I tried to make the vector out of the base BD but nothing work or is something trickie and I can't figure it out.
for (i in 1:dim(BD)[1])
{
for (j in 1:dim(BD2)[1])
{
if((BD$Start.Date[i]<BD2$X_TIMESTAMP[j]) && (BD2$X_TIMESTAMP[j]<BD$End.Date[i]) && (BD$Linea[i]==BD2$Linea[j]))
{
vl = BD2$X_VALUE[j]
BD$vec[i] = paste(BD$vec[i],vl,sep="")
#vect = as.numeric(BD$vec[i])
BD$Sum[i] = BD$Sum[i]+ BD2$X_VALUE[j]
#BD$Average[i] = mean(vect)
}
}
}
I tried to create vec before the for sentence. But still it doesn't work. The error is always the same: Error in $<-.data.frame(*tmp*, "vec", value = list(NULL, NULL, NULL, : replacement has 47 rows, data has 530
EDIT: I added a photo with details. In fact, I don't necessarily want to retain the vector. I jus want to be able to calculate Mean, Sd etc.
Thank you
EDIT 2: For a reproducible example:
BD2
| X_TIMESTAMP |
X_VALUE |
Linea |
| 23/05/2020 05:40 |
106 |
1 |
| 23/05/2020 05:40 |
100 |
3 |
| 23/05/2020 05:41 |
82 |
1 |
| 23/05/2020 05:41 |
101 |
3 |
| 23/05/2020 05:42 |
109 |
1 |
| 23/05/2020 05:42 |
100 |
3 |
| 23/05/2020 05:43 |
104 |
1 |
| 23/05/2020 05:43 |
56 |
3 |
| 23/05/2020 05:44 |
104 |
1 |
| 23/05/2020 05:44 |
56 |
3 |
| 23/05/2020 05:45 |
56 |
1 |
| 23/05/2020 05:45 |
104 |
3 |
| 23/05/2020 05:46 |
0 |
1 |
BD
| Start.Date |
End.Date |
Linea |
Vector |
Sum |
Mean |
| 23/05/2020 05:40 |
23/05/2020 05:43 |
1 |
82;109 |
191 |
95.5 |
| 24/05/2020 05:41 |
24/05/2020 05:45 |
3 |
100;56;56 |
212 |
70.66 |