-2

I have over 200 CSV files. Each file has 150+ columns and 1000s of rows. Each file is named by product name. Each file provides dataset with a flag for different items for various categories in different columns. One of the columns is total usage for each item across the category. Below is sample of the dataset:

    Values   A B C
    1        Y   
    2          Y
    3        Y   Y 
    4            Y 

I had asked a question yesterday, and I received help regarding how to get the following results.

     Count  Sum
A      2     4
B      1     2
C      2     7

Basically I want the Count Column to give me the number of "y" for A, B, and C, and the Sum column to give me sum from the Usage column for each time there is a "Y" in Columns A, B, and C

Now I have question on Step 2 - I have brought all the files into a folder. What I would like to do is use the above results, apply it in each of the file, and then have the answer grouped by file and category. For example

File 1 Count A Sum A Count B Sum B Count C Sum C
File 2 Count A Sum A Count B Sum B Count C Sum C and so on. 

How can I do this in R? I have searched the forum, and found loop to be helpful, but I am not sure how I can combine the results into one.

jalsa
  • 101
  • 2
  • 2
  • 7

1 Answers1

0

It really helps if you share a reproducible example and if its a follow on question a link to the original question.

Here is some useful info for you which should get you started and then when you have a specific stumbling block I suggest you redo the question.

Looping over files

filenames = dir(pattern="*01.csv")
for( i in 1:length(filenames) )
{
  ...

Merging data

This is really not a good idea, but if you wanted to do it this way, I guess you can try:

for (i in 1:10) {
  df <- rbind(df, data.frame(x = i, y = toString(i)))
}

Note that in your code, there is one other problem:

You should use stringsAsFactors if you want the characters to not get converted to factors. Use: df = data.frame(x = numeric(), y = character(), stringsAsFactors = FALSE)

Community
  • 1
  • 1
gtwebb
  • 2,981
  • 3
  • 13
  • 22
  • I tried the looping over files, but I am missing something. When I 'run' it, it does looks like it is doing something, but I am not seeing any results. Also, how do i have the results returned by each file? – jalsa Apr 13 '16 at 17:53