'Change column pattern R

I don't know how to call the pattern of column but I have column like this

Gene         value1     value2
A              10          5
B              52         23
C              14          9

I want to change pattern of columns to be like this

Gene        Type       Value
A          value1        10      
A          value2         5
B          value1        52
B          value2        23
C          value1        14
C          value2         9

Mostly, in ggplot2 always apply this pattern of column in plotting so I am wondering how to write code in R

Thank you in advanced :D

r


Solution 1:[1]

For completeness, the desired transformation can also be obtained in base R with reshape function:

# Sample data
dta <- read.table(text = "Gene         value1     value2
A              10          5
B              52         23
C              14          9",  header = TRUE)

# Transformation
reshape(
    data = dta,
    direction = "long",
    idvar = "Gene",
    varying = c("value1", "value2"),
    v.names = "Value",
    timevar = "Type",
    times = c("value1", "value2")
)

Outcome

         Gene   Type Value
A.value1    A value1    10
B.value1    B value1    52
C.value1    C value1    14
A.value2    A value2     5
B.value2    B value2    23
C.value2    C value2     9

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Konrad