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)
Related
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)
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.
This question already has answers here:
Why does apply convert logicals in data frames to strings of 5 characters?
(2 answers)
Selecting only numeric columns from a data frame
(12 answers)
Closed 2 years ago.
I know that the question is very easy, but I have a more specific one:
I have a data frame, with 50 variables (numeric and non-numeric) and 5000 observations.
Now what I want to do is create another data frame containing only the numerica variables of the original one.
On this website I found the solution of my problem, that is:
numeric_variables<-unlist(lapply(original_data,is.numeric))
X<-original_data[numeric_variables]
But I was wondering: why if I try like this, it does not work instead? what's wrong?
numeric_variables2<-apply(original_data,2,is.numeric)
x<-original_data[numeric_variables2]
try this :
names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]
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"))
This question already has answers here:
Filter multiple values on a string column in dplyr
(6 answers)
Closed 2 years ago.
I would like to filter values based on one column with multiple values.
For example, one data.frame has s&p 500 tickers, i have to pick 20 of them and associated closing prices. How to do it?
If I understand well you question, I believe you should do it with dplyr:
library(dplyr)
target <- c("Ticker1", "Ticker2", "Ticker3")
filter(df, Ticker %in% target)
The answer can be found in https://stackoverflow.com/a/25647535/9513536
Cheers !