Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

extracting word from named vector in r through a variable

$
0
0

I want to extract a specific word from a named vector having set of two words.

freq= c(23,34,45,21)
names(freq) = c("good boy", "bad boy","good car","car good") 
freq

I want all those words starting with good. The following works but i want to pass good in a variable t and then extract it by passing that variable but it's not working. How to tell r that t is a variable and not a character in itself

# it works  
freq[grep("^good",names(freq))]   
# passing in a variable
t <- "good"
freq[grep("^t",names(freq))]  

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>