I have below data frame
df1:
Q1(25%) Q2(50%) Q3(75%)
438.55 654.78 870.34
in df1 Q1(25%), Q2(50%), Q3(75%) are column names.
want to convert the above data frame df1 as below
df2:
quant points
25 438.55
50 654.78
75 870.34
>Solution :
You could use stack() and extract the 2-digit numbers from the quant column.
transform(
setNames(stack(df)[2:1], c("quant", "points")),
quant = as.integer(regmatches(quant, regexpr("\\d{2}", quant)))
)
# quant points
# 1 25 438.55
# 2 50 654.78
# 3 75 870.34
Data
df <- data.frame("Q1(25%)" = 438.55, "Q2(50%)" = 654.78, "Q3(75%)" = 870.34,
check.names = FALSE)