2013-05-06 113 views
2

我想拆分4個等份的數據框,因爲我想使用我的電腦的4個內核。拆分和非拆分數據框分爲四部分

我這樣做:

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

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

但是未分裂功能沒有工作,我有這些警告信息

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

你有一個想法原因 ?

+1

爲此,我認爲您可以很好地使用'plyr'。它支持多核處理,例如使用'ddply'。 – 2013-05-06 07:47:17

+1

您無需爲了並行操作而拆分數據框。只需使用像lapply(seq(nrow(df)),function(i){...})'和R的內置'parallel'軟件包。或者是否有迫切需要手動分割數據? – fdetsch 2013-05-06 07:51:24

+0

我想你真的不想逐行處理(一次4行)。除非每行花費很長時間(>幾秒),否則平行化開銷將導致分析變得更慢。 – 2013-05-06 07:53:36

回答

7

df中有多少行?如果表中的行數不能被4整除,我們會得到該警告。我認爲您錯誤地使用了拆分因子f,除非您想要將每個後續行放入不同的拆分數據幀中。

如果您確實想將您的數據拆分爲4個數據框。前一後一排,然後讓你的分裂因素使用相同的尺寸爲您的數據幀的行數rep_len這樣的:

## 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))) 

希望這個例子說明了爲什麼會出現錯誤,以及如何避免它(即使用適當的分裂因子!)。

## 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 
0

在R語言的「split」示例中。 。 。

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

的「規模」功能被執行

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

我猜測,在R中歷史 功能「縮放」一週時間沒 增加額外的屬性,它被修改列後。

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

如在此處所見。 。 。

> 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 ... 

但現在它確實增加了這些屬性

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

和非常簡單的「不分開的」功能沒有被編程來處理這些屬性。

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

(直接和簡單)解決方案是擺脫這些屬性。

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 

然後嘗試再次分離。

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 ... 

所以,現在它工作。

Andre Mikulec