I have the following dataframes DF1
and DF2
. I am trying to fill DF2
with values from the Close
column in DF1
. But it turns out that the output is zero when running the loop. Don't know what is wrong, but it seems it doesn't read the Asset
column values.
DF1:
Data Asset Close
1 1986-11-27 ABC 6 5.95
2 1986-12-01 ABC 6 5.90
3 1986-12-03 ABC 6 5.90
4 1986-12-04 ABC 6 5.90
5 1986-12-05 ABC 6 5.00
6 1986-12-08 ABC 6 5.00
7 1986-12-09 ABC 6 4.78
8 1986-10-31 ABC 8 3.90
9 1986-11-03 ABC 8 3.70
10 1986-11-04 ABC 8 3.70
. . . .
. . . .
DF2:
ABC 6 ABC 8
1986-11-27 NA NA
1986-12-01 NA NA
1986-12-03 NA NA
1986-12-04 NA NA
1986-12-05 NA NA
1986-12-08 NA NA
1986-12-09 NA NA
1986-12-10 NA NA
1986-12-11 NA NA
1986-12-12 NA NA
. . .
. . .
for (i in 1:length(DF2))
{
for (m in 1:nrow(DF2))
{
for (n in 1:nrow(DF1))
{
if ((names(DF2[i]) == DF1[n,2]) & (row.names(DF2[m,0])==as.character(DF1[n,1])))
{
DF2[m,i] <- DF1[n,3]
} else{DF2[m,i] <- 0}
}
}
}
Output:
ABC 6 ABC 8
1986-11-27 0 0
1986-12-01 0 0
1986-12-03 0 0
1986-12-04 0 0
1986-12-05 0 0
1986-12-08 0 0
1986-12-09 0 0
1986-12-10 0 0
1986-12-11 0 0
1986-12-12 0 0
. . .
. . .