Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse vector elements as string in R

Tags:

string

r

vector

I know this is simple, but I cannot find a straightforward solution.

How can I tell the interpreter to read vector contents as string without using quotation marks? Example:

vector<-c("AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH")
vector
[1] "AAA" "BBB" "CCC" "DDD" "EEE" "FFF" "GGG" "HHH"

What if I want to build the same object with:

vector<-c(AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH)
Error: object 'AAA' not found

Do we have some function like "to.character" or something? It would help me much. Thanks in advance, sorry for naive question.

like image 927
Scientist Avatar asked Dec 07 '22 14:12

Scientist


1 Answers

Without quotes, AAA etc will be interpreted as names and an object with this name will be sought for. So you will need nonstandard evaluation (using the argument "as is", without evaluating -- substitute returns the unevaluated expression, and deparse converts it to a string), something like

c__ <- function(...) sapply(substitute(list(...)),deparse)[-1]
vec <-c__(AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH)
vec 
# [1] "AAA" "BBB" "CCC" "DDD" "EEE" "FFF" "GGG" "HHH"
like image 191
lebatsnok Avatar answered Dec 26 '22 10:12

lebatsnok