2

I am attempting to create a new dataframe based on the results from each iteration of a for loop in R. I have a "weekly_df" which has weekly values for multiple columns. I would like to create a new dataframe which contains the values from each column (based on the iteration number) along with the date columns (Year and Week, which are always the last two columns).

Currently, I have the following data columns (which have numeric values for each row)...

A1 Score, A1 Volume, A2 Score, A2 Volume, A3 Score, A3 Volume, Year, Week

So essentially I want to create a new dataframe for each column up until 'Year'...

A1 Score, Year, Week

A1 Volume, Year, Week

So on and so fourth, but I am unsure how to loop my data to create a new dataframe for each of these. I currently have the following...

for (i in 1:(length(names(weekly_df)) -2)) {
print(weekly_df[,c(i,((length(names(weekly_df))) -1), (length(names(weekly_df))))]) }

This prints the correct 'tibble' for each of the dataframes that I want but I'm unsure how to transform these into saved dataframes in my environment to be used later on.

Here is a reproducible dataset:

A1 Score, A1 Volume, A2 Score, A2 Volume, A3 Score, A3 Volume, Year, Week

1, 2, 3, 4, 5, 6, 2016, 1

7, 8, 9, 10, 11, 12, 2016, 2

13, 14, 15, 16, 17, 18, 2016, 3

And my desired output is each of the following as a dataframe in my environment:

A1 Score, Year, Week

1, 2016, 1

7, 2016, 2

13, 2016, 3

A1 Volume, Year, Week

2, 2016, 1

8, 2016, 2

14, 2016, 3

A2 Score, Year, Week

3, 2016, 1

9, 2016, 2

15, 2016, 3

... so and so fourth as to create a dataframe from each column.

Community
  • 1
  • 1
Trevor Ferree
  • 45
  • 1
  • 5
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Creating a bunch of different data.frame variables can be difficult to work with in R. It's probably better to create a list of data.frames. They are easier to create and work with. – MrFlick May 15 '20 at 05:09
  • I have included a reproducible dataset and my desired output. – Trevor Ferree May 15 '20 at 05:21

1 Answers1

1

You can use :

n <- ncol(weekly_df)
list_df <- lapply(names(weekly_df)[-c(n-1, n)], function(x) 
               cbind(weekly_df[x], weekly_df[c(n-1, n)]))

This will return you a list of dataframes, usually it is better to keep data in a list since it is easier to manage and avoids creating lot of variables in the global environment but if you want them as separate dataframes, you can name the list and use list2env.

list_df <- paste0('data', seq_along(list_df))
list2env(list_df, .GlobalEnv)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213