gpt4 book ai didi

r - 改变两个data.table中两个变量的编码,然后合并data.table

转载 作者:行者123 更新时间:2023-12-04 09:17:18 25 4
gpt4 key购买 nike

我有两个 data.tables(“偶数”和“奇数”),每个都有变量“time”(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.tables 合并为一个 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

有人可以帮忙吗?

最佳答案

根据您的描述,您可能正在寻找 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"))[]

关于r - 改变两个data.table中两个变量的编码,然后合并data.table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27375521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com