Nikitau
Nikitau

Reputation: 371

Looping to group data columns

I'm relatively new to R. I've been trying to experiment with looping through the for() argument.

I have dummy data sets that follow this pattern:

Date    EUR HIGH    EUR LOW EUR CLOSE   JPY CURNCY  JPY LOW JPY CLOSE   GBP CURNCY  GBP CURNCY  GBP CURNCY
8/30/2016   1.12    1.12    1.12    102.56  101.76  102.50  1.31    1.31    1.31
8/29/2016   1.12    1.12    1.12    102.39  101.84  101.92  1.32    1.32    1.32
8/28/2016   1.13    1.12    1.12    101.94  100.06  101.84  1.33    1.33    1.33
8/27/2016   1.13    1.12    1.12    101.94  100.06  101.84  1.33    1.33    1.33
8/26/2016   1.13    1.12    1.12    101.94  100.06  101.84  1.33    1.33    1.33
8/25/2016   1.13    1.13    1.13    100.62  100.30  100.53  1.33    1.33    1.33
8/24/2016   1.13    1.12    1.13    100.61  100.10  100.45  1.33    1.33    1.33
8/23/2016   1.14    1.13    1.13    100.39  99.94   100.24  1.32    1.32    1.32
8/22/2016   1.13    1.13    1.13    100.93  100.21  100.33  1.32    1.32    1.32
8/21/2016   1.14    1.13    1.13    100.46  99.88   100.22  1.32    1.32    1.32
8/20/2016   1.14    1.13    1.13    100.46  99.88   100.22  1.32    1.32    1.32
8/19/2016   1.14    1.13    1.13    100.46  99.88   100.22  1.32    1.32    1.32

Since I have various Excel files that follows this pattern organization (i.e: price low, high and close), I'm trying to create a function that would merge the three variables (low, high, close) and the date into a table. The idea behind this is that I would like to have a separate table generated for each individual currency so that a separate analysis can be done for each individual currency and then have it loop through all the currencies, if that makes sense.

So ideally, I would want R to generate a table like:

Date   Euro High   Euro Close  Euro low
1/1    1.15        1.13        1.12
1/2    1.15        1.13        1.12
1/3    1.15        1.13        1.12

And then I'll have R do some basic analysis, and then have it repeat the process for the next variable (GBP, JPY). I'm having a little trouble on the grouping part.

My code is incredibly wrong (and also probably a major eyesore), but it might be useful in pointing out improvements/errors:

Data.Prep = function(x){ 
for(i in seq(1))
 {
   dates = as.Date(x[,i], format="%m/%d/$Y")
 }

for(i in seq(2, length(x)))
 {
   x[,i] = as.numeric(as.character(x[,i]))
 }

for(i in seq(2, length(x), 3))  
 {
  x1 = x[i:(i-1)]
 }

}

x1 returns:

   GBP.CURNCY JPY.CLOSE
1      1.3120    102.50
2      1.3172    101.92
3      1.3279    101.84
4      1.3279    101.84
5      1.3279    101.84
6      1.3264    100.53
7      1.3273    100.45
8      1.3211    100.24
9      1.3157    100.33
10     1.3185    100.22
11     1.3185    100.22
12     1.3185    100.22

Upvotes: 0

Views: 53

Answers (1)

Chirayu Chamoli
Chirayu Chamoli

Reputation: 2076

You could do this, but you should which currency to feed. Here i feed a vector of currency which would then retrieve the column indices. Then we would could just do a standard sub-setting.

vec=c('EUR', 'JPY', 'GBP' )
i=sapply(vec, function(y) grep(y,names(x)), USE.NAMES = F)
apply(i,2, function(i) x[c(1,i)] )

Upvotes: 3

Related Questions