3

I'd like to split a dataframe in 4 equals parts, because I'd like to use the 4 cores of my computer.

I did this :

df2 <- split(df, 1:4)
unsplit(df2, f=1:4)

and that

df2 <- split(df, 1:4)
unsplit(df2, f=c('1','2','3','4')

But the unsplit function did not work, I have these warnings messages

1: In split.default(seq_along(x), f, drop = drop, ...) :
  data length is not a multiple of split variable
...

Do you have an idea of the reason ?

Ricol
  • 377
  • 4
  • 9
  • 22
  • 1
    For this I think you can nicely use `plyr`. It supports multicore processing, e.g. using `ddply`. – Paul Hiemstra May 06 '13 at 07:47
  • 1
    There is no need for you to split your dataframe in order to parallelize your operations. Just use something like `lapply(seq(nrow(df)), function(i) {...})` along with R's built-in `parallel` package. Or is there any urgency for you to manually split your data? – fdetsch May 06 '13 at 07:51
  • I think you really do not want to process row by row (4 rows at a time). Unless each row takes very long (> few seconds), the overhead of parallelisation will cause the analysis to become even slower. – Paul Hiemstra May 06 '13 at 07:53
  • Yes, I just do it in order to parallelize my operations. I'm sorry Paul Hiemstra, I don't understand your last comment. Even with ddply it's not good ? – Ricol May 06 '13 at 07:54
  • Stephane @PaulHiemstra won't see the comment unless you mention them using the @ symbol to ensure they get a notification that you posted on your question. (sorry Paul!) – Simon O'Hanlon May 06 '13 at 11:49
  • @Stephane see for example this SO question for more detail http://stackoverflow.com/questions/14614306/why-is-the-parallel-package-slower-than-just-using-apply/14614698#14614698 – Paul Hiemstra May 06 '13 at 11:50

2 Answers2

8

How many rows in df? You will get that warning if the number of rows in your table is not divisible by 4. I think you are using the split factor f incorrectly, unless what you want to do is put each subsequent row into a different split data.frame.

If you really want to split your data into 4 dataframes. one row after the other then make your splitting factor the same size as the number of rows in your dataframe using rep_len like this:

## Split like this:
split(df , f = rep_len(1:4, nrow(df) ) )
## Unsplit like this:
unsplit( split(df , f = rep_len(1:4, nrow(df) ) ) , f = rep_len(1:4,nrow(df) ) )

Hopefully this example illustrates why the error occurs and how to avoid it (i.e. use a proper splitting factor!).

## Want to split our data.frame into two halves, but rows not divisible by 2
df <- data.frame( x = runif(5) )
df

## Splitting still works but...
## We get a warning because the split factor 'f' was not recycled as a multiple of it's length
split( df , f = 1:2 )
#$`1`
#         x
#1 0.6970968
#3 0.5614762
#5 0.5910995

#$`2`
#         x
#2 0.6206521
#4 0.1798006

Warning message:
In split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
  data length is not a multiple of split variable


## Instead let's use the same split levels (1:2)...
## but make it equal to the length of the rows in the table:
splt <- rep_len( 1:2 , nrow(df) )
splt
#[1] 1 2 1 2 1


## Split works, and f is not recycled because there are 
## the same number of values in 'f' as rows in the table
split( df , f = splt )
#$`1`
#         x
#1 0.6970968
#3 0.5614762
#5 0.5910995

#$`2`
#         x
#2 0.6206521
#4 0.1798006

## And unsplitting then works as expected and reconstructs our original data.frame
unsplit( split( df , f = splt ) , f = splt )
#         x
#1 0.6970968
#2 0.6206521
#3 0.5614762
#4 0.1798006
#5 0.5910995
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
1

In the R language 'split' example . . .

aq <- airquality
g <- aq$Month
l <- split(aq,g)

After the 'scale' function is executed

l <- lapply(l, transform, Ozone = scale(Ozone))

I am guessing that at one time in R history the function 'scale' did not add extra attributes to the column it is modifying.

  ..$ Ozone  : num ...
  .. ..- attr(*, "scaled:center")= num 29.4
  .. ..- attr(*, "scaled:scale")= num 18.2

As seen in here . . .

> str(l)
List of 5
 $ 5:'data.frame':      31 obs. of  6 variables:
  ..$ Ozone  : num [1:31, 1] 0.782 0.557 -0.523 -0.253 NA ...
  .. ..- attr(*, "scaled:center")= num 23.6
  .. ..- attr(*, "scaled:scale")= num 22.2
  ..$ Solar.R: int [1:31] 190 118 149 313 NA NA 299 99 19 194 ...
  ..$ Wind   : num [1:31] 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
  ..$ Temp   : int [1:31] 67 72 74 62 56 66 65 59 61 69 ...
  ..$ Month  : int [1:31] 5 5 5 5 5 5 5 5 5 5 ...
  ..$ Day    : int [1:31] 1 2 3 4 5 6 7 8 9 10 ...
 $ 6:'data.frame':      30 obs. of  6 variables:
  ..$ Ozone  : num [1:30, 1] NA NA NA NA NA ...
  .. ..- attr(*, "scaled:center")= num 29.4
  .. ..- attr(*, "scaled:scale")= num 18.2
  ..$ Solar.R: int [1:30] 286 287 242 186 220 264 127 273 291 323 ...
  ..$ Wind   : num [1:30] 8.6 9.7 16.1 9.2 8.6 14.3 9.7 6.9 13.8 11.5 ...
  ..$ Temp   : int [1:30] 78 74 67 84 85 79 82 87 90 87 ...
  ..$ Month  : int [1:30] 6 6 6 6 6 6 6 6 6 6 ...
  ..$ Day    : int [1:30] 1 2 3 4 5 6 7 8 9 10 ...
 $ 7:'data.frame':      31 obs. of  6 variables:
  ..$ Ozone  : num [1:31, 1] 2.399 -0.32 -0.857 NA 0.154 ...
  .. ..- attr(*, "scaled:center")= num 59.1
  .. ..- attr(*, "scaled:scale")= num 31.6
  ..$ Solar.R: int [1:31] 269 248 236 101 175 314 276 267 272 175 ...
  ..$ Wind   : num [1:31] 4.1 9.2 9.2 10.9 4.6 10.9 5.1 6.3 5.7 7.4 ...
  ..$ Temp   : int [1:31] 84 85 81 84 83 83 88 92 92 89 ...
  ..$ Month  : int [1:31] 7 7 7 7 7 7 7 7 7 7 ...
  ..$ Day    : int [1:31] 1 2 3 4 5 6 7 8 9 10 ...
 $ 8:'data.frame':      31 obs. of  6 variables:
  ..$ Ozone  : num [1:31, 1] -0.528 -1.284 -1.108 0.455 -0.629 ...
  .. ..- attr(*, "scaled:center")= num 60
  .. ..- attr(*, "scaled:scale")= num 39.7
  ..$ Solar.R: int [1:31] 83 24 77 NA NA NA 255 229 207 222 ...
  ..$ Wind   : num [1:31] 6.9 13.8 7.4 6.9 7.4 4.6 4 10.3 8 8.6 ...
  ..$ Temp   : int [1:31] 81 81 82 86 85 87 89 90 90 92 ...
  ..$ Month  : int [1:31] 8 8 8 8 8 8 8 8 8 8 ...
  ..$ Day    : int [1:31] 1 2 3 4 5 6 7 8 9 10 ...
 $ 9:'data.frame':      30 obs. of  6 variables:
  ..$ Ozone  : num [1:30, 1] 2.674 1.928 1.721 2.467 0.644 ...
  .. ..- attr(*, "scaled:center")= num 31.4
  .. ..- attr(*, "scaled:scale")= num 24.1
  ..$ Solar.R: int [1:30] 167 197 183 189 95 92 252 220 230 259 ...
  ..$ Wind   : num [1:30] 6.9 5.1 2.8 4.6 7.4 15.5 10.9 10.3 10.9 9.7 ...
  ..$ Temp   : int [1:30] 91 92 93 93 87 84 80 78 75 73 ...
  ..$ Month  : int [1:30] 9 9 9 9 9 9 9 9 9 9 ...
  ..$ Day    : int [1:30] 1 2 3 4 5 6 7 8 9 10 ...

But now it does add those attributes

  ..$ Ozone  : num ...
  .. ..- attr(*, "scaled:center")= num 29.4
  .. ..- attr(*, "scaled:scale")= num 18.2

and the very simple 'unsplit' function is not programmed to handle those attributes.

> unsplit(l,g)
Error in xj[i, , drop = FALSE] : (subscript) logical subscript too long

The (direct and simple) solution is to get rid of those attributes.

attributes(l[[1]]$Ozone) <- NULL
attributes(l[[2]]$Ozone) <- NULL
attributes(l[[3]]$Ozone) <- NULL
attributes(l[[4]]$Ozone) <- NULL
attributes(l[[5]]$Ozone) <- NULL

Then try to unsplit again.

str( unsplit(l,g) )

> str( unsplit(l,g) )
'data.frame':   153 obs. of  6 variables:
 $ Ozone  : num  0.782 0.557 -0.523 -0.253 NA ...
 $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
 $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
 $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
 $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
 $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...

So, now it works.

Andre Mikulec