1

In my R code, a python server returns 2 lists :

For example: columns = ['a','b','c'] and Values = ['mode','median','0']

How can I convert this list to a vector in R? Or convert both of them into a data.frame in R?

I want something like

col <- c('a','b','c')
val <- c('mode','median','0') 

I want to use these as:

df <- data.frame(Column_Name = col, value = val)
3
  • One way I could think of, what if you save them as CSV and then read it the way you want? Commented Jan 24, 2018 at 12:49
  • What do you mean by "In my R code, a python server returns 2 lists"? How do you call a python process from R? Using a particular package? System call? Commented Jan 24, 2018 at 13:06
  • This code is in a RMarkdown report- used in a separate tool, not the typical Rstudio. In this tool, i can connect python code to this report Commented Jan 24, 2018 at 14:05

1 Answer 1

2

I figured out the answer:-
R reads the result I get from python code as a string as given below:-

columns = "['a','b','c']"

So to convert the above into a vector/dataframe following can be done:-

a <- data.frame(Col_Name = unlist(strsplit(gsub("\\[|\\]|\\'",'',columns),',')))

The above code returns a data frame as:-

Col_name
a
b
c
d

Similar process can be done for "Values" and converted into a dataframe

Sign up to request clarification or add additional context in comments.

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.