This question already has answers here:
Sort (order) data frame rows by multiple columns
(19 answers)
Closed 3 years ago.
I have a data frame like;
dataframe <- data.frame(country=c("Japan","Korea","China","Japan","Korea","China","Japan","Korea","China"),
count=c(4,5,6,1,2,3,0,2,3))
Now I want to sort by the country like;
dataframe <- data.frame(country=c("Japan","Japan","Japan","Korea","Korea","Korea","China","China","China"),
count=c(4,1,0,5,2,2,6,3,3))
I tried grouped_by function, but it doesn't work.
Please tell me how to do.
You can use the arrange function of the dplyr package:
library(tidyverse)
dataframe <- dataframe %>%
arrange(match(country, c("Japan", "Korea", "China")))
This question already has answers here:
Filter multiple values on a string column in dplyr
(6 answers)
Closed 3 years ago.
I have a large dataframe "Marks", containing marks each year from 2014/5-2017/8. I have separated the dataframe into 4 smaller ones, by year of completion using:
marks14 <-
Marks%>%
filter(YearOfCompletion == "2014/5")
marks15 <-
Marks%>%
filter(YearOfCompletion == "2015/6")
marks16 <-
Marks%>%
filter(YearOfCompletion == "2016/7")
marks17 <-
Marks%>%
filter(YearOfCompletion == "2017/8")
I am attempting now to separate the "2016/7" and "2017/8" marks in to one dataframe. I have tried to manipulate the filter function, but I'm unable to figure it out and I can't find the code for this in online cookbooks.
We can use %in% to filter a vector of dates with length greater than or equal to 1
library(dplyr)
Marks %>%
filter(YearOfCompletion %in% c("2016/7", "2016/8"))
This question already has answers here:
Filter data.frame rows by a logical condition
(9 answers)
Closed 4 years ago.
I have panel data from 2000 to 2017. I want to select rows which are 2005.
Amazingly
mydata <- subset(mydata, select= c(mydata$Year>="2005"))
did not work. Any suggestion?
It is assumed the data is in date or numeric format.
library(dplyr)
df%>%
filter(year>=2005)
Only 2005:
library(dplyr)
flights %>%
filter(year==2005)
This question already has answers here:
Filter data.frame rows by a logical condition
(9 answers)
Closed 4 years ago.
How can I extract two rows (rows a and b below) from my data frame df?
df1 = df[(df$column1 == "a" & "b"),]
Just use the subset function: subset(df, subset = column1 %in% c("a","b"))
Regards.
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 !