Repeat a value within each ID

stata copy value within group
replace missing value by group stata
keep max value by group stata
stata replace group

I have a dataset in R in long format. Each ID does not appear the same number of times (i.e. one ID might be one row, another might appear 79 rows).


ID      V1       V2    
1       B        0    
1       A        1    
1       C        0   
2       C        0    
3       A        0    
3       C        0

I want to create a variable which, if any of the rows for a given ID have Var2 == 1, then 1 repeats for every row of that ID


ID      V1       V2       V3    
1       B        0        1    
1       A        1        1    
1       C        0        1    
2       C        0        0    
3       A        0        0           
3       C        0        0

In base R we can use any - and ave for the grouping.

DF$V3 <- with(DF, ave(V2, ID, FUN = function(x) any(x == 1)))
#  ID V1 V2 V3
#1  1  B  0  1
#2  1  A  1  1
#3  1  C  0  1
#4  2  C  0  0
#5  3  A  0  0
#6  3  C  0  0


DF <- structure(list(ID = c(1L, 1L, 1L, 2L, 3L, 3L), V1 = c("B", "A", 
"C", "C", "A", "C"), V2 = c(0L, 1L, 0L, 0L, 0L, 0L)), .Names = c("ID", 
"V1", "V2"), class = "data.frame", row.names = c(NA, -6L))

copying a value within group, Dear all, I need help about the following issue: Target: In my data there exists a variable, x. Within each group of observations identified by a  Create ID vectors in SAS/IML software. One way to create ID vectors in SAS/IML software is to use the REPEAT function. The REPEAT function creates a matrix from an input vector by repeating the vector a specified number of times horizontally and vertically. For example, the expression T(1:N) is a column vector with N elements.

Here's a tidyverse solution.

If V2 can only be 0 or 1:

df %>%
    group_by(ID) %>%
    mutate(V3 = max(V2))

If you want to check that V2 is exactly 1.

df %>%
    group_by(ID) %>%
    mutate(V3 = as.numeric(any(V2 == 1)))

Examples (REPEATING DATA command), Three variables are defined for each repeating group: MAKE, MODEL, and The task in this example is to read only the repeating data for records with value  To hide duplicates for a many value within groups, group the report, as follows: With the report in Design view, click Sorting and Grouping on the Report Design toolbar. Choose OrderID from the

Another base R option is

df$V3 <- with(df,  +(ID %in% which(rowsum(V2, ID) > 0)))

Grouping and repeating questions, How to group the fields in your forms, for organization or for repeating certain You can organize some or all of your questions into distinct groups. times based on a prior field value, include ${fieldname} in the repeat_count column (​where  Re: Proc Report: Repeating group variable values for each line and for multiple columns Posted 03-16-2018 (3396 views) | In reply to smoorman Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software.

Suppressing repeating values, When you suppress a repeating value, the value displays at the start of each new page and, if you are using groups, each time a value changes in a higher group. When you use the BY statement in the DATA step, the DATA step creates two temporary indicator variables for each variable in the BY statement. The names of these variables are FIRST.variable and LAST.variable, where variable is the name of a variable in the BY statement.

Group rows in a table (Power Query) - Excel, In the Query Editor, you can group the values in various rows into a single value Power Query has two types of Group By operations: aggregate a column with an column only displays the number of row values for each grouped row value. R> rep(1:8, each=20) improve this answer. answered Jun 21 '11 at 21:17. Dirk Eddelbuettel. Dirk Eddelbuettel. 547 silver badges. 632 bronze badges. For your example, Dirk's answer is perfect. If you instead had a data frame and wanted to add that sort of sequence as a column, you could also use group from groupdata2 (disclaimer: my package) to

Repeat item labels in a PivotTable - Excel - Office Support, In the example shown below, the regions are repeated for each row and the product is repeated for each column. Repeating labels. Right-click the row or  The code below shows how to create an id variable that takes on the values 1 to n within each group/panel (where n is the number of cases in a group). The grouping variable is defined by the user. The number of cases, n, need not be the same across all groups. In the example dataset, group is the grouping variable.