How to extract all the rows if a level in one column contains all the levels of another column in R?
Multiple ways to do this :
In dplyr
df %>%
group_by(ID) %>%
filter(all(c("Neolone Preservatives", "Optiphen") %in% PRODUCT))
# ID INDUSTRY PRODUCT
# <int> <chr> <chr>
#1 625109 PersonalCare Neolone Preservatives
#2 227047 Pharma Optiphen
#3 625109 PersonalCare Optiphen
#4 227047 Food Neolone Preservatives
In base R :
df[ave(df$PRODUCT, df$ID, FUN = function(x)
all(c("Neolone_Preservatives", "Optiphen") %in% x)) == "TRUE", ]
Extract column data on the basis of another column levels in r
Conducting a t.test in R. It is best practice to convert your factor variable (the 0/1) to a factor before running the t.test. The function assumes each row is a separate person, and they belong to the group specified in Col1.
Col1 <- sample(c(0,1), 50, replace = TRUE)
Col2 <- round(rnorm(50),2)
dat <- data.frame(Col1, Col2)
dat$Col1 <- factor(dat$Col1)
t.test(Col2 ~ Col1, data = dat)
R - How to extract a subset of a data frame containing many observations of two factors, each with multiple levels based on conditions?
Here is a dplyr solution:
library(dplyr)
group_by(df, Alphabet) %>% filter(!(L == 3 & any(L %in% c(1, 2))))
Replace whole values in a column with another column using condition
I think you want to merge the two datasets using the level. Then for each date you would have variable
and depth
, and you can do what you want with that (including overwriting the level
with the depth
, as you suggest).
library(tidyverse)
reference_data <- read_table("depth levels
0.06 1
0.19 2
0.33 3
0.48 4
0.63 5
0.8 6")
data_file <- read_table("Date Levels variable
3-Jan 1 15.25
3-Feb 5 13.09
3-Mar 25 14.21
3-Apr 26 13.65
3-May 27 12.79
3-Jun 27 15.65")
data_file %>%
left_join(reference_data, by = c("Levels" = "levels"))
#> # A tibble: 6 x 4
#> Date Levels variable depth
#> <chr> <dbl> <dbl> <dbl>
#> 1 3-Jan 1 15.2 0.06
#> 2 3-Feb 5 13.1 0.63
#> 3 3-Mar 25 14.2 NA
#> 4 3-Apr 26 13.6 NA
#> 5 3-May 27 12.8 NA
#> 6 3-Jun 27 15.6 NA
Created on 2021-07-14 by the reprex package (v2.0.0)
Extract rows from R data frame based on factors (strings)
Try using the subset
function.
This site provides a good reference:
HowtoInR
my_data = subset(my_data, gender == "male")
filter for rows that meet multiple conditions in a column
After grouping by 'ID', filter
by checking all
the elements of the vector
(c("up", "down")
) are %in%
the column 'DIR'
library(dplyr)
x %>%
group_by(ID) %>%
filter(all(c("up", "down") %in% DIR) )
# A tibble: 2 x 2
# Groups: ID [1]
# ID DIR
# <dbl> <fct>
#1 2 up
#2 2 down
Or using base R
i1 <- with(x, as.logical(ave(as.character(DIR), ID, FUN =
function(x) all(c("up", "down") %in% x))))
x[i1, ]
# ID DIR
#2 2 up
#3 2 down
Related Topics
Importing Data into R from Google Spreadsheet
What Algorithm I Need to Find N-Grams
How to Print R Variables in Middle of String
Cartogram + Choropleth Map in R
How to Make the Horizontal Scrollbar Visible in Dt::Datatable
How Can R Loop Over Data Frames
Get Selected Rows of Rhandsontable
How to Create Binned Factor Variables from a Continuous Variable, with Custom Breaks
How to Create Base R Plot 'Type = B' Equivalent in Ggplot2
How to Find Index of Match Between Two Set of Data Frame
Doing a Plyr Operation on Every Row of a Data Frame in R
Ggplot2: Is There a Fix for Jagged, Poor-Quality Text Produced by Geom_Text()
Parallel Execution of Random Forest in R
Change Stringsasfactors Settings for Data.Frame
Unnest a List Column Directly into Several Columns