r - 如何将向量变成r中的集合

标签 r

我想在数据框的一列中看到所有可能值的列表,没有重复。就像是:

as.set(series["begin_year"][,1])

对于列“begin_year”虽然 as.set不存在。

最佳答案

unique() [或 levels() ,如果该列是一个因素]。

这是可重现的示例:

dat <- OrchardSprays
dat$rowpos
unique(dat$rowpos)
dat$treatment
unique(dat$treatment)
levels(dat$treatment) 

编辑
请注意 levels()将返回因子的唯一级别,即使该级别未使用。考虑:
dat2 <- subset(dat, treatment != "A")
unique(dat2$treatment)
# [1] D E B H G F C
# Levels: A B C D E F G H
levels(dat2$treatment) 
# [1] "A" "B" "C" "D" "E" "F" "G" "H"

您可以使用 droplevels() 删除未使用的级别。 :
dat2$treatment <- droplevels(dat2$treatment)
levels(dat2$treatment)
# [1] "B" "C" "D" "E" "F" "G" "H"

关于r - 如何将向量变成r中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7953686/

相关文章:

R - 具有特定值的置换(样本)列

r - styleColorBar : have the size of the color bar be proportional to absolute values of a column

r - 使用 if 语句对数字进行分类的循环

r - 聚合/Group_by R中的第二个最小值

r - 模拟季节性 ARIMA 模型的问题

r - 指定类别中关键字的匹配数

r - 使用系统命令打开具有特定路径的 Windows 资源管理器

正则表达式、R 和逗号

r - 在 Ubuntu 18.04 上编译 R 3.6.1 时出错

r - 从 R 到 MongoDB 的并行插入