如何获取r中数据帧中重复值的下一个元素?

时间:2019-05-01 13:27:22

标签: r dataframe merge duplicates

我想删除A列中的重复元素,并且需要合并新列中重复值的相关元素 我有这个数据框:

${nominalTime}

这是我期望的数据框:

A   B   Repeat
a   x1  5
a   x5  5
a   x4  5
a   x2  5
a   x3  5
b   x2  3
b   x4  3
b   x1  3
c   x5  3
c   x9  3
c   x3  3
d   x2  2
d   x8  2
e   x5  2
e   x1  2
f   x6  1
g   x2  1
h   x5  1
i   x4  1
j   x7  1

2 个答案:

答案 0 :(得分:1)

使用dplyr,我们可以group_by A并使用BtoString创建一个逗号分隔的值,并获得first的值的Repeat

library(dplyr)

df %>% 
  group_by(A) %>%
  summarise(new_B = toString(B),
            Repeat = first(Repeat))


# A tibble: 10 x 3
#    A     new_B              Repeat
#   <fct> <chr>               <int>
# 1 a     x1, x5, x4, x2, x3      5
# 2 b     x2, x4, x1              3
# 3 c     x5, x9, x3              3
# 4 d     x2, x8                  2
# 5 e     x5, x1                  2
# 6 f     x6                      1
# 7 g     x2                      1
# 8 h     x5                      1
# 9 i     x4                      1
#10 j     x7                      1

答案 1 :(得分:0)

我们可以使用let tensorArr = tensor.arraySync() fs.writeFileSync("test", JSON.stringify(tensorArr)) let test = JSON.parse(classifierFile) tf.tensor(test)

data.table

数据

library(data.table)
setDT(df)[, .(new_B = toString(B), Repeat = first(Repeat)), A]
#    A              new_B Repeat
# 1: a x1, x5, x4, x2, x3      5
# 2: b         x2, x4, x1      3
# 3: c         x5, x9, x3      3
# 4: d             x2, x8      2
# 5: e             x5, x1      2
# 6: f                 x6      1
# 7: g                 x2      1
# 8: h                 x5      1
# 9: i                 x4      1
#10: j                 x7      1