I have a data set below. I need to transform them so that variables names are now values
>(p2)
B D F
36.0 38.93 36.06
I need to have like below
p2
Col1 COl2
B 36.0
D 38.93
F 36.06
I have a data set below. I need to transform them so that variables names are now values
>(p2)
B D F
36.0 38.93 36.06
I need to have like below
p2
Col1 COl2
B 36.0
D 38.93
F 36.06
You can use the gather
function from the Tidyverse :
library(tidyverse)
gather(p2, key = "Col1", value = "Col2")
data.frame(Col1= names(p2), Col2 = p2, row.names = NULL)
Data :
p2 <- c(B =36.0, D=38.93, F= 36.06)
If this is a vector:
p2 <- c(B = 36, D = 38.93, F = 36.06)
Then enframe from package tibble could help:
library(tibble)
enframe(p2)
# # A tibble: 3 x 2
# name value
# <chr> <dbl>
# 1 B 36
# 2 D 38.9
# 3 F 36.1
You could change names using
enframe(p2, "Col1", "Col2")