Reputation: 136
I have a data frame in r that looks like this:
Wave_Expo Tide_Zone avg
1 HighEx High 5.750000
2 HighEx Low 5.333333
3 HighEx Mid 7.833333
4 LowEx High 4.083333
5 LowEx Low 5.916667
6 LowEx Mid 4.916667
7 MidEx High 4.583333
8 MidEx Low 5.833333
9 MidEx Mid 4.090909
i've been trying to rearrange it by the Tide_Zone
column so it goes low,mid,high whilst keeping the exposure column in order but have had no luck. I feel there must be some simple way. Any help?
Upvotes: 2
Views: 62
Reputation: 146174
Specify the order of each factor using levels
:
dat$Tide_Zone = factor(dat$Tide_Zone, levels = c("Low", "Mid", "High"))
dat$Wave_Expo = factor(dat$Wave_Expo, levels = c("HighEx", "LowEx", "MidEx"))
# the default is alphabetical, which is how your Wave_Expo column are ordered
# I included that line just to make it clear, you could change it...
Reorder the data sorting first by Wave_Expo, then Tide_Zone.
dat = dat[order(dat$Wave_Expo, dat$Tide_Zone), ]
If they start as characters, I would recommend doing this process, then adding an id
column in this desired order, and then converting back to character
if needed. Then, if you need to re-order again, this order is preserved in the id
column.
Upvotes: 2