Avi
Avi

Reputation: 2283

How to transform into a table

I have the following list of header and numbers:

data <- read.delim(header=TRUE, text='cor 0.7688679 0.7877358 0.8490566 0.8349057 0.7783019')
> data<-as.table (data)
Error in as.table.default(data) : cannot coerce to a table

I would like to get the results into data as if I entered it as followed:

data <- read.table(header=TRUE, text='
  cor 
0.7688679
 0.7877358
 0.8490566
 0.8349057
')

How can I transform it into the read.table format?

Upvotes: 1

Views: 5716

Answers (1)

akrun
akrun

Reputation: 887831

We could replace the space with \n using gsub and read with read.table

df1 <- read.table(text=gsub(' ', '\n', str1), header=TRUE)

Or without replacing with gsub, we scan the string, and then use read.table

df1 <- read.table(text=scan(text=str1, what='', quiet=TRUE), header=TRUE)

To convert to a table object

as.table(as.matrix(df1))

data

str1 <- 'cor 0.7688679 0.7877358 0.8490566 0.8349057 0.7783019'

Upvotes: 2

Related Questions