简体   繁体   English

R中数值向量中的空数值

[英]Empty numeric value in a numeric vector in R

I want to create a numeric vector in R with a placeholder.我想用占位符在 R 中创建一个数字向量。 Just like in a chracter vector like:就像在字符向量中一样:

characterVec <- c("a", "b", "", "d")

This gives me a characterVec vector with a length of 4.这给了我一个长度为 4 的characterVec向量。

How can I create a numeric vector with a length of 4, but still has one empty value?如何创建一个长度为 4 的数字向量,但仍有一个空值? For example, I would like to know what do I put into the question mark in the following vector.例如,我想知道在以下向量中的问号中放入什么。

numericVec <- c(1, 2, ?, 4)

If I'm understanding your question properly, you can use a named vector to create a data dictionary linking letters to corresponding numbers:如果我正确理解您的问题,您可以使用命名向量来创建将字母链接到相应数字的数据字典:

# data dictionary
dat <- 1:26
names(dat) <- letters

then map dictionary onto your vector然后将字典映射到您的向量上

characterVec <- c("a", "b", "", "d")
numVec <- dat[characterVec]

gives

   a    b <NA>    d 
   1    2   NA    4 

You can remove the vector names with unname() :您可以使用unname()删除向量名称:

numVec <- unname(dat[characterVec])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM