How to Extract Just the Number from a Named Number (Without the Name)

How do I extract just the number from a named number (without the name)?

For a single element like this, use [[ rather than [. Compare:

coefficients(out)["newx"]
# newx
# 1

coefficients(out)[["newx"]]
# [1] 1

More generally, use unname():

unname(coefficients(out)[c("newx", "(Intercept)")])
# [1] 1.0 1.5

head(unname(mtcars))
# NA NA NA NA NA NA NA NA NA NA NA
# Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

## etc.

How to get the name of a named num in R?

Using names() and supplying the index can help with that.

names(correlationCoeffs[index]) 

What is a named number?

Here is a solution to access the names in order and the name of the maximum :

v <- c(a=1, b=2, c=-1)
names(sort(v))
> [1] "c" "a" "b"
names(which.max(v))
> "b"

Remove names from named vector and get only the values

You just want to remove the names attribute from tmp. There are a number of ways to do that.

You can unname it.

unname(tmp)
# [1] 1 2 3

Or use a very common method for removing names, by setting them to NULL.

names(tmp) <- NULL

Or strip the attributes with as.vector.

as.vector(tmp)
# [1] 1 2 3

Or re-concatenate it without the names.

c(tmp, use.names=FALSE)
# [1] 1 2 3

Or use setNames.

setNames(tmp, NULL)
# [1] 1 2 3

Extract all values from a vector of named numerics with the same name in R

You can subset p.values using [ with names(p.values) == "a" to show all values named a.

p.values[names(p.values) == "a"]
#a a a a a
#1 2 3 4 5

Accessing values in list without names

You could try:

unname(aa_palette)

Update

Based on comment below.

Alternatively you could create aa_palette as a list and then use your code above.
If you run class(aa_palette) on the above you will get character - i.e. a character vector which therefore can't be "unlisted".

aa_palette <- list("#75041a", "#a50026", "#d73027", "#f46d43", "#fdae61", "#fee090", "#ffffbf", 
"#e0f3f8", "#aad0e5", "#abd9e9", "#74add1", "#4575b4", "#313695", "#2a167a", "#989898")
names(aa_palette) <- c("Asx", "Glx", "Ser", "Thr", "His", "Gly", "Arg", "Ala", "Tyr", "Val",
"Met", "Phe", "Leu", "Ile", "Other")

Then this works:

unlist(aa_palette, use.names = FALSE) 

extracting names and numbers using regex

Not a regex expert, however with stringr package we can extract a number pattern with optional "-" in it and replace the "-" with empty string to extract numbers without any "-". For names, we extract the first word at the beginning of the string.

library(stringr)
data.frame(Name = str_extract(phones, "^[A-Za-z]+"),
Number = gsub("-","",str_extract(phones, "[0-9]+[-]?[0-9]+[-]?[0-9]+")))


# Name Number
#1 Ann 077789663
#2 Johnathan 99656565
#3 Maria 099656569

If you want to stick completely with stringr we can use str_replace_all instead of gsub

data.frame(Name = str_extract(phones, "[A-Za-z]+"), 
Number=str_replace_all(str_extract(phones, "[0-9]+[-]?[0-9]+[-]?[0-9]+"), "-",""))

# Name Number
#1 Ann 077789663
#2 Johnathan 99656565
#3 Maria 099656569

How to extract a number from a column with words and numbers in postgresql

This can easily be one using regexp_replace() and removing everything that is not a digit from the input string:

SELECT regexp_replace(details, '[^0-9]+', '', 'g')
FROM transactions
WHERE details LIKE 'Used as payment on order%'

How do I retrieve a simple numeric value from a named numeric vector in R?

You are confusing the printed representation of the numeric value with the actual value. As far as R is concerned, q contains a named numeric vector:

> dat <- rnorm(100)
> q <- quantile(dat)
> q
0% 25% 50% 75% 100%
-2.2853903 -0.5327520 -0.1177865 0.5182007 2.4825565
> str(q)
Named num [1:5] -2.285 -0.533 -0.118 0.518 2.483
- attr(*, "names")= chr [1:5] "0%" "25%" "50%" "75%" ...

All the "named" bit means is that the vector has an attached attribute "names" containing the (in this case) quantile labels. R prints these for a named vector as they are considered helpful to have in printed output if present. But, they in no way alter the fact that this is a numeric vector. You can use these in computations as if they didn't have the "names" attribute:

> q[3] + 10
50%
9.882214

If the names bother you, the unname() function exists to remove them:

> q2 <- unname(q)
> q2
[1] -2.2853903 -0.5327520 -0.1177865 0.5182007 2.4825565

For completeness, I should probably add that you can extract the "names" using the names() function, which also has an assignment version ('names<-'()). So another way to remove the names from a vector is to assign NULL to the names:

> q3 <- q
> names(q3)
[1] "0%" "25%" "50%" "75%" "100%"
> names(q3) <- NULL
> names(q3)
NULL


Related Topics



Leave a reply



Submit