【问题标题】:Change the coding of two variables in two data.tables and then merge data.table改变两个data.table中两个变量的编码,然后合并data.table
【发布时间】:2015-02-07 03:26:05
【问题描述】:

我有两个 data.tables(“偶数”和“奇数”),每个都有变量“时间”(1:6)。我想重新编码两个“时间”变量,以便“偶数”中的“时间”只有偶数,而“奇数”中的“时间”只有奇数。 这是我到目前为止所做的:

library(data.table)

## Creating data set for odd weeks
odd <- data.table (id=c(11, 11, 11, 22, 22, 22, 33, 33, 33), time=c(1,2,3,1,2,3,1,2,3),     emotion=c(4,6,7,3,5,2,4,6,7), learning=c(60,50,60,30,40,20,50,30,20))

## Creating data set for even weeks
even <- data.table (id=c(11, 11, 11, 22, 22, 22, 33, 33, 33), time=c(1,2,3,1,2,3,1,2,3),     emotion=c(5,7,8,3,10,13,4,3,2), learning=c(40,70,30,80,20,30,40,50,30))

## Recode variable "time" for odd weeks

recode1 <- function (x){
   sapply(x, function(a) (a*2-1))
 }

time_odd <- recode1(odd[,time])
time_odd
## [1] 1 3 5 1 3 5 1 3 5

odd2 <- cbind(odd, time_odd)
odd2
   id time emotion learning time_odd
1: 11    1       4       60        1
2: 11    2       6       50        3
3: 11    3       7       60        5
4: 22    1       3       30        1
5: 22    2       5       40        3
6: 22    3       2       20        5
7: 33    1       4       50        1
8: 33    2       6       30        3
9: 33    3       7       20        5

## Recode variable "time" for even weeks

recode2 <- function (x){
   sapply(x, function(a) (a*2))
 }

time_even <- recode2(even[,time])
time_even
## [1] 2 4 6 2 4 6 2 4 6

even2 <- cbind(even, time_even)
even2
   id time emotion learning time_even
1: 11    1       5       40         2
2: 11    2       7       70         4
3: 11    3       8       30         6
4: 22    1       3       80         2
5: 22    2      10       20         4
6: 22    3      13       30         6
7: 33    1       4       40         2
8: 33    2       3       50         4
9: 33    3       2       30         6

现在我想将两个 data.table 合并为一个 data.table。新的 data.table 应该包含 id、emotion、learning 和一个名为“time_x”的变量。 "time_x" 应该是 time_even 和 time_odd 的合并结果。 新的 data.tables 包含两倍的行数,但列数相同(可以删除旧变量“时间”)

结果表应如下所示:

> result <- data.table(id=c(11, 11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33, 33), time_x=c(1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6), emotion=c(4,5,6,7,7,8,3,3,5,10,2,13,4,4,6,3,7,2), learning=c(60, 40, 50, 70, 60,30, 30, 80, 40,20, 20, 30, 50, 40, 30, 50, 20, 30))
> result
    id time_x emotion learning
 1: 11    1       4       60
 2: 11    2       5       40
 3: 11    3       6       50
 4: 11    4       7       70
 5: 11    5       7       60
 6: 11    6       8       30
 7: 22    1       3       30
 8: 22    2       3       80
 9: 22    3       5       40
10: 22    4      10       20
11: 22    5       2       20
12: 22    6      13       30
13: 33    1       4       50
14: 33    2       4       40
15: 33    3       6       30
16: 33    4       3       50
17: 33    5       7       20
18: 33    6       2       30

有人可以帮忙吗?

【问题讨论】:

    标签: r merge data.table recode


    【解决方案1】:

    根据您的描述,您可能正在寻找rbind,而不是merge

    尝试以下方法:

    rbind(odd[, time_x := time * 2 - 1], 
          even[, time_x := time * 2])[, time := NULL][]
    #     id emotion learning time_x
    #  1: 11       4       60      1
    #  2: 11       6       50      3
    #  3: 11       7       60      5
    #  4: 22       3       30      1
    #  5: 22       5       40      3
    #  6: 22       2       20      5
    #  7: 33       4       50      1
    #  8: 33       6       30      3
    #  9: 33       7       20      5
    # 10: 11       5       40      2
    # 11: 11       7       70      4
    # 12: 11       8       30      6
    # 13: 22       3       80      2
    # 14: 22      10       20      4
    # 15: 22      13       30      6
    # 16: 33       4       40      2
    # 17: 33       3       50      4
    # 18: 33       2       30      6
    

    除了 @David 在 cmets 中的建议之外,如果关注行和列的顺序,您可以复合更多语句,例如:

    ## Takes cares of the rows
    rbind(odd[, time_x := time * 2 - 1], 
          even[, time_x := time * 2])[, time := NULL][order(id, time_x)]
    
    ## Takes care of the rows and columns
    setcolorder(
      rbind(odd[, time_x := time * 2 - 1], 
            even[, time_x := time * 2])[, time := NULL][order(id, time_x)], 
      c("id", "time_x", "emotion", "learning"))[]
    

    【讨论】:

    • 非常感谢,这是重新编码时间变量的一种更简单的方法。然后我如何合并/重新绑定/无论这两个 data.tables,以便将两个 data.tables 集成到一个 data.table 中(参见我上面的“结果”data.table?
    • 将 Amandas 代码放入 result,然后执行 setkey(result, id, time_x)setorder(result, id, time_x) 之类的操作
    • @DavidArenburg, AAaarrgghh!再次发生性别变化! :-)
    • 这次是故意的 ;)
    • 我运行它很累,但收到此错误消息::=(time_x, time * 2 - 1) 中的错误:检查 is.data.table(DT) == TRUE。否则,:= 和:=(...) 被定义为在 j 中使用,仅一次且以特定方式使用。请参阅帮助(“:=”)。
    猜你喜欢
    • 2023-03-19
    • 2018-01-12
    • 2013-12-26
    • 2019-04-17
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多