从R中的数字中提取数字 [英] Extract digit from numeric in r

查看:404
本文介绍了从R中的数字中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从R中的数字矢量中提取小数点后的第一个数字。有没有一种方法可以将其转换为字符串?例如:

I would like to extract the first digit after a decimal place from a numeric vector in R. Is there a way to do this without turning it into a character string? For example:

x <- c(1.0,1.1,1.2)

我希望函数返回向量:

 0,1,2

谢谢。

推荐答案

会有很多方法,但这是一种方法:

There'll be a bunch of ways, but here's one:

(x %% 1)*10
# [1] 0 1 2

这假定小数点后只有一位。如果不是这种情况:

This assumes there's only ever one digit after the decimal place. If that's not the case:

floor((x %% 1)*10)

这篇关于从R中的数字中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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