2

我有一个只有一列和 158112 个不同值的数据框。这些值不是随机排序的。每 24 个值代表一天。每天在那里列出 18 次,然后是第二天,例如。18x24 用于 01.01.2012,18x24 用于 02.01.2012 等等。

        df
1       593
2       939
3       734
4       791
5       184
6       495
...
158112  683

我想将它们组织在不同结构的新数据框中。这个过程看起来像这样:

取前 24 个值并将它们放入新数据框“new_df”列号中。1,取接下来的 24 个值并放入“new_df”列号。2,取接下来的 24 个值并放入“new_df”列号。3. 这样做直到 18 列被每 24 个值填充,然后从第 1 列重新开始,并添加接下来的 24 个值,依此类推......所以最后我希望有 18 列的“new_df”和每行 8784 行。

有任何想法吗?

4

3 回答 3

2

试试这个:

set.seed(1)  
df <- data.frame(df=sample(1:999, 158112, TRUE))  # creating some data
new_df <- data.frame(matrix(unlist(df), ncol=18)) # putting df into a 8784 x 18 data.frame 
dim(new_df) # checking the dimensions of new_df
于 2013-07-23T11:01:33.747 回答
1

我认为您想要以下内容:

# sample data
mydf <- data.frame(df=rnorm(18*8784,0,1))
# split dataframe into chunks (of 18*24)
mylist <- split(mydf,rep(1:366,each=432))
# turn each chunk into a matrix of the right shape and `rbind` them back together
new_df <- do.call(rbind, lapply(mylist, function(x) matrix(x[,1],nrow=24)))

您可以检查这是否正确:

all.equal(mydf[1:24,1],new_df[1:24,1]) # first 24 values are first column
all.equal(mydf[25:48,1],new_df[1:24,2]) # next 24 values are second column
all.equal(mydf[433:456,1],new_df[25:48,1]) # day 2 starts in the first column

所有这些都应该是TRUE。我猜你想要它作为一个data.frame,所以只需使用as.data.frame(new_df)将结果返回到一个data.frame。

于 2013-07-23T11:10:11.737 回答
1

到目前为止,也许比其他替代方案更好的是使用 anarray将您的数据操作到您想要的结构中。由于您只处理单个向量并且您希望按列填充数据,因此您只需将dims 分配给您的向量。

这是一个简化的例子。我们将从长度为 40 的向量开始。

mydata <- rep(1:8, each = 5)
mydata
#  [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 
# [21] 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8

现在,假设我们要将其转换为四列,其中前 20 个值组合在一起,后 20 个值组合在一起。(在您的数据中,前 24*18 个值组合在一起表示一天的 18 列记录。)

下面是我们将如何做到这一点:

myarray <- array(mydata, dim=c(5, 4, 2),
                 dimnames = list(NULL, NULL,
                                 c("2012-01-01", "2012-01-02")))
myarray
# , , 2012-01-01
# 
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    3    4
# [2,]    1    2    3    4
# [3,]    1    2    3    4
# [4,]    1    2    3    4
# [5,]    1    2    3    4
# 
# , , 2012-01-02
# 
#      [,1] [,2] [,3] [,4]
# [1,]    5    6    7    8
# [2,]    5    6    7    8
# [3,]    5    6    7    8
# [4,]    5    6    7    8
# [5,]    5    6    7    8

也许你想在这一点上停下来。但是,如果您想一直到 single data.frame,那也很容易。

使用@Jilber 的示例数据只是为了便于复制:

set.seed(1)
df <- data.frame(df=sample(1:999, 158112, TRUE))
# Hopefully you've done your math correctly
#   R will recycle if the dims aren't correct
#   for your data.
Ndays <- nrow(df)/(24*18)
dfarray <- array(df$df, 
                 dim = c(24, 18, Ndays), 
                 # Add dimnames by creating a date sequence
                 dimnames = list(NULL, NULL, as.character(
                   seq(as.Date("2012-01-01"), by = "1 day", 
                       length.out = Ndays))))
# Use `apply` to convert this to a `list` of `data.frame`s
temp <- apply(dfarray, 3, as.data.frame)
# Use `lapply` to create your intermediate `data.frame`s
out <- lapply(names(temp), function(x) {
  data.frame(date = as.Date(x), temp[[x]])
})
# Use `do.call(rbind, ...)` to get your final `data.frame`
final <- do.call(rbind, out)

输出的前几行如下所示:

head(final)
#         date  V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 V11 V12 V13 V14 V15
# 1 2012-01-01 266 267 732 347 455 991 729 724 101 649 307 702 133 841 443
# 2 2012-01-01 372 386 693 334 410 496 453 338 927 953 578 165 222 720 157
# 3 2012-01-01 573  14 478 476 811 484 175 630 283 953 910  65 227 267 582
# 4 2012-01-01 908 383 861 892 605 174 746 840 590 340 143 754 132 495 970
# 5 2012-01-01 202 869 438 864 655 755 105 856 111 263 415 620 981  84 989
# 6 2012-01-01 898 341 245 390 353 454 864 391 840 166 211 170 327 354 177
#   V16 V17 V18
# 1 109 232  12
# 2 333 241 940
# 3 837 797 993
# 4 277 831 358
# 5 587 114 747
# 6 836 963 793

如果您要对时间序列数据进行大量工作,我仍然强烈建议您熟悉“xts”包。

data.frame从上面的“最终”转换为xts对象很容易:

library(xts)
Final <- xts(final[-1], order.by=final[[1]])

这会让你轻松地做这样有趣的事情:

apply.quarterly(Final, mean)
#                  V1       V2       V3       V4       V5       V6
# 2012-03-31 490.5256 493.8338 507.4272 503.5421 495.0929 494.4025
# 2012-06-30 511.5792 508.1493 500.9043 500.2152 509.0614 499.9881
# 2012-09-30 496.2672 501.1399 496.3542 493.7423 504.8170 507.1671
# 2012-12-31 503.9583 502.5616 502.8936 509.2120 503.2387 502.4678
#                  V7       V8       V9      V10      V11      V12
# 2012-03-31 490.2477 492.2115 510.6525 499.8168 506.9510 494.3654
# 2012-06-30 494.0962 497.0357 506.9267 500.2198 501.4263 494.1117
# 2012-09-30 509.9561 487.0543 497.2206 485.4511 498.1191 494.5190
# 2012-12-31 503.0095 500.7903 494.7428 494.1409 502.0181 496.9764
#                 V13      V14      V15      V16      V17      V18
# 2012-03-31 504.4130 499.8581 503.0023 501.0137 499.1021 504.7711
# 2012-06-30 500.0504 501.2903 490.7582 502.7395 503.5737 496.4821
# 2012-09-30 493.4860 499.2088 500.7260 503.1907 491.9583 490.4293
# 2012-12-31 500.4348 507.9475 499.3637 486.4438 496.8220 492.8890
于 2013-07-23T16:39:22.960 回答