1

There are few post related to this question but none could help me to solve it. for example How to split a data frame? How to split a data frame into multiple data frames based on columns

What I want is to split the data based on different column number, for example 1, then I must have as many columns as a data frame has. 2, I must have half of a data frame data (if it is even)

An example data is below

fredTable <- structure(list(Symbol = structure(c(3L, 1L, 4L, 2L, 5L), .Label = c("CASACBM027SBOG", 
"FRPACBW027SBOG", "TLAACBM027SBOG", "TOTBKCR", "USNIM"), class = "factor"), 
    Name = structure(1:5, .Label = c("bankAssets", "bankCash", 
    "bankCredWk", "bankFFRRPWk", "bankIntMargQtr"), class = "factor"), 
    Category = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Banks", class = "factor"), 
    Country = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "USA", class = "factor"), 
    Lead = structure(c(1L, 1L, 3L, 3L, 2L), .Label = c("Monthly", 
    "Quarterly", "Weekly"), class = "factor"), Freq = structure(c(2L, 
    1L, 3L, 3L, 4L), .Label = c("1947-01-01", "1973-01-01", "1973-01-03", 
    "1984-01-01"), class = "factor"), Start = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = "Current", class = "factor"), End = c(TRUE, 
    TRUE, TRUE, TRUE, FALSE), SeasAdj = c(FALSE, FALSE, FALSE, 
    FALSE, TRUE), Percent = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Fed", class = "factor"), 
    Source = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Res", class = "factor"), 
    Series = structure(c(1L, 1L, 1L, 1L, 2L), .Label = c("Level", 
    "Ratio"), class = "factor")), .Names = c("Symbol", "Name", 
"Category", "Country", "Lead", "Freq", "Start", "End", "SeasAdj", 
"Percent", "Source", "Series"), row.names = c("1", "2", "3", 
"4", "5"), class = "data.frame")

I want to split it to 12 data frames and name them case 1 to 12.

so my first data frame is case1

case_1
TLAACBM027SBOG
CASACBM027SBOG
TOTBKCR
FRPACBW027SBOG
USNIM

the second data frame is case 2

case_2
bankAssets
bankCash
bankCredWk
bankFFRRPWk
bankIntMargQtr

etc.

In case I set the number to 2, i should have 6 columns (pairs of 2) with name as cas_1, case_2 until case_12

I know that split function is used to split the data but I could not figure out how to do it

for first case I did like

split(fredTable, 1:ncol(fredTable))
Community
  • 1
  • 1

1 Answers1

1

We can use

lst <-  setNames(lapply(seq_along(fredTable),
      function(i) fredTable[i]), 
         paste('case', seq_along(fredTable), sep="_"))

list2env(lst, envir=.GlobalEnv)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • thanks, can you please complete your answer. this print me the results, I want to have them in workspace with specific name as I mentioned above , for example for each column , case 1 to 12 – koskesh kiramtodahanet Feb 26 '16 at 12:27
  • @koskeshkiramtodahanet We can use `setNames` to do this. Please check the update – akrun Feb 26 '16 at 12:28
  • I checked thanks, it is not giving me 12 separated data frame but only one, maybe I cannot convey correctly my question. I would like to have as many columns as a data frame , data frame with specific names saved in "Environment" so when I invoke case_1, I only see that column – koskesh kiramtodahanet Feb 26 '16 at 12:31
  • @koskeshkiramtodahanet The above gives the results in a `list`, but if you want separate data.frames, we can use `list2env` (but I won't recommend to do that). – akrun Feb 26 '16 at 12:33
  • Yes it works ! can you please tell me why you don't recommend? and also please tell me how you code ????? I just posted the question and in few second I got the answer !!!!!! – koskesh kiramtodahanet Feb 26 '16 at 12:35
  • @koskeshkiramtodahanet It is not recommended as you will be having a lot of objects in the global environment where as in a single `list` you can hold all those objects. Inside the `list`, almost all the operations can be done and then we can also save it. – akrun Feb 26 '16 at 12:36
  • but then it is the same as the original data not ? just it convert it to a list file ! (sorry for stupid question but I don't understand the differences) – koskesh kiramtodahanet Feb 26 '16 at 12:39
  • @koskeshkiramtodahanet Just to understand the aim, why do you need the columns as separate dataframes? – akrun Feb 26 '16 at 12:40
  • I want to apply a function on each of the columns. and then save the results , I tried to get the function without splitting the data using apply , but did not work . the name of my function is "dahanet" – koskesh kiramtodahanet Feb 26 '16 at 12:42
  • 1
    @koskeshkiramtodahanet If you want a particular function on each of the columns try `lapply(fredTable, dahanet)` (assuming that the function works on all the column types because you have `factor` and `logical` class columns) – akrun Feb 26 '16 at 12:43
  • 1
    thanks I will have it checked . I accepted and liked your answer . – koskesh kiramtodahanet Feb 26 '16 at 12:45
  • 1
    @koskeshkiramtodahanet Thank you for the feedback. – akrun Feb 26 '16 at 12:45