0

I have that in samples 1 the x,y and z values are respectively 100, 200 and 300.

A B C
1 x 100
1 y 200
1 z 300

How can I transpose the table making as a column names the values x, y and z? Keeping ordered the values for each samples of column C? I would like to achieve something like this:

A x y z
1 100 200 300

Thanks!

Luca Corda
  • 33
  • 4

2 Answers2

0
# load package
library(data.table)

# set dataframe as data table
setDT(df)

# pivot long to wide
dcast(df
      , a ~ b
      , value.var = 'c'
      )
Sweepy Dodo
  • 1,522
  • 8
  • 13
0
df <- data.frame(A=c(1,1,1), B=c("x", "y", "z"), C=c(100,200,300))
tidyr::pivot_wider(df, names_from="B", values_from="C")

# A tibble: 1 x 4
      A     x     y     z
  <dbl> <dbl> <dbl> <dbl>
1     1   100   200   300
JKupzig
  • 1,196
  • 2
  • 13