Extract two rows out of dataframe - r [duplicate] - r

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.

Related

Subset rows which have a rowSums of 0 [duplicate]

This question already has answers here:
How to delete rows where all the columns are zero
(6 answers)
How does the logical negation operator ! work?
(3 answers)
Closed 2 years ago.
Looking to subset a data frame (all columns numeric) based on a condition. I would like the rows which have a rowSum = 0 to be subsetted. Only able to find a solution to subset the rows which don't equal 0!
Would anyone be able to help?
Thanks in advance.
We can use subset with rowSums
subset(df1, rowSums(df1, na.rm = TRUE) == 0)

Subsetting based on multiple conditions in R [duplicate]

This question already has answers here:
Subset of rows containing NA (missing) values in a chosen column of a data frame
(7 answers)
Closed 2 years ago.
I would like to subset my data based on two conditions: if X is blank and if Y is blank.
Subsetting based on 1 condition is:
Blank_X <- Q4[is.na(Q4$X),]
How do I add a second condition to this?
Here is one way with subset
Blank_X <- subset(Q4,is.na(Q4$X) & is.na(Q4$Y))
with filter
Blank_X <- Q4 %>% filter(X!= NA & Y!=NA)
You can use & (and) to combine multiple conditions.
Blank_X <- Q4[is.na(Q4$X) & is.na(Q4$Y),]

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

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

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

Remove selected columns in R dataframe [duplicate]

This question already has answers here:
How do you remove columns from a data.frame?
(12 answers)
Remove an entire column from a data.frame in R
(8 answers)
Closed 6 years ago.
dataframename <- data.frame(
  col1=1:10,
  col2=10:1,
  col3=1:50,
  col4=11:20
)
Consider the above dataframe and I want to remove column 1 and column 4.
1. Without using any package.
2. The answer should be in dataframe format only and not vector results.
###Use subset command:###
dataframename <- subset(dataframename, select = -c(col1,col4) )
###One more approach is you can use list(NULL) to the dataframe:###
dataframename[,c("col1","col4")] <- list(NULL)

Resources