我们如何将整数拆分为 R 中其组成数字的向量 [英] How can we split an integer number into a vector of its constituent digits in R

查看:27
本文介绍了我们如何将整数拆分为 R 中其组成数字的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为一个例子应该足够清楚了.

I think an example should make things clear enough.

我有

a_1 = 6547

我想要一些将 a_1 转换为以下 a_2 的函数

and I want some function that transform a_1 into the following a_2

a_2 = c(6, 5, 4, 7)

a_2 = c(6, 5, 4, 7)

推荐答案

转换为字符然后拆分即可

Convert to character then split will do the trick

a <- 6547
as.numeric(strsplit(as.character(a), "")[[1]])
## [1] 6 5 4 7

这篇关于我们如何将整数拆分为 R 中其组成数字的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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