What I tried to do:
In aphid package there is a function deriveHMM() which needs to be fed with a list like:
x <- list(c("c"="10.0", "b"="5.0","c"="10.0", "a"="1.0", "a"="2.0",...))
which 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). Each double has to get a name depending on its closest distance to a predefined value, for example:
and after that all doubles be converted to a character (or string (?)) so the method deriveHMM() accepts the input.
I would be very happy to have suggestions. I am new to R and this is my first post on 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.
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.
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"