我有以下数据框 df1。(编辑为在数据框中具有不同数量的重复值。)
> dput(df1)
structure(list(...1 = c("a", "b", "c", "d", "e"), x = c(5, 10,
20, 20, 25), y = c(2, 6, 6, 6, 10), z = c(6, 2, 1, 8, 1)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
>df1
x y z
a 5 2 6
b 10 6 2
c 20 6 1
d 20 6 8
e 25 10 1
我想得到一个 df2,它只有每列“x”、“y”和“z”的唯一值。
我试过了:
df2<-apply(df1,2, unique)
df2 <- do.call(cbind, df2)
df2 <- as.data.frame(df2)
期望的输出:
>df2
x y z
5 2 6
10 6 2
20 10 1
25 8