gpt4 book ai didi

r - 从非因子列生成键列

转载 作者:行者123 更新时间:2023-12-01 00:18:35 25 4
gpt4 key购买 nike

给定一个数据框的形式

       Key.1 Key.2      Value
1 5/25/2018 -10 0.53928999
2 5/25/2018 -10 0.23083204
3 5/25/2018 -10 0.33742676
4 5/25/2018 0 0.53479860
5 5/25/2018 0 0.27612761
6 5/25/2018 0 0.74993199
7 5/25/2018 10 0.01397069
8 5/25/2018 10 0.10553610
9 5/25/2018 10 0.66147883
10 1/17/2018 -10 0.14381738
11 1/17/2018 -10 0.52708544
12 1/17/2018 -10 0.75862925
13 1/17/2018 0 0.45954116
14 1/17/2018 0 0.68467543
15 1/17/2018 0 0.15865298
16 1/17/2018 10 0.01039363
17 1/17/2018 10 0.49886623
18 1/17/2018 10 0.98269967
19 5/25/2018 10 0.10553610
20 5/25/2018 -10 0.33742676

我需要生成一个 Group来自 key.1 互动专栏和 key.2那看起来像

       Key.1 Key.2      Value Group
1 5/25/2018 -10 0.53928999 1
2 5/25/2018 -10 0.23083204 1
3 5/25/2018 -10 0.33742676 1
4 5/25/2018 0 0.53479860 2
5 5/25/2018 0 0.27612761 2
6 5/25/2018 0 0.74993199 2
7 5/25/2018 10 0.01397069 3
8 5/25/2018 10 0.10553610 3
9 5/25/2018 10 0.66147883 3
10 1/17/2018 -10 0.14381738 4
11 1/17/2018 -10 0.52708544 4
12 1/17/2018 -10 0.75862925 4
13 1/17/2018 0 0.45954116 5
14 1/17/2018 0 0.68467543 5
15 1/17/2018 0 0.15865298 5
16 1/17/2018 10 0.01039363 6
17 1/17/2018 10 0.49886623 6
18 1/17/2018 10 0.98269967 6
19 5/25/2018 10 0.10553610 3
20 5/25/2018 -10 0.33742676 1

注意最后两行

重要的是 Group 的值正在沿数据框上升。我已经设法获得所需的行为

Data$Group          <-  interaction(paste(Data$Key.1,Data$Key.2),1)
levels(Data$Group) <- 1:length(levels(Data$Group))
levels(Data$Group) <- unique(Data$Group)

然而,这感觉非常不直观和笨拙。

如何使这既简短又直观?

需要注意的是,对 Key.1 的内容没有实际限制。或 Key.2可能是 - 核心行为只需是 Group由唯一的一对 Key.1 定义和 Key.2 ,然后从 table 上下来。

最佳答案

这是一个使用因子的想法:

使用基础 R:

df$Group = as.integer(factor(paste(df$Key.1, df$Key.2), 
levels = unique(paste(df$Key.1, df$Key.2))))

或与 mutate来自 dplyr :
library(dplyr)

df = mutate(df, Group = paste(Key.1, Key.2) %>%
factor(., levels = unique(.)) %>%
as.integer())

结果:
       Key.1 Key.2      Value Group
1 5/25/2018 -10 0.53928999 1
2 5/25/2018 -10 0.23083204 1
3 5/25/2018 -10 0.33742676 1
4 5/25/2018 0 0.53479860 2
5 5/25/2018 0 0.27612761 2
6 5/25/2018 0 0.74993199 2
7 5/25/2018 10 0.01397069 3
8 5/25/2018 10 0.10553610 3
9 5/25/2018 10 0.66147883 3
10 1/17/2018 -10 0.14381738 4
11 1/17/2018 -10 0.52708544 4
12 1/17/2018 -10 0.75862925 4
13 1/17/2018 0 0.45954116 5
14 1/17/2018 0 0.68467543 5
15 1/17/2018 0 0.15865298 5
16 1/17/2018 10 0.01039363 6
17 1/17/2018 10 0.49886623 6
18 1/17/2018 10 0.98269967 6
19 5/25/2018 10 0.10553610 3
20 5/25/2018 -10 0.33742676 1

数据:
df = structure(list(Key.1 = c("5/25/2018", "5/25/2018", "5/25/2018", 
"5/25/2018", "5/25/2018", "5/25/2018", "5/25/2018", "5/25/2018",
"5/25/2018", "1/17/2018", "1/17/2018", "1/17/2018", "1/17/2018",
"1/17/2018", "1/17/2018", "1/17/2018", "1/17/2018", "1/17/2018",
"5/25/2018", "5/25/2018"), Key.2 = c(-10L, -10L, -10L, 0L, 0L,
0L, 10L, 10L, 10L, -10L, -10L, -10L, 0L, 0L, 0L, 10L, 10L, 10L,
10L, -10L), Value = c(0.53928999, 0.23083204, 0.33742676, 0.5347986,
0.27612761, 0.74993199, 0.01397069, 0.1055361, 0.66147883, 0.14381738,
0.52708544, 0.75862925, 0.45954116, 0.68467543, 0.15865298, 0.01039363,
0.49886623, 0.98269967, 0.1055361, 0.33742676)), .Names = c("Key.1",
"Key.2", "Value"), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20"))

关于r - 从非因子列生成键列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50533786/

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