Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create data frame from another data frame

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading