Subsetting dataframe based on two values in one column [duplicate] - r

This question already has answers here:
Filter data.frame rows by a logical condition
(9 answers)
Filtering a data frame on a vector [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to subset two years of microclimate data from a larger dataset in R. I can subset one year, but am struggling to subset two years in the same operation.
This operation works fine:
ChamberTemp <- subset(ChamberTemp,
subset=year=="2011",
select=c(year,month,chamber,cat1.avg,cat2.avg,cat3.avg))
How do I subset by two years? i.e. 2011 and 2012
Thank you!

We need %in% instead of == for a vector of length greater than 1
subset(ChamberTemp,
subset=year %in% c("2011", "2012"),
select=c(year,month,chamber,cat1.avg,cat2.avg,cat3.avg))
with dplyr, this can be done using
library(dplyr)
ChamberTemp %>%
filter(year %in% c("2011", "2012")) %>%
select(year, month, chamber, matches("^cat[1-3]\\.avg$"))

Related

How to sort the dataframe by items? [duplicate]

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")))

Create a dataframe from a subset of rows in a larger dataframe [duplicate]

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"))

Dropping dates except specific year from panel data [duplicate]

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)

Extract two rows out of dataframe - r [duplicate]

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.

Filtering Column by Multiple values [duplicate]

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 !

Resources