sort dataframe r by column values [duplicate] - r

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.

Related

Trouble converting Values in Column into Row Names of Data Frame in R [duplicate]

This question already has answers here:
Why am I getting X. in my column names when reading a data frame?
(5 answers)
data.frame without ruining column names
(2 answers)
Closed last month.
I am trying to convert the first column of a data frame as Row names.
It works fine but the names of the data frame format changes!
It changes from like 100-21-0 to X100.21.0
First column is Character values: Code, CBT, DQY, DQX etc.
and the names (or the first row?) of the data frame (double) like: Code, 100-21-0, 1002-84-2, 100-47-0 etc.
Code
100-21-0
CBT
0
DQY
1
I am using the code similar to:
newdataframe <- data.frame(dataframe, row.names = 1)
It works fine but the names of the data frame change from 100-21-0, 1002-84-2, 100-47-0 to X100.21.0, X1002.84.2, X100.47.0 !!!
I am confused why? Can anyone help on this?

Order a dataframe by a specific variable [duplicate]

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)

How to recalculate columns and store new values in a same dataframe? [duplicate]

This question already has answers here:
How can I divide one column of a data frame through another?
(2 answers)
Closed 3 years ago.
The below link shows my initial data frame.
The inc_per column has wrong values in it. so I want to recalculate this and update data frame with new values in it. So I tried this code:
cars<- within(cars,cars$per_inc<- (cars$oldprice / cars$newprice)*100)
But it created unnecessary columns and I didn't get my desired output
My desired output can be seen below:
it is very easy use
cars$per_inc <- (cars$oldprice / cars$newprice)*100
this will replace the current column values with new one.

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)

How can I keep only rows with a certain name in R? [duplicate]

This question already has answers here:
Subsetting matrices
(3 answers)
Closed 8 years ago.
I have a large data set in the form of a matrix, each row has its own unique name. I have a list of row names where I want to keep that data in that row. Is there a way that I can keep only the rows of my matrix that have row names in common with my list of row names? i.e. Can I throw out any row that does not have a row name in my list and be left with a matrix of the rows with names from my list?
Any help would be greatly appreciated! My current method is slow and very circuitous.
if dat is your data frame and names.to.keep is a vector containing the names of the rows which you want, then
dat.keep = dat[rownames(dat) %in% names.to.keep, ]
should do what you want.

Resources