Change the encoding of the two variables in the two data.tables and then merge the data.table

I have two data.tables ("even" and "odd"), each with a "time" variable (1: 6). I want to recode two time variables so that "time" in "even" has only even numbers and "time" in "odd" only has 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 combine two data.tables into one data table. The new datasheet should contain an id, emotion, training, and one variable named "time_x". "time_x" must be the combined result of time_even and time_odd. The new data.tables contain twice as many rows, but the same number of columns (the old "times" can be deleted)

The final 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 anyone please help?

+3


source to share


1 answer


From what you have described, you can search rbind

instead 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 the ordering of rows and columns is a concern, you can add a few more operators, for example:

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

      

+3


source







All Articles