我试图查看所有精确值在一个按数值排序的框架.令我惊讶的是,"unique"命令并没有显示所有的唯一值.我想也许有一个子命令unique或什么,但我找不到任何东西.这段代码演示了我试图做的事情和我期望的输出.

d <- data.frame(
  crp = as.numeric(c("1.0001","1","0.999999999", "0.999999999")),
  crp.txt = as.integer(c("1.0001","1","0.999999999", "0.999999999")) 
)

sort(unique(d$crp)) # is sorted but doesn't show exact values
sort(unique(toString(d$crp))) # is not sorted but shows all values

sort(unique(d$crp.txt)) # is sorted but doesn't show exact values
sort(unique(toString(d$crp.txt))) # is not sorted and doesn't show exact values
 
# expected output: [1] "0.999999999, 1, 1.0001"

我做错了什么/我应该做不同的事情吗?

推荐答案

我认为问题是,当你使用toString()将列转换为字符串时,你会将整个列转换为单个字符串,这不是你想要的.相反,您应该使用as. charge()将列的每个元素分别转换为字符串.这里有一个替代方案:

sort(unique(unlist(d)))

[1] "0.999999999" "1"           "1.0001" 

如果您想使用您的代码,可以执行以下操作:

# Convert each element of d$crp to character and then get unique values
unique(as.character(d$crp))
# Sort the unique values
sort(unique_crp)


# Convert each element of d$crp.txt to character and then get unique values
unique(as.character(d$crp.txt))
# Sort the unique values
sort(unique_crp_txt)

R相关问答推荐

如何对数据集进行逆向工程?

从开始时间和结束时间导出时间

RStudio中相关数据的分组箱形图

如何直接从Fortran到R的数组大小?

二维样条,严格以一个参数递增

自动变更列表

如何在geom_col中反转条

使用整齐的计算(curl -curl )和杂音

R函数,用于生成伪随机二进制序列,其中同一数字在一行中不出现超过两次

从一个列表的框架中移除列表包装器

如何对r中包含特定(未知)文本的行求和?

带RStatix的Wilcoxon环内检验

我需要使用ggplot2制作堆叠条形图

如何在shiny 的应用程序 map 视图宣传单中可视化单点

如何将图例文本添加到图例符号中

如何在R中的两列以上使用联合(&U)?

希望解析和复制R中特定模式的数据

R中从因数到数字的转换

具有由向量定义的可变步长的序列

如何修改Rust中的R字符串并将其赋给新的R变量,并使用extendr保留原始R字符串