从非因子列生成键列

时间:2018-05-25 16:54:19

标签: r dataframe grouping

 

给出

形式的数据框
       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

我需要从Groupkey.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.1Key.2可能没有实际限制 - 核心行为只需GroupKey.1唯一的Key.2定义1}}和JLabel,然后沿着桌子上升。

2 个答案:

答案 0 :(得分:3)

以下是使用因素的想法:

使用Base 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"))

答案 1 :(得分:1)

选项是使用.GRP中的data.table符号为组分配唯一编号。第一组Key.1Key.2,然后添加值为.GRP的新列。

library(data.table)
setDT(df)
df[,Group:=.GRP, by=.(Key.1, Key.2)]  # Use of .GRP

#Check Result
df
#        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 <- read.table(text =
"       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",
stringsAsFactors = FALSE, header = TRUE)