I have data that comes out of a DB in a normalized way with a field for year, state, and value.
I would like to do analysis on the data and need it formatted where each year is a field and not a record.So I would like the data where each record is a state and then there's a field for each year and each value for those fields are the value for that year and that state.
Is there a command for doing this?
So I have:
State Year Value
KY 1998 56
KY 1997 78
IL 1998 48
IL 1997 72
and I want:
State 1997_value 1998_value
KY 78 56
IL 72 48
Here is how it looks: Select and copy the needed range. Right-click on a cell where you want to convert rows to columns. Select the Paste Transpose option to rotate rows to columns.
In Excel, to convert any Columns to Rows, first select the column which we want to switch and copy the selected cells or columns. To proceed further, go to the cell where we want to paste the data, then from the Paste option, which is under the Home menu tab, select the Transpose option.
Steps: ➤ Select the data range and then go to the Home Tab >> Styles Group >> Conditional Formatting Dropdown >> Highlight Cells Rules Option >> Duplicate Values Option.
You want to use the reshape()
function.
reshape(data, idvar="State", timevar="Year", direction="wide")
Another option is to use the reshape package, created by the inimitable Hadley Wickham:
library(reshape)
tuna<-melt(data,id.vars=c("State","Year"))
cast(tuna,State~Year~variable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With