在数据框中*删除数字中的前导零 [英] Remove leading zeros in numbers *within a data frame*

查看:80
本文介绍了在数据框中*删除数字中的前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:对于以后的任何人:这不是重复的,因为它明确涉及数据帧的工作,而不是单个变量/向量。

For anyone coming later: THIS IS NOT A DUPLICATE, since it explicitely concerns work on data frames, not single variables/vectors.

我发现了几个站点,描述了如何在数字或字符串(包括向量)中删除前导零。但是我发现的描述似乎都不适用于数据帧。

I have found several sites describing how to drop leading zeros in numbers or strings, including vectors. But none of the descriptions I found seem applicable to data frames.

numform 包中的 f_num 函数。它处理 [a]数字向量(或等效字符串),但似乎无法解决数据帧中不需要的前导零。

Or the f_num function in the numform package. It treats "[a] vector of numbers (or string equivalents)", but does not seem to solve unwanted leading zeros in a data frame.

我对R还是比较陌生,但可以理解,我可以开发一些(在我看来)复杂的代码,以通过将数据帧中的向量设置子集然后将其合并来删除前导零。向量变成完整的数据帧。我想避免这种情况。

I am relatively new to R but understand that I could develop some (in my mind) complex code to drop leading zeros by subsetting vectors from a data frame and then combining those vectors into a full data frame. I would like to avoid that.

这是一个简单的数据框:

Here is a simple data frame:

df <-  structure(list(est = c(0.05, -0.16, -0.02, 0, -0.11, 0.15, -0.26, 
-0.23), low2.5 = c(0.01, -0.2, -0.05, -0.03, -0.2, 0.1, -0.3, 
-0.28), up2.5 = c(0.09, -0.12, 0, 0.04, -0.01, 0.2, -0.22, -0.17
)), row.names = c(NA, 8L), class = "data.frame")

哪个给出了

df
    est low2.5 up2.5
1  0.05   0.01  0.09
2 -0.16  -0.20 -0.12
3 -0.02  -0.05  0.00
4  0.00  -0.03  0.04
5 -0.11  -0.20 -0.01
6  0.15   0.10  0.20
7 -0.26  -0.30 -0.22
8 -0.23  -0.28 -0.17

我想要

est low2.5 up2.5
1  .05   .01  .09
2 -.16  -.20 -.12
3 -.02  -.05  .00
4  .00  -.03  .04
5 -.11  -.20 -.01
6  .15   .10  .20
7 -.26  -.30 -.22
8 -.23  -.28 -.17

相对si可能吗

编辑:错误的链接已删除。

An incorrect link has been removed.

推荐答案

我正在解释您的问题的意图是转换 data.frame 中的每个数字单元格转换为漂亮打印的字符串,可以使用字符串替换和简单的正则表达式(这是一个好问题,顺便说一句,因为我不知道有什么方法可以配置数字数据的输出以抑制前导零而不将数字数据转换为字符串) !):

I am interpreting the intention of your question is to convert each numeric cell in the data.frame into a "pretty-printed" string which is possible using string substitution and a simple regular expression (a good question BTW since I do not know any method to configure the output of numeric data to suppress leading zeros without converting the numeric data into a string!):

df2 <- data.frame(lapply(df,
                         function(x) gsub("^0\\.", "\\.", gsub("^-0\\.", "-\\.", as.character(x)))),
                  stringsAsFactors = FALSE)
df2
#    est low2.5 up2.5
# 1  .05    .01   .09
# 2 -.16    -.2  -.12
# 3 -.02   -.05     0
# 4    0   -.03   .04
# 5 -.11    -.2  -.01
# 6  .15     .1    .2
# 7 -.26    -.3  -.22
# 8 -.23   -.28  -.17

str(df2)
# 'data.frame': 8 obs. of  3 variables:
# $ est   : chr  ".05" "-.16" "-.02" "0" ...
# $ low2.5: chr  ".01" "-.2" "-.05" "-.03" ...
# $ up2.5 : chr  ".09" "-.12" "0" ".04" ...

如果要在小数点后获得固定位数(如预期输出所示,但不要求明确地),您可以使用 sprintf 格式

If you want to get a fixed number of digits after the decimal point (as shown in the expected output but not asked for explicitly) you could use sprintf or format:

df3 <- data.frame(lapply(df, function(x) gsub("^0\\.", "\\.", gsub("^-0\\.", "-\\.", sprintf("%.2f", x)))), stringsAsFactors = FALSE)
df3
#    est low2.5 up2.5
# 1  .05    .01   .09
# 2 -.16   -.20  -.12
# 3 -.02   -.05   .00
# 4  .00   -.03   .04
# 5 -.11   -.20  -.01
# 6  .15    .10   .20
# 7 -.26   -.30  -.22
# 8 -.23   -.28  -.17

注意:此解决方案针对不同的小数点字符(不同的语言环境)是可靠的-总是期望小数点...

Note: This solution is not robust against different decimal point character (different locales) - it always expects a decimal point...

这篇关于在数据框中*删除数字中的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆