karuno
karuno

Reputation: 411

How to insert a vector into one row of a data frame?

I'm trying to insert a character vector into a row of a data frame rather than create a separate row for each value in the data frame. What I have so far:

a<- as.character(c(1:10))


data_frame <- as.data.frame(a)

Instead of 10 observations in 1 variable, I want 1 observation in 1 variable and that 1 observation would look like "1", "2"...."10" where each value in the vector is separated by a comma.

Upvotes: 0

Views: 854

Answers (2)

Seshadri
Seshadri

Reputation: 679

Please try below. "I" is a function that inhibits the interpretation / conversion of objects as indicated by typing "?I" in the console.

data.frame(test = I(list(a)))

Upvotes: 1

YOLO
YOLO

Reputation: 21749

You can simply do transpose vector t:

df = data.frame(t(a))

  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1  1  2  3  4  5  6  7  8  9  10

Upvotes: 1

Related Questions