[英]Change the coding of two variables in two data.tables and then merge data.table
I have two data.tables ("even" and "odd"), each with the variable "time" (1:6). 我有两个data.tables(“偶数”和“奇数”),每个都有变量“time”(1:6)。 I want to recode the two "time" variables, so that "time" in "even" has only even numbers and "time" in "odd" has only odd numbers. 我想重新编码两个“时间”变量,因此“偶数”中的“时间”只有偶数,“奇数”中的“时间”只有奇数。 This is what I have done so far: 这是我到目前为止所做的:
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
Now I want to merge the two data.tables to one data.table. 现在我想将两个data.tables合并到一个data.table。 The new data.table should contain the id, emotion, learning, and one variable named "time_x". 新的data.table应该包含id,情感,学习和一个名为“time_x”的变量。 "time_x" should be the merged result of time_even and time_odd. “time_x”应该是time_even和time_odd的合并结果。 The new data.tables contains twice as much rows, but the same number of columns (the old variables "time" can be deleted) 新的data.tables包含两倍的行,但列数相同(旧的变量“time”可以删除)
The resulting table should look like this: 结果表应如下所示:
> 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
Can somebody help? 有人可以帮忙吗?
From what you describe, you might be looking for rbind
, not merge
. 根据您的描述,您可能正在寻找rbind
,而不是merge
。
Try the following: 请尝试以下方法:
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
In addition to @David's suggestions in the comments, if row and column ordering are of concern, you can compound a few more statements, like: 除了@ 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"))[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.