Scientist
Scientist

Reputation: 1339

Parse vector elements as string in R

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.

Upvotes: 4

Views: 1794

Answers (5)

lmo
lmo

Reputation: 38500

If you have the object as a character vector of length 1, ie

x <- "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH"

then you can use scan to parse this into a character vector.

myVec <- scan(textConnection(x), what="", sep=",")

Since scan typically expects an external file, you can construct something that will work on the fly with textConnection. The inner workings of connections are still a bit blurry to me, but they can be very helpful when working with large files via chunking. See ?textConnection and ?file for more details on connections.

This returns your desired result.

myVec
[1] "AAA"  " BBB" " CCC" " DDD" " EEE" " FFF" " GGG" " HHH"

Upvotes: 1

Len Greski
Len Greski

Reputation: 10845

As one learns R, it's important to think about the relationship between a vector and a data frame. A data frame can be thought of as a collection of related vectors of a given length. Therefore, another solution to this problem using Base R is to use one of the file read functions (e.g. read.table()) and extract the vector from the resulting data table.

Note that instead of listing the data elements on one line, they must be entered on separate lines.

aString <- "AAA 
BBB 
CCC 
DDD 
EEE 
FFF 
GGG 
HHH"
# use text argument to read the aString object as if it is an external file
aVector <- read.table(text=aString,stringsAsFactors=FALSE)$V1
aVector

...and the output:

> aVector
[1] "AAA" "BBB" "CCC" "DDD" "EEE" "FFF" "GGG" "HHH"
>

This technique enables one to enter data in rows, and read it into a data frame.

# example with multiple vectors per row

aString ="
var1 var2
var1 var2
var1 var2"

read.table(text=aString,stringsAsFactors=FALSE)

...and the output:

> read.table(text=aString,stringsAsFactors=FALSE)
    V1   V2
1 var1 var2
2 var1 var2
3 var1 var2
>

Upvotes: 2

SeGa
SeGa

Reputation: 9809

I'm not sure if you can do that, as R will look for the object AAA which will not be found. But why not just put quotes around the whole thing and then split the vector by ","?

vector<-"AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH"

vec <- strsplit(vector, ",")[[1]]

str(vec)
 chr [1:8] "AAA" " BBB" " CCC" " DDD" " EEE" " FFF" " GGG" " HHH"

Upvotes: 2

lebatsnok
lebatsnok

Reputation: 6449

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"

Upvotes: 5

De Novo
De Novo

Reputation: 7600

You're not trying to coerce anything to character here. You're trying to input a string literal without telling R that it's a string literal. You can coerce another kind of vector to a character vector with as.character(), e.g., as.character(1:10), but there's nothing you can do that will allow R to interpret AAA as "AAA". The interpreter will always look for an object named AAA, if you tell use AAA instead of "AAA".

Upvotes: 4

Related Questions