[R] loop to subtract arrays / error

arun smartpink111 at yahoo.com
Mon Nov 19 14:22:09 CET 2012


HI,
May be this helps:

Vobsr<-read.table(text="
81.071
73.187
66.991
62.482
59.662
58.529
59.085
61.328
65.259
70.878
",sep="",header=FALSE)
Vsimr=read.table(text="
81.07 81.07
73.19 73.19
65.81 67.16
58.93 63
52.55 60.7
46.68 60.25
41.31 61.67
36.44 64.95
32.08 70.08
28.22 77.08
",sep="",header=FALSE)
 Vsimr1<-as.matrix(Vsimr)
 sapply(split(Vsimr1,col(Vsimr1)),function(x) x-as.matrix(Vobsr))
#            1      2
 #[1,]  -0.001 -0.001
 #[2,]   0.003  0.003
 #[3,]  -1.181  0.169
 #[4,]  -3.552  0.518
 #[5,]  -7.112  1.038
 #[6,] -11.849  1.721
 #[7,] -17.775  2.585
 #[8,] -24.888  3.622
 #[9,] -33.179  4.821
#[10,] -42.658  6.202

#if you are using data.frame()
res2<-sapply(Vsimr,function(x) x-Vobsr[,1])
 head(res2,3)
#         V1     V2
#[1,] -0.001 -0.001
#[2,]  0.003  0.003
#[3,] -1.181  0.169
#or just
res3<-Vsimr-Vobsr[,1]
 head(res3,3)
#      V1     V2
#1 -0.001 -0.001
#2  0.003  0.003
#3 -1.181  0.169

A.K.








----- Original Message -----
From: iembry <iruckaE at mail2world.com>
To: r-help at r-project.org
Cc: 
Sent: Sunday, November 18, 2012 8:41 PM
Subject: [R] loop to subtract arrays / error

Hi everyone, I am having trouble with creating a loop to subtract arrays.

In R, this is what I have done:
> Vobsr <- read.csv("Observed_Flow.csv", header = TRUE, sep =",") # see data
> below
> Vsimr <- read.csv("1000Samples_Vsim.csv", header = TRUE, sep =",") # see
> data below

> Vobsr <- as.matrix(Vobsr[,-1]) # remove column 1 from analysis thus Vobsr
> is 101x1 double matrix (column 1 is date information)
> Vsimr <- as.matrix(Vsimr[,-1]) # remove column 1 from analysis thus Vsimr
> is 101x1000 double matrix (column 1 is date information)

> Vobsr - Vsimr
Error in Vobsr - Vsimr : non-conformable arrays

Thus I attempted to create the loop below to perform the subtraction
operation for each of the 1000 columns.

> dim(Vsimr)[2]
[1] 1000

> for (i in 1:dim(Vsimr)[2]) {
    Xjj <- Vobsr - Vsimr[,i]
}
Xjj is a 101x1 double matrix rather than a 101X1000 double matrix

How can I subtract each column of Vsimr from the single column of Vobsr over
the 1000 columns present?

I would like to thank each of you in advance for your assistance.

How can I subtract each column of Vsimr from the single column of Vobsr over
the 1000 columns present?

I would like to thank each of you in advance for your assistance.


I am including some of the data from the files that I am operating on below:
1 column of Observed_Flow.csv
81.071
73.187
66.991
62.482
59.662
58.529
59.085
61.328
65.259
70.878
78.184
87.179
97.862
110.23
124.29
140.08
157.57
176.76
197.63
220.18
244.4
270.31
297.88
327.14
358.09
390.71
425.03
461.03
498.72
538.09
579.16
621.91
666.35
712.48
760.29
809.8
860.99
913.87
968.44
1024.7
1082.6
1142.3
1203.6
1266.6
1331.3
1397.7
1465.7
1535.5
1606.9
1680.1
1754.9
1831.4
1907.1
1981.9
2055.9
2129
2201.3
2272.7
2343.3
2413.1
2482
2550.1
2617.3
2683.7
2749.2
2813.9
2877.8
2940.8
3003
3064.3
3124.8
3184.4
3243.2
3301.1
3358.2
3414.5
3469.9
3524.4
3578.2
3631
3683.1
3734.3
3784.6
3834.1
3882.8
3930.6
3977.6
4023.7
4069
4113.4
4157
4199.8
4241.7
4282.7
4323
4362.3
4400.9
4438.6
4475.4
4511.4
4546.6


2 columns of 1000 columns of 1000Samples_Vsim.csv
81.07 81.07
73.19 73.19
65.81 67.16
58.93 63
52.55 60.7
46.68 60.25
41.31 61.67
36.44 64.95
32.08 70.08
28.22 77.08
24.86 85.94
22.01 96.65
19.65 109.23
17.8 123.67
16.46 139.96
15.61 158.12
15.27 178.14
15.43 200.02
16.1 223.75
17.27 249.35
18.94 276.81
21.11 306.13
23.79 337.31
26.97 370.34
30.65 405.24
34.84 442
39.52 480.62
44.71 521.1
50.41 563.44
56.61 607.64
63.31 653.7
70.51 701.62
78.21 751.4
86.42 803.04
95.13 856.53
104.35 911.89
114.06 969.11
124.28 1028.2
135.01 1089.1
146.23 1151.9
157.96 1216.6
170.19 1283.1
182.93 1351.5
196.16 1421.7
209.9 1493.8
224.15 1567.8
238.89 1643.6
254.14 1721.3
269.89 1800.8
286.15 1882.2
302.91 1965.5
320.17 2050.6
337.18 2134.8
353.93 2218.1
370.44 2300.4
386.69 2381.8
402.7 2462.3
418.45 2541.8
433.95 2620.4
449.2 2698.1
464.2 2774.9
478.94 2850.7
493.44 2925.6
507.68 2999.5
521.67 3072.6
535.41 3144.7
548.9 3215.8
562.14 3286.1
575.12 3355.4
587.86 3423.8
600.34 3491.2
612.57 3557.7
624.55 3623.3
636.28 3688
647.76 3751.7
658.98 3814.5
669.96 3876.4
680.68 3937.3
691.15 3997.3
701.37 4056.4
711.34 4114.6
721.06 4171.8
730.52 4228.1
739.74 4283.4
748.7 4337.9
757.41 4391.4
765.87 4443.9
774.08 4495.6
782.04 4546.3
789.74 4596.1
797.2 4644.9
804.4 4692.8
811.35 4739.8
818.05 4785.9
824.5 4831
830.7 4875.2
836.64 4918.5
842.33 4960.8
847.78 5002.2
852.97 5042.7
857.91 5082.3



--
View this message in context: http://r.789695.n4.nabble.com/loop-to-subtract-arrays-error-tp4650001.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list