我有一个包含160行和12列的数据框“DEM2”,我想创建一个新的数据框“zcp_DEM”来实现
zcb_DEM[1,i] <- 100/(100+DEM2[i,2]) ## for all i in 1:160
## for all i in 1:160 and j in 2:11
zcb_DEM[j,i] <- (100/100+DEM2[i,j])*(1-sum(zcb_DEM[1:j-1,i])*(DEM2[i,j]/100))
我的想法是使用一个或两个for循环。到目前为止,我编写了以下代码,遗憾的是这些代码似乎无法正常工作:
zcb_DEM <- matrix(nrow = 11, ncol = 160)
for (i in 1:160 ) {
zcb_DEM[1,i] <- 100/(100+DEM2[i,2])
zcb_DEM[2,i] <- (100/100+DEM2[i,2])*(1-sum(zcb_DEM[1:1,i])*(DEM2[i,2]/100))
zcb_DEM[3,i] <- (100/100+DEM2[i,3])*(1-sum(zcb_DEM[1:2,i])*(DEM2[i,3]/100))
zcb_DEM[4,i] <- (100/100+DEM2[i,4])*(1-sum(zcb_DEM[1:3,i])*(DEM2[i,4]/100))
zcb_DEM[5,i] <- (100/100+DEM2[i,5])*(1-sum(zcb_DEM[1:4,i])*(DEM2[i,5]/100))
zcb_DEM[6,i] <- (100/100+DEM2[i,6])*(1-sum(zcb_DEM[1:5,i])*(DEM2[i,6]/100))
zcb_DEM[7,i] <- (100/100+DEM2[i,7])*(1-sum(zcb_DEM[1:6,i])*(DEM2[i,7]/100))
zcb_DEM[8,i] <- (100/100+DEM2[i,8])*(1-sum(zcb_DEM[1:7,i])*(DEM2[i,8]/100))
zcb_DEM[9,i] <- (100/100+DEM2[i,9])*(1-sum(zcb_DEM[1:8,i])*(DEM2[i,9]/100))
zcb_DEM[10,i] <- (100/100+DEM2[i,10])*(1-sum(zcb_DEM[1:9,i])*(DEM2[i,10]/100))
zcb_DEM[11,i] <- (100/100+DEM2[i,11])*(1-sum(zcb_DEM[1:10,i])*(DEM2[i,11]/100))
print(zcb_DEM)
}
或更优雅
for (i in 1:160) {
for (j in 2:11) {
zcb_DEM[1,i] <- 100/(100+DEM2[i,2])
zcb_DEM[j,i] <- (100/100+DEM2[i,j])*(1-sum(zcb_DEM[1:j-1,i])*(DEM2[i,j]/100))
print(zcb_DEM)
}}
也不起作用。
编辑运行第一个代码时,我获得以下内容:
[,155] [,156] [,157] [,158] [,159] [,160] ##this is the last block appearing in the console
[1,] NA NA NA NA NA NA ##for each row there is such a NA vector appearing
[2,] NA NA NA NA NA NA
[3,] NA NA NA NA NA NA
[4,] NA NA NA NA NA NA
[5,] NA NA NA NA NA NA
[6,] NA NA NA NA NA NA
[7,] NA NA NA NA NA NA
[8,] NA NA NA NA NA NA
[9,] NA NA NA NA NA NA
[10,] NA NA NA NA NA NA
[11,] NA NA NA NA NA NA
There were 50 or more warnings (use warnings() to see the first 50)
使用warnings()
时,会出现以下(错误)
Warnmeldungen:
1: In Ops.factor(100, DEM2[i, 2]) : ‘+’ not meaningful for factors
2: In Ops.factor(100/100, DEM2[i, 2]) : ‘+’ not meaningful for factors
3: In Ops.factor(DEM2[i, 2], 100) : ‘/’ not meaningful for factors
4: In Ops.factor(100/100, DEM2[i, 3]) : ‘+’ not meaningful for factors
5: In Ops.factor(DEM2[i, 3], 100) : ‘/’ not meaningful for factors
6: In Ops.factor(100/100, DEM2[i, 4]) : ‘+’ not meaningful for factors
7: In Ops.factor(DEM2[i, 4], 100) : ‘/’ not meaningful for factors
8: In Ops.factor(100/100, DEM2[i, 5]) : ‘+’ not meaningful for factors
9: In Ops.factor(DEM2[i, 5], 100) : ‘/’ not meaningful for factors
10: In Ops.factor(100/100, DEM2[i, 6]) : ‘+’ not meaningful for factors
11: In Ops.factor(DEM2[i, 6], 100) : ‘/’ not meaningful for factors
12: In Ops.factor(100/100, DEM2[i, 7]) : ‘+’ not meaningful for factors
13: In Ops.factor(DEM2[i, 7], 100) : ‘/’ not meaningful for factors
14: In Ops.factor(100/100, DEM2[i, 8]) : ‘+’ not meaningful for factors
15: In Ops.factor(DEM2[i, 8], 100) : ‘/’ not meaningful for factors
16: In Ops.factor(100/100, DEM2[i, 9]) : ‘+’ not meaningful for factors
17: In Ops.factor(DEM2[i, 9], 100) : ‘/’ not meaningful for factors
18: In Ops.factor(100/100, DEM2[i, 10]) : ‘+’ not meaningful for factors
19: In Ops.factor(DEM2[i, 10], 100) : ‘/’ not meaningful for factors
20: In Ops.factor(100/100, DEM2[i, 11]) : ‘+’ not meaningful for factors
21: In Ops.factor(DEM2[i, 11], 100) : ‘/’ not meaningful for factors
22: In Ops.factor(100, DEM2[i, 2]) : ‘+’ not meaningful for factors
23: In Ops.factor(100/100, DEM2[i, 2]) : ‘+’ not meaningful for factors
24: In Ops.factor(DEM2[i, 2], 100) : ‘/’ not meaningful for factors
25: In Ops.factor(100/100, DEM2[i, 3]) : ‘+’ not meaningful for factors
26: In Ops.factor(DEM2[i, 3], 100) : ‘/’ not meaningful for factors
27: In Ops.factor(100/100, DEM2[i, 4]) : ‘+’ not meaningful for factors
28: In Ops.factor(DEM2[i, 4], 100) : ‘/’ not meaningful for factors
29: In Ops.factor(100/100, DEM2[i, 5]) : ‘+’ not meaningful for factors
30: In Ops.factor(DEM2[i, 5], 100) : ‘/’ not meaningful for factors
31: In Ops.factor(100/100, DEM2[i, 6]) : ‘+’ not meaningful for factors
32: In Ops.factor(DEM2[i, 6], 100) : ‘/’ not meaningful for factors
33: In Ops.factor(100/100, DEM2[i, 7]) : ‘+’ not meaningful for factors
34: In Ops.factor(DEM2[i, 7], 100) : ‘/’ not meaningful for factors
35: In Ops.factor(100/100, DEM2[i, 8]) : ‘+’ not meaningful for factors
36: In Ops.factor(DEM2[i, 8], 100) : ‘/’ not meaningful for factors
37: In Ops.factor(100/100, DEM2[i, 9]) : ‘+’ not meaningful for factors
38: In Ops.factor(DEM2[i, 9], 100) : ‘/’ not meaningful for factors
39: In Ops.factor(100/100, DEM2[i, 10]) : ‘+’ not meaningful for factors
40: In Ops.factor(DEM2[i, 10], 100) : ‘/’ not meaningful for factors
41: In Ops.factor(100/100, DEM2[i, 11]) : ‘+’ not meaningful for factors
42: In Ops.factor(DEM2[i, 11], 100) : ‘/’ not meaningful for factors
43: In Ops.factor(100, DEM2[i, 2]) : ‘+’ not meaningful for factors
44: In Ops.factor(100/100, DEM2[i, 2]) : ‘+’ not meaningful for factors
45: In Ops.factor(DEM2[i, 2], 100) : ‘/’ not meaningful for factors
46: In Ops.factor(100/100, DEM2[i, 3]) : ‘+’ not meaningful for factors
47: In Ops.factor(DEM2[i, 3], 100) : ‘/’ not meaningful for factors
48: In Ops.factor(100/100, DEM2[i, 4]) : ‘+’ not meaningful for factors
49: In Ops.factor(DEM2[i, 4], 100) : ‘/’ not meaningful for factors
50: In Ops.factor(100/100, DEM2[i, 5]) : ‘+’ not meaningful for factors
EDIT²我发现我的错误在哪里:数据框DEM2不完全是数字,我只需修复它。