Reputation: 11
I am trying to sort a table that I converted from wide to long using the tidyr and dplyr packages.
data_long <- gather(data_wide, family, abundance, Phietavirus:Phi29virus, factor_key = TRUE )
This is how part of the resulting table looks like data table
The ID's, will be repeated for each type of virus. In total there is 10 type of viruses. I am trying to sort the table by the ID number. I have tried the following code but it gives me an error.
data_long<- data_long[order(data_long$
ID
)] Error: Can't subset columns that don't exist. x Locations 5, 16, 27, 38, 49, etc. don't exist. ℹ There are only 3 columns.
I think the problem might be the repeating ID's, but I am not sure. In order for me to move on with the graph plotting, I need to sort the long table first. If anyone could help figure out how to sort this, it will be appreciated!
Upvotes: 1
Views: 73
Reputation: 1008
I'm not sure what's going wrong with your code (unless you don't actually have a column called exactly ID
), but you could try using arrange()
:
library(dplyr)
data_long <- data_long %>% arrange(ID)
Also, I think you might be missing a comma before the ]
which would leave the result with no columns (and error).
Upvotes: 1