Chingiz
Chingiz

Reputation: 13

Assign values depending on two conditions (non-trivial)

I have quite simple method which assigns values for my list in following way: 1. It goes to the folder with files 2. Checks for the presence of file with the bank number I set 3. If it is there, it takes data from it for a particular date, again which I set in this method Here is the method:

balancesheet.list <- getBalanceSheet_list("2016-04-01",1481)

So in this particular example it goes to the folder, takes the file with number 1481 and assigns data for date 2016-04-01 from this file to my list.

I need to assign data from all files in this folder for each date I need. Data concerning each particular date and bank number should be recorded in separate sublist or just separate list. In more details, I need to assign data starting from 2007-04-01 and up to 2017-01-01 by months for each bank number. I've created two csv files: one with date range I need by months:

2007-02-01
2007-03-01
2007-04-01
2007-05-01
2007-06-01
...
2016-11-01
2016-12-01
2017-01-01

and second with bank numbers I have in general:

1
2
21
52
53
55
...
3525
3527
3528
3529
3533

My idea was to import those files in R and put them instead "2016-04-01" and 1481. But this is poor idea as I saw.

Also I decided to try following code, not importing any files:

balancesheet.list<- getBalanceSheet_list(seq(as.Date("2007-04-01"), as.Date("2017-01-01"), by="months"),1481) 

R gives an error: length of argument "pattern" is >1, only first element will be used

As I understand the problem is that it tries to assign data from multiple months in 1 list. May be it is possible to say it to record data by lists or sublists for each separate date?

How any of two ideas can be modernised? Please, help.

Upvotes: 0

Views: 27

Answers (1)

Łukasz Deryło
Łukasz Deryło

Reputation: 1860

You may try

lapply(seq(as.Date("2007-04-01"), as.Date("2017-01-01"),by="months"),getBalanceSheet_list,1481)

This should give you data from file 1481 for all the dates. I say 'sholud' because it's hard to be sure, when you do not provide any details about getBalanceSheet_list function ;)

No we can make a funtion

one_file <- function(file) lapply(seq(as.Date("2007-04-01"), as.Date("2017-01-01"),by="months"),getBalanceSheet_list, file)

And execute it for all the files. But first we need a vector with their names, say:

files<-c(1, 2, 21, ...)

Obviously, you have to change ... into numers. I can't do that, beacuse (again) you did not provide them :)

Now

lapply(files, one_file)

sholud give you all the informations you need.

You may also replace all the lapply's with sapply's. This would make results look prettier if only getBalanceSheet_list returns some pretty object (like vector or single value).

Upvotes: 1

Related Questions