質問

I have a large number of files that I want to separately run read.transactions() on (part of the ARULES package).

I'd like to do something like this:

x_1 = read.transactions(file_1.csv,...); rules_1 = apriori(x_1,...);

x_2 = read.transactions(file_2.csv,...); rules_2 = apriori(x_1,...);

...

x_N = read.transactions(file_N.csv,...); rules_1 = apriori(x_N,...);

So far I've just been copying out the code. Obviously, it seems faster to me to do this within a loop. The source data will already be formatted, sorted, and in the same place, so I'm not worried about issues involving multiple directories.

I'm new to R, so I'm not totally sure how to structure this. If there's another similar question (just concerning looping) which would do the job, then I'd be happy to be pointed in the right direction.

Thanks.

役に立ちましたか?

解決

Something like

filenames <- paste0("file_",1:N,".csv")  
## or maybe list.files(pattern="^file_.*.csv"))
rulesList <- lapply(filenames,
                 function(x) apriori(read.transactions(x,...),...))

which will return a list of rules you can access. Or:

transList <- lapply(filenames,read.transactions,...)
rulesList <- lapply(transList,apriori,...)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top