Category "dplyr"

How do I assign group level value - based on row level values - to df using dplyr

I have the following decision rules: RELIABILITY LEVEL DESCRIPTION LEVEL I Multiple regression LEVEL II Multiple regression + mec

Pivot Longer in R Issues

I've looked quite a few different questions on here around Pivot Longer but I can't seem to figure out how to get my scenario to work. For example R Pivot multi

Randomizing within and across groups using group_by and sample

I'm running a study in which each participant will be presented with stimuli that have been randomized at two different levels: blocks (3 unique blocks) and tri

How to flag time-varying indicators with overlapping dates in a longitudinal data set?

I have a simulated data set with 5 rows, each representing a block of person-time, each with its own start and end date ('start' and 'end'). Each row has a visi

Create a new column based on multiple conditions in other columns in R

I have a data frame that has this structure: dat <- data.frame(col1 = sample(0:3, 10, replace = TRUE), col2 = sample(0:3, 10, replace = TRU

Converting column names so they can be put in an numerical order

I am trying to expand on this answer, by creating a solution that works both on the new_dat and the old_dat. New Data new_dat <- structure(list(`[0,25) east`

How to use created functions argument inside the code?

When I create a function and use arguments as variable names in group_by() function there is error: comb <- function(z,x,y) { df <- z %>% group

Create unique row values in new column based on matching criteria in R

I have a dataframe with one identifier column of unique values, and one column which contains specific criteria. I want to create a new identifier column of uni

Retain all columns after using group_by summarise, and mutatue dplyr on categorical variable and plot barplot with confidence intervals

I'm new to R. This is my dataset df <- tribble( ~Area_of_interst ,~Meds,~Response, "Internal Med", "asprin", "yes", "Inter

How to apply code to dataframe by condition?

I have the following dataframe: library(dplyr) library(tidyverse) library(concordance) Year <- c(2016,2016,2017,2019,2020,2020,2020,2013,2010,2010) Pf <-

How to use mutate() +across() only for specific rows

The data: df <- tribble( ~name, ~val.I, ~val.V, ~`val.%`, "Peter", 123, 12.4, 14, "Peter in %", 111, 532, 57, "Harald", 2222, 3333, 444, "Harald in

Summing across in a dataframe with condition coming from another column

this is not a very good title for the question. I want to sum across certain columns in a data frame for each group, excluding one column for each of my groups.

How to filter out a row if there are two consecutive instances of the same value?

I have a data frame with multiple similar sequences in which column Z has a string pattern containing "VALUE1" and "VALUE2" (only these two patterns matter) and

Multiples rows to one row in R [closed]

In R, I have a data frame with several values. I would like to have a data frame that transforms the data frame into a data frame with just on

Ways to select multiple columns in base R using the native pipe |>?

What are good ways to select multiple columns of a data frame in base R using the native pipe |>? (i.e., without the tidyverse/dplyr to reduce external depen

How to insert rows in specific indices of dataframe containing sum of few rows above only in R pipe dplyr

for dataframe below, df <- data.frame(id = c(rep(101, 4), rep(202, 3)), status = c("a","b","c","d", "a", "b", "c"), wt = c(10

How can I filter rows out if their start date is within 90 days from today and place them out until the 1st of the following month in R?

I am having difficulty finding the words to describe what I am searching for but will try. I would like to solve the following using R or Python (but preferably

How can I use a vector variable to avoid repeating references to non-adjacent columns in R?

Question In R, can I used a vector that holds the names of data frame columns to avoid repeated code? vec_columns <- c("col1", "col2", "col8", "col54") Bac

str_detect removing some but not all strings with specified ending

I'd like to remove any string that ends in either of 2 characters in a pipe. In this example it's ".o" or ".t". Some of them get removed, but not all of them, a

Problem with `mutate()` input `index_date` when performing time series xgboost in R

here my reproducible example mydat=structure(list(supplier = c("TKP", "TKP", "TKP", "TKP", "TKP", "TKP", "TKP", "TKP", "TKP", "TKP", "TKP", "TKP", "TKP",