如何删除R数据框中的尾随零 [英] How to remove trailing zeros in R dataframe
本文介绍了如何删除R数据框中的尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的 R data.frame:
I have a R data.frame that looks like this:
Gene Score
1 AAT2 15.40100
2 ACB1 5.11880
3 ACF2 15.04500
4 ADE16 3.04080
5 ADE17 0.28143
6 ADE4 19.79200
但是我需要去掉尾随的零才能得到:
However I need to get rid of the trailing zeros in order to get:
Gene Score
1 AAT2 15.401
2 ACB1 5.1188
3 ACF2 15.045
4 ADE16 3.0408
5 ADE17 0.28143
6 ADE4 19.792
基本上我试图将分数列中的位数设置为 5 位数.我如何在 R 中做到这一点?
Basically I am trying to set the number of digits in the score column to 5 digits. How can I do this in R?
推荐答案
如果你想查看你的 Score
数据作为文本,没有尾随零,然后使用:
If you want to view your Score
data as text, with no trailing zeroes, then use:
df_view <- df[c("Gene", "Score")]
df_view$Score <- sub("0+$", "", as.character(df_view$Score))
df_view
Gene Score
1 AAT2 15.401
2 ACB1 5.1188
3 ACF2 15.045
4 ADE16 3.0408
5 ADE17 0.28143
6 ADE4 19.792
数据:
df <- data.frame(Gene=c("AAT2", "ACB1", "ACF2", "ADE16", "ADE17", "ADE4"),
Score=c(15.40100, 5.11880, 15.04500, 3.04080, 0.28143, 19.79200),
stringsAsFactors=FALSE)
这篇关于如何删除R数据框中的尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文