Zach
Zach

Reputation: 1396

Parse a long string using gsub and replacement is reserved char in R

I have a character vector of the form (below). When you read this vector it is one long string such as, "tBodyAcc-XYZ\tGravityAcc-XYZ\tBodyAccJerk-XYZ\BodyGyro-XYZ..." where whitespace is split by the escape char "\".

How can I use gsub to replace the white space pattern = " ", replacement = " ", so that each variable name is surrounding by quotes and is an element of the vector?

Ideally, I'd be able to say varnames[1] and get back "tBodyAacc-XYZ".

Thank you for your time on this matter.

varnames <- "tBodyAcc-XYZ
    tGravityAcc-XYZ
    tBodyAccJerk-XYZ
    tBodyGyro-XYZ
    tBodyGyroJerk-XYZ
    tBodyAccMag
    ..."

Upvotes: 2

Views: 338

Answers (1)

akrun
akrun

Reputation: 887951

You could try

scan(text=gsub('\\s+', ' ', varnames), sep='', what='')

Upvotes: 1

Related Questions