0
votes

I am attempting to create a new column (C) that has TRUE/FALSE value.

There are a number of possible values for Column A, and each also has a TRUE/FALSE value shown in Column B.

For each value of column A, if at least one row is "TRUE" in column B, I want all of column C to be TRUE for that value. For example...

A   B
1   TRUE
1   FALSE
1   FALSE
1   FALSE
2   TRUE
2   FALSE
2   FALSE
3   FALSE
3   FALSE
4   TRUE
4   FALSE
4   FALSE

The return that I want looks like:

A   B      C
1   TRUE   TRUE
1   FALSE   TRUE
1   FALSE   TRUE
1   FALSE   TRUE
2   TRUE   TRUE
2   FALSE   TRUE
2   FALSE   TRUE
3   FALSE   FALSE
3   FALSE   FALSE
4   TRUE   TRUE
4   FALSE   TRUE
4   FALSE   TRUE

The issue is that, while I could enter an ifelse statement to define TRUE/FALSE for each value in column A manually, I have upward of 100 different values in column A.

So, as stated: if at least one row is TRUE per value in column A, all rows for that value need to be defined as TRUE in column C.

I do not know how to get R to perform such an operation, and any advice would be very welcome.

1

1 Answers

2
votes

Here is a base R method with any and grouping with ave

df$c <- ave(df$B, df$A, FUN=any)
df
   A     B     c
1  1  TRUE  TRUE
2  1 FALSE  TRUE
3  1 FALSE  TRUE
4  1 FALSE  TRUE
5  2  TRUE  TRUE
6  2 FALSE  TRUE
7  2 FALSE  TRUE
8  3 FALSE FALSE
9  3 FALSE FALSE
10 4  TRUE  TRUE
11 4 FALSE  TRUE
12 4 FALSE  TRUE

any returns TRUE if any values in the vector are TRUE. This is performed by group with ave.

data

df <- 
structure(list(A = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 
4L, 4L), B = c(TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, 
FALSE, FALSE, TRUE, FALSE, FALSE)), .Names = c("A", "B"),
class = "data.frame", row.names = c(NA, 
-12L))