richpiana
richpiana

Reputation: 421

reshape (converting to character)

i have a the following matrix

data=structure(list(Metric = c("x", "y"), Result1 = c(9, 
18), Result2 = c(7, 14
), Delta = c(-170, -401)), .Names = c("Metric", 
"Result_1", "Result_2", "Delta"), row.names = c(NA, 
-2L), class = "data.frame")

I would like to transform it in 1 row and to double the number of columns

I tried the following (as.vector(t(data))). However when I do that it transofrms everything to character and i loose data information

Any help?

Upvotes: 0

Views: 34

Answers (2)

BENY
BENY

Reputation: 323306

In Base R

dd=stack(data)
A=dd$values
names(A)=dd$ind
data.frame(as.list(A))

      Metric Metric.1 Result_1 Result_1.1 Result_2 Result_2.1 Delta Delta.1
1      x        y        9         18        7         14  -170    -401

Upvotes: 1

www
www

Reputation: 39154

We can split the data frame and then use bind_cols from the dplyr package. Although I am not sure this is your expected output as you did not provide any examples, just description.

dplyr::bind_cols(split(data, data$Metric))

  Metric Result_1 Result_2 Delta Metric1 Result_11 Result_21 Delta1
1      x        9        7  -170       y        18        14   -401

Upvotes: 1

Related Questions