重新整形R中的数据将长表更改为宽表

时间:2015-06-26 02:37:10

标签: r pivot-table reshape2

我想在R中使用reshape2包将我的长表更改为宽表。

我有一个数据库的数据集,就像这样(例子):

id1   |  id2 |  info  | action_time |
 1    | a    |  info1 |    time1    |
 1    | a    |  info1 |    time2    |  
 1    | a    |  info1 |    time3    |  
 2    | b    |  info2 |    time4    |
 2    | b    |  info2 |    time5    |

现在我希望它是这样的:

id1   |  id2 |  info  |action_time 1|action_time 2|action_time 3|
 1    | a    |  info1 |    time1    |    time2    |    time3    |
 2    | b    |  info2 |    time4    |    time5    |             | 

我曾多次尝试使用reshape()dcast()查看某些网站上的一些示例,但无法找到这样的示例。每个ID的action_time数量不同,对于某些ID,它们可能超过10 action_time s,因此在这种情况下,重新整形的数据集将包含超过10列的{{1} }。

任何人都可以想到一个方便的方法吗?如果有一种方法可以在excel中实现这一点(Pivot Table?),它也会很棒。谢谢堆

2 个答案:

答案 0 :(得分:2)

尝试:

library(dplyr)
library(tidyr)

df %>% 
  group_by(id1) %>% 
  mutate(action_no = paste("action_time", row_number())) %>%
  spread(action_no, action_time)

给出了:

#Source: local data frame [2 x 6]
#
#  id1 id2  info action_time 1 action_time 2 action_time 3
#1   1   a info1         time1         time2         time3
#2   2   b info2         time4         time5            NA

数据

df <- structure(list(id1 = c(1, 1, 1, 2, 2), id2 = structure(c(1L, 
1L, 1L, 2L, 2L), .Label = c("a", "b"), class = "factor"), info = structure(c(1L, 
1L, 1L, 2L, 2L), .Label = c("info1", "info2"), class = "factor"), 
    action_time = structure(1:5, .Label = c("time1", "time2", 
    "time3", "time4", "time5"), class = "factor")), .Names = c("id1", 
"id2", "info", "action_time"), class = "data.frame", row.names = c(NA, -5L))

答案 1 :(得分:1)

使用tidyr

require(tidyr)
# replicate data
df <- structure(list(id1 = c(1, 1, 1, 2, 2), id2 = structure(c(1L, 
                                                               1L, 1L, 2L, 2L), .Label = c(" a    ", " b    "), class = "factor"), 
                     info = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("  info1 ", 
                                                                        "  info2 "), class = "factor"), action_time = structure(1:5, .Label = c("    time1    ", 
                                                                                                                                                "    time2    ", "    time3    ", "    time4    ", "    time5    "
                                                                        ), class = "factor")), .Names = c("id1", "id2", "info", "action_time"
                                                                        ), class = "data.frame", row.names = c(NA, -5L))


# create additional column on action_time sequence
action_no <- paste("action_time",
                   unlist(sapply(rle(df$id1)$lengths, function(x) seq(1, x))))
y <- cbind(df, action_no)

# spread into final dataframe
z <- spread(y, action_no, action_time)

最终输出

> z
  id1    id2     info action_time 1 action_time 2 action_time 3
1   1  a       info1      time1         time2         time3    
2   2  b       info2      time4         time5              <NA>