0

I currently have a dataframe that looks like:

State  Area  Metal    Premium
AK     AK01  Gold     500
AK     AK01  Silver   400
AK     AK02  Gold     600
AK     AK02  Silver   500

I am trying to create new columns, such that AK01 is in a single row, with separate columns for each metal. Something like

State  Area  gold_premium  silver_premium
AK     AK01  500           400
AK     AK02  600           500

I was able to create the new columns using

df$gold_premium <- NA
df$silver_premium <- NA

However, I have been unable to move the values into the columns. Any advice helps

fdauspa
  • 11
  • 1

1 Answers1

1

Maybe you can try reshape if you are with base R

reshape(
  df,
  direction = "wide",
  idvar = c("State", "Area"),
  timevar = "Metal"
)

which gives

  State Area Premium.Gold Premium.Silver
1    AK AK01          500            400
3    AK AK02          600            500
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81