Order a dataframe by a specific variable [duplicate] - r

This question already has answers here:
Sort (order) data frame rows by multiple columns
(19 answers)
Closed 3 months ago.
I am trying to order a data frame/matrix by the variable VIRUS, by descending or ascending it does not matter.
Anncol<-data.frame(Metadata$VIRUS) ##From left to case, add more if neeedd
sortedAnncol<-Anncol[order(Anncol$VIRUS),]
sAnncol<-as.matrix(sortedAnncol)
This is what I have tried so far, but I lose the first column of data, the corresponding data points in the data frame. How can order the 'Anncol' data frame by the variable 'VIRUS' while simultaneously ordering the rest of the data frame.
Any help would be greatly appreciated! Thank you in advance

Here a solution using dplyr
library(dplyr)
Metadata <- Metadata %>% arrange(VIRUS)

Related

sort dataframe r by column values [duplicate]

This question already has answers here:
Order dataframe in R [duplicate]
(1 answer)
Sort (order) data frame rows by multiple columns
(19 answers)
Closed last year.
I would like to reorder this dataframe based on the values ​​of the "new" column, how can I do? tell me the exact formula, thanks
Substituting "DF" as your data frame, you can use this line of code:
DF[order(DF$new), ]
If you just wanted to view the data frame sorted, you can use
View(DF)
And click the arrows on the column name, which automatically sorts the data frame by ascending/descending.

How to delete rows in r [duplicate]

This question already has answers here:
How do I delete rows in a data frame?
(10 answers)
Closed 1 year ago.
So I've been trying to subset and remove the observations of a country from my data frame (ESS6). I have been able to remove certain variables with this function, -c(variable), but that is not useful since I only want to remove certain rows from the variable countries (cntry).
Thank you for your help :)
Try using dplyr and the "filter" function

How to Extract Two Columns of Data in R [duplicate]

This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 4 years ago.
I have to extract two columns from this data set (Cars93 on MASS) and create a separate folder consisting only of the two columns MPG.highway and EngineSize. How do I go about doing this?
You can look at Cars93 on Mass and just get the first ten rows to see it.
You can create a subset using the names directly using the subset function or alternately,
new_df <- Cars93[,c("MPG.highway","EngineSize")]
#or
new_df <- subset(Cars93, keep = c("MPG.highway","EngineSize"))

trying to isolate two columns in a data frame in r [duplicate]

This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 6 years ago.
Does anybody know of a way to create a new data frame that contains the information of specific columns from a master data frame that has multiple columns? I have a master dataframe and I'm trying to run various tests (regression, ANOVA...etc.,) on specific columns in the data frame. Any suggestions would be greatly appreciated.
if you want to choose columns 3,12 and 15 from the old DF:
newDF <- oldDF[,c(3,12,15)]
if you want to remove columns 3,12 and 15 from the old DF:
newDF <- oldDF[,-c(3,12,15)]

Sorting the data in the dataset [duplicate]

This question already has answers here:
Sort (order) data frame rows by multiple columns
(19 answers)
Closed 6 years ago.
I want to sort a variable in the dataset.
la3 <-order(la1$Id)
Iam getting the output as index. How to get the output as real values in the datatset
la3 <-la1[order(la1$Id),]
The length of the order will correspond to the length of the column and specify the ordered position.
Using an index call of the original data will therefore put the rows in that order.
Using dplyr
library(dplyr)
la1 %>%
arrange(Id)

Resources