重塑R中的数据帧?

时间:2015-12-28 12:34:15

标签: r reshape2

这是我的dataframe action_slippers:

                   X_id                       cd      iios                       ui     w
1  56548c6ab65dd425cc3dda13 2015-11-24T16:12:26.572Z 194635691 563734c3b65dd40e340eaa56 0.010
2  56548df4b84c321fe4cdfb91 2015-11-24T16:19:00.798Z 194153563 56548df4b84c321fe4cdfb8f 0.010
3  56548fc7735e782a88591662 2015-11-24T16:26:46.952Z 177382028 563e12657d4c410c5832579c 0.010
4  565494e1b84c321fe4ce2f44 2015-11-24T16:48:33.828Z 177382031 563e12657d4c410c5832579c 0.010
5  5654994a735e782a88595802 2015-11-24T17:07:18.269Z 195129144 56549946735e782a885957e6 0.080
6  56549ce2b65dd425cc3e550c 2015-11-24T17:22:42.775Z 196972549 565181854c24b410e4891e11 0.010
7  56549f9bb84c321fe4ce7a3a 2015-11-24T17:34:19.732Z 194153563 56549f9bb84c321fe4ce7a37 0.010
8  5654a35a735e782a8859a055 2015-11-24T17:50:18.068Z 196258704 5654a35a735e782a8859a053 0.010
9  5654a5bab8e3a9227cffd593 2015-11-24T18:00:26.102Z 194907960 56320e0e55e89c3e14e26d3d 0.010
10 5654a7bb735e782a8859c495 2015-11-24T18:08:59.476Z 196950156 5651b53fec231f1df8482d23 0.027
11 5654a8955ff32d03cc2124b3 2015-11-24T18:12:37.381Z 194907960 5654a8955ff32d03cc2124b1 0.080

ı写了以下代码:

for (i in 1:nrow(actions_slippers)) {

if (actions_slippers$w[i]==0.027) {
  user_id=actions_slippers$ui[i]

  for (j in 1:i) {
    mydf <- data.frame(
      ui = c(actions_slippers$ui[1:i]),
      w = c(actions_slippers$w[1:i]),
      iios = factor(
        c(actions_slippers$iios[1:i]),
        levels = unique(x)))

  a=  dcast(mydf, formula = ui ~ iios, 
          fill = 0, value.var = "w", 
          fun.aggregate = sum, drop = FALSE)

我使用reshape2包和action_slippers数据帧转换为如下所示的数据框:

  ui 194635691 194153563 177382028 177382031 195129144 196972549 196258704 194907960 196950156 194139014 153444738 192982501 192891196
1 237      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.01     0.000         0         0         0         0
2 261      0.01      0.00      0.00      0.00      0.00      0.00      0.00      0.00     0.000         0         0         0         0
3 290      0.00      0.00      0.01      0.01      0.00      0.00      0.00      0.00     0.000         0         0         0         0
4 483      0.00      0.00      0.00      0.00      0.00      0.01      0.00      0.00     0.000         0         0         0         0
5 485      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00     0.027         0         0         0         0
6 533      0.00      0.01      0.00      0.00      0.00      0.00      0.00      0.00     0.000         0         0         0         0
7 534      0.00      0.00      0.00      0.00      0.08      0.00      0.00      0.00     0.000         0         0         0         0
8 535      0.00      0.01      0.00      0.00      0.00      0.00      0.00      0.00     0.000         0         0         0         0
9 536      0.00      0.00      0.00      0.00      0.00      0.00      0.01      0.00     0.000         0         0         0         0

但问题是如您所见,ui值已更改。 任何人都可以告诉我如何防止改变ui的值?

1 个答案:

答案 0 :(得分:3)

您已将因子actions_slippers$ui转换为数字。为避免这种情况,您可以通过转换:

将其转换为字符
ui = c(actions_slippers$ui[1:i])

ui = c(as.character(actions_slippers$ui[1:i]))

但是,您的代码不必要地复杂 - 如果可能的话,在r中避免循环总是一个好主意。查看代码,您希望排除上一个w == 0.027之后的所有内容,为wiios的每个组合添加ui列并传播结果。

使用dplyrtidyr

这是一个更快捷的解决方案
library(dplyr)
library(tidyr)
actions_slippers %>%
   filter(row_number() <= which.max(w == 0.027)) %>%
   group_by(iios, ui) %>%
   summarise(w = sum(w)) %>%
   spread(iios, w, fill = 0)

Source: local data frame [9 x 10]
                        ui 177382028 177382031 194153563 194635691 194907960 195129144 196258704 196950156
                    (fctr)     (dbl)     (dbl)     (dbl)     (dbl)     (dbl)     (dbl)     (dbl)     (dbl)
1 56320e0e55e89c3e14e26d3d      0.00      0.00      0.00      0.00      0.01      0.00      0.00     0.000
2 563734c3b65dd40e340eaa56      0.00      0.00      0.00      0.01      0.00      0.00      0.00     0.000
3 563e12657d4c410c5832579c      0.01      0.01      0.00      0.00      0.00      0.00      0.00     0.000
4 565181854c24b410e4891e11      0.00      0.00      0.00      0.00      0.00      0.00      0.00     0.000
5 5651b53fec231f1df8482d23      0.00      0.00      0.00      0.00      0.00      0.00      0.00     0.027
6 56548df4b84c321fe4cdfb8f      0.00      0.00      0.01      0.00      0.00      0.00      0.00     0.000
7 56549946735e782a885957e6      0.00      0.00      0.00      0.00      0.00      0.08      0.00     0.000
8 56549f9bb84c321fe4ce7a37      0.00      0.00      0.01      0.00      0.00      0.00      0.00     0.000
9 5654a35a735e782a8859a053      0.00      0.00      0.00      0.00      0.00      0.00      0.01     0.000
Variables not shown: 196972549 (dbl)