简体   繁体   English

将“双精度”列表的名称创建为“命名向量列表”

[英]Creating names for a list of doubles to a “List of named Vectors”

What I try to do: 我尝试做什么:

In aphid package there is a function deriveHMM() which needs to be fed with a list like: 包有一个函数deriveHMM(),它需要与像列表被馈送:

x <- list(c("c"="10.0", "b"="5.0","c"="10.0", "a"="1.0", "a"="2.0",...))

wich needs to be created of a very large input vector like 需要创建一个非常大的输入向量,如

iv <- c(10, 5, 10, 1, 2,...)

It is important, that the order of my original input vector remains unchanged. 重要的是,我的原始输入向量的顺序保持不变。

I need to automatically create this list by a large input of doubles from a .csv file (import of doubles to R worked fine). 我需要通过.csv文件中的大量双输入自动创建此列表(导入双精度到R工作正常)。 Each double has to get a name depending on its closest distance to a predefined value, for example: 每个double必须根据与预定义值的最近距离获取名称,例如:

all doubles rangig from 0 to 2.5 should be named "a" 所有双打范围从0到2.5应命名为“a”

all doubles ranging from 2.5 to 7.5 should be named "b" 从2.5到7.5的所有双打应命名为“b”

all doubles greater than 7.5 should be named "c" 所有大于7.5的双打都应命名为“c”

and after that all doubles be converted to a character (or string (?)) so the method deriveHMM() accepts the input. 然后将所有双精度转换为字符(或字符串(?)),以便方法deriveHMM()接受输入。

I would be very happy to have suggestions. 我很乐意有建议。 I am new to R and this is my first post on Stackoverflow.com. 我是R的新手,这是我在Stackoverflow.com上的第一篇文章。 I am not an experienced programmer, but I try my best to understand your help. 我不是一位经验丰富的程序员,但我会尽力了解你的帮助。

EDIT: Updated the question, because what i need is a "List of named vectors of characters", exactly like in my example above without changing the order. 编辑:更新了问题,因为我需要的是一个“字符命名向量列表”,完全像我上面的例子而不改变顺序。

This solution uses findInterval to get an index into a tags vector, the vector of names. 此解决方案使用findInterval获取tags向量(名称向量)的索引。

set.seed(1234)    # Make the results reproducible
x <- runif(10, 0, 20)

tags <- letters[1:3]
breaks <- c(0, 2.5, 7.5, Inf)

names(x) <- tags[findInterval(x, breaks)]

x
#         a          c          c          c          c 
# 2.2740682 12.4459881 12.1854947 12.4675888 17.2183077 
#         c          a          b          c          c 
#12.8062121  0.1899151  4.6510101 13.3216752 10.2850228

Edit. 编辑。

If you need x to be of class "character" , get the index into tags first, then coerce x to character and only then assign the names attribute. 如果你需要x为类"character" ,首先将索引转换为tags ,然后将x强制转换为字符,然后再分配names属性。

i <- findInterval(x, breaks)
x <- as.character(x)
names(x) <- tags[i]
x
#                  a                   c                   c 
# "2.27406822610646"  "12.4459880962968"  "12.1854946576059" 
#                  c                   c                   c 
# "12.4675888335332"  "17.2183076711372"  "12.8062121057883" 
#                  a                   b                   c 
#"0.189915127120912"  "4.65101012028754"   "13.321675164625" 
#                  c 
# "10.2850228268653" 

Here is an example, where x represents your input vector. 这是一个例子,其中x代表你的输入向量。

x <- seq(1, 10, 0.5)

The first step is to give your elements names depending on their values. 第一步是根据值给出元素名称。

names(x) <- ifelse(x <= 2.5, "a", ifelse(x > 2.5 & x <= 7.5, "b", "c"))

Next, split your vector and a apply as.character . 接下来,拆分矢量并应用as.character We can use by here. 我们可以使用by这里。

lst <- by(x, names(x), as.character, simplify = TRUE)
is.list(lst)
# [1] TRUE

Result 结果

lst
#names(x): a
#[1] "1"   "1.5" "2"   "2.5"
#----------------------------------------------------------------------------------------------------------------------- 
#names(x): b
# [1] "3"   "3.5" "4"   "4.5" "5"   "5.5" "6"   "6.5" "7"   "7.5"
#----------------------------------------------------------------------------------------------------------------------- 
#names(x): c
#[1] "8"   "8.5" "9"   "9.5" "10" 

You could also use split and lapply as shown below, by is shorthand of such an approach. 你也可以使用splitlapply如下图所示, by这样的方法的简写。

lapply(split(x, names(x)), as.character)

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

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