finding if boolean is ever true by groups in R
A base R option would be
df1$c <- with(df1, ave(b, a, FUN=any))
Or
library(sqldf)
sqldf('select * from df1
left join(select a, b,
(sum(b))>0 as c
from df1
group by a)
using(a)')
R: return one TRUE / FALSE from a group of boolean values
Use all
:
all(abs(mu.iter - mu) > 1.5)
check if a value exists group by id in R
Here is a base R
option with transform
transform(df1, new_var = as.integer(id %in% id[reference>0]))
# id reference new_var
#1 A 0 1
#2 A 1 1
#3 B 0 0
#4 B 0 0
R check if a list of TRUE/FALSE values evaluate to TRUE
Maybe try the all
function:
> x <- c(rep(TRUE, 5), FALSE)
> y <- c(rep(TRUE, 6))
> all(x)
[1] FALSE
> all(y)
[1] TRUE
As its name says, it checks if all the values are TRUE.
Create Aggregate of Boolean Values in R
Try using summarise
as the terminal step in the pipeline:
library(dplyr)
new %>%
group_by(ctfips) %>%
summarise(vote = sum(MinorityTract))
Actually, there is nothing wrong with summing a boolean column, and that should already be giving the correct sums.
Group by and summarise number of boolean values of a column corresponding to each unique value of another column on a specific date
We can subset the 'id' based on the logical vector and get the n_distinct
library(dplyr)
data_frame %>%
group_by(date) %>%
summarise(total_ids =n_distinct(id),
total_true_values = n_distinct(id[boolean_value]))
-output
# A tibble: 3 x 3
# date total_ids total_true_values
# <date> <int> <int>
#1 2020-01-01 1 1
#2 2020-01-02 2 2
#3 2020-01-03 1 0
If ID contains all elements in vector return TRUE for that ID in dplyr
Here, we need filter
with all
. Create a logical vector with %in%
and wrap with all
to get a single TRUE/FALSE output after grouping by 'id'
library(dplyr)
df1 %>%
group_by(id) %>%
filter(all(4:6 %in% time))
NOTE: The OP created a matrix
with cbind
. Inorder to create a data.frame
, use the data.frame
constructor. Also, logical vector TRUE/FALSE
('solution') should be without any quotes i.e. TRUE
or FALSE
instead of "TRUE"
or "FALSE"
data
df1 <- data.frame(id, time)
Related Topics
Does Calculating Correlation Between Two Dataframes Require a Loop
Puzzled by Xlim/Ylim Behavior in R
Function for Polynomials of Arbitrary Order (Symbolic Method Preferred)
Calculate Difference Between Dates by Group in R
Adding a Legend to an Rgl 3D Plot
Ggplot2: Geom_Smooth Confidence Band Does Not Extend to Edge of Graph, Even with Fullrange=True
R Histogram from Frequency Table
Why "Character Is Often Preferred to Factor" in Data.Table for Key
R Shiny: Multiple Use in UI of Same Renderui in Server
Create a Concentric Circle Legend for a Ggplot Bubble Chart
Getting the Minimum of the Rows in a Data Frame
Cannot Install Stringi Since Xcode Command Line Tools Update
Grouped Bar Graph Custom Colours
Count Number of Distinct Values in a Vector