4

I have a string whose structure and length can keep varying, that is

Input:

X <- ("A=12&B=15&C=15")
Y <- ("A=12&B=15&C=15&D=32&E=53")

What I was looking for this string to convert to data frame

Output Expected:

Dataframe X

 A  B  C
 12 15 15

and Dataframe Y

 A  B  C  D  E
 12 15 15 32 53

What I tired was this:

X <- as.data.frame(strsplit(X, split="&"))

But this didn't work for me, as it created only one column and column name was messed up.

P.S: I cannot hard code the column names because they can vary, and at any given time a string will contain only one row

1
  • 1
    eval(parse(text=paste("data.frame(",gsub("&", ",", X),")"))) I feel bad for suggesting it, but maybe it isn't all that horrible in the current context. Commented Nov 9, 2016 at 5:59

2 Answers 2

5

One option is to extract the numeric part from the string, and read it with read.table. The pattern [^0-9]+ indicates one or more characters that are not a number and replace it with a space in the first gsub, read that using read.table, and specify the column names in the col.names argument with the values got by removing all characters that are not an upper case letter (second gsub)

f1 <- function(str1){
read.table(text=gsub("[^0-9]+", " ", str1), 
         col.names = scan(text=trimws(gsub("[^A-Z]+", " ", str1)), 
             what = "", sep=" ", quiet=TRUE))
 }

f1(X)
#   A  B  C
#1 12 15 15
f1(Y)
#   A  B  C  D  E
#1 12 15 15 32 53
Sign up to request clarification or add additional context in comments.

3 Comments

Works beautifully, will wait for two minutes before accepting yours!
If its not too much to ask, would you mind explaining how your code works, if not for me, others will find it helpful ( one up vote from me for sure )
@USER3875610 Sure, I was going to update it when my laptop gets so slow.
3

You can try this too:

library(stringr)
res <- str_match_all(X, "([A-Z]+)=([0-9]+)")[[1]]
df <- as.data.frame(matrix(as.integer(res[,3]), nrow=1))
names(df) <- res[,2]

df
   A  B  C
1 12 15 15

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.