在两个data.tables中更改两个变量的编码,然后合并data.table

时间:2014-12-09 09:33:00

标签: r merge data.table recode

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

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.tables合并到一个data.table中。新的data.table应该包含id,情感,学习和一个名为&#34; time_x&#34;的变量。 &#34; time_x&#34;应该是time_even和time_odd的合并结果。 新的data.tables包含两倍的行,但列数相同(旧的变量&#34;时间&#34;可以删除)

结果表应如下所示:

> 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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

根据您的描述,您可能正在寻找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的建议外,如果需要关注行和列排序,还可以复合几个语句,例如:

## 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"))[]