简体   繁体   English

重塑数据时间表示为法术

[英]reshaping data with time represented as spells

I have a dataset in which time is represented as spells (ie from time 1 to time 2), like this: 我有一个数据集,其中时间表示为法术(即从时间1到时间2),如下所示:

d <- data.frame(id = c("A","A","B","B","C","C"),
                t1 = c(1,3,1,3,1,3),
                t2 = c(2,4,2,4,2,4),
                value = 1:6)

I want to reshape this into a panel dataset, ie one row for each unit and time period, like this: 我想将其重塑为面板数据集,即每个单元和时间段的一行,如下所示:

result <- data.frame(id = c("A","A","A","A","B","B","B","B","C","C","C","C"),
                     t= c(1:4,1:4,1:4),
                     value = c(1,1,2,2,3,3,4,4,5,5,6,6))

I am attempting to do this with tidyr and gather but not getting the desired result. 我试图用tidyr做这个并且gather但没有得到理想的结果。 I am trying something like this which is clearly wrong: 我正在尝试这样的事情,这显然是错误的:

gather(d, 't1', 't2', key=t)

In the actual dataset the spells are irregular. 在实际数据集中,法术是不规则的。

So, the goal is to melt t1 and t2 columns and to drop the key column that will appear as a result. 因此,目标是融合t1t2列并删除将作为结果显示的key列。 There are a couple of options. 有几种选择。 Base R's reshape seems to be tedious. Base R的reshape似乎很乏味。 We may, however, use melt : 但是,我们可以使用melt

library(reshape2)
melt(d, measure.vars = c("t1", "t2"), value.name = "t")[-3]
#    id value t
# 1   A     1 1
# 2   A     2 3
# 3   B     3 1
# 4   B     4 3
# 5   C     5 1
# 6   C     6 3
# 7   A     1 2
# 8   A     2 4
# 9   B     3 2
# 10  B     4 4
# 11  C     5 2
# 12  C     6 4

where -3 drop the key column. 其中-3删除key列。 We may indeed also use gather as in 我们确实也可以使用gather

gather(d, "key", "t", t1, t2)[-3]
#    id value t
# 1   A     1 1
# 2   A     2 3
# 3   B     3 1
# 4   B     4 3
# 5   C     5 1
# 6   C     6 3
# 7   A     1 2
# 8   A     2 4
# 9   B     3 2
# 10  B     4 4
# 11  C     5 2
# 12  C     6 4

You were almost there. 你快到

Code

d %>%
    # Gather the needed variables. Explanation:
    # t_type: How will the call the column where we will put the former
    #         variable names under?
    # t:      How will we call the column where we will put the
    #         values of above variables?
    # -id,
    # -value: Which columns should stay the same and NOT be gathered
    #         under t_type (key) and t (value)?
    # 
    gather(t_type, t, -id, -value) %>%
    # Select the right columns in the right order. 
    # Watch out: We did not select t_type, so it gets dropped.
    select(id, t, value) %>%
    # Arrange / sort the data by the following columns.
    # For a descending order put a "-" in front of the column name.  
    arrange(id, t)

Result 结果

   id t value
1   A 1     1
2   A 2     1
3   A 3     2
4   A 4     2
5   B 1     3
6   B 2     3
7   B 3     4
8   B 4     4
9   C 1     5
10  C 2     5
11  C 3     6
12  C 4     6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM