0

I am a novice to R and have the following problem:

Data Input:

I have a CSV with two columns like this:

1,10
2,20
3,30
4,40
5,50
...
N, M

Problem:

a) Read in the CSV
b) Convert to a matrix
c) Take 4 rows starting with the first row
d) calculate the third column of elements as a simple square of the second column elements
e) Calculate the sumproduct (value11*value12*value13 + value21*value22*value23 + value31*value32*value33 +value41*value42*value43)
f) Do this until the end of the dataset

Outcome:

The result for the first two matrices should be as follows:

First matrix:

Start with the first row, select four rows.

Step 1

1,10
2,20
3,30
4,40

Step 2 (add the 3rd column as a square of the second column value)

1,10,100
2,20,400
3,30,900
4,40,1600

Step 3 (calculate the sumproduct) (1*10*100 + 2*20*400 +3*30*900 + 4*40*1600) == 354.000

Second matrix:

Proceed with the second row and take the next 4 rows

Step 1

2,20
3,30
4,40
5,50

Step 2 (add the 3rd column as a square of the second column value)

2,20,400
3,30,900
4,40,1600
5,50,2500

Step 3 (calculate the sumproduct)

(2*20*400 +3*30*900 + 4*40*1600 + 5*50*2500) == 978.000

N matrix Do steps 1 to 3 until the end of the dataset

RESULT

The result should be the list of sumproducts:

354.000, 978.000, ..., N

Thank you!

user1766682
  • 400
  • 3
  • 14
  • I do not believe that you are unable to solve at least some checkpoints in your problem description by doing some research, eg. reading basic introductions and tutorials. – Roland Apr 22 '13 at 09:16
  • Hi Roland, I solved some of the checkpoints. My problem is how to define the four row chunks of the data as a matrix and do it until the end of the dataset. I event managed to do it with a For loop but I think there should be a better way to do it as a simple loop. – user1766682 Apr 22 '13 at 09:24
  • You're close. Think about how you would subset your rows based on an index. You can probably do this in a number of ways, but a `for` loops would be one intuitive approach. – Roman Luštrik Apr 22 '13 at 09:37

0 Answers0