从向量中提取数字元素 [英] Extract numeric elements from a vector

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

问题描述

我有一个包含数字和非数字元素的向量a".我想从向量中提取数字元素并构建一个新向量.我该怎么做?

I have a vector 'a' which has numeric and non-numeric elements. I want to extract the numeric elements from the vector and build a new vector. How do I do this?

所以如果 a 是:

a = c(15, 3, 44, "hello", 4, "NA")

我想得到一个包含四个元素的向量,即元素 15 3 44 和 4

I want to get a vector with four elements namely elements 15 3 44 and 4

推荐答案

b = as.numeric(a)
b = b[!is.na(b)]
b

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

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