[R] "And" condition spanning over multiple columns in data frame

Eric Berger er|cjberger @end|ng |rom gm@||@com
Thu Sep 12 10:44:54 CEST 2024


Hi,
To rephrase what you are trying to do, you want a copy of all the cp
columns, in which all the NAs become 1s and any other value becomes a
zero. There is an exception for the first row, where the NAs should
become 0s.

a <- c10Dt
b <- matrix(as.numeric(is.na(a[,-1])), nrow=nrow(a))
b[1,] <- 0  # first row gets special treatment
colnames(b) <- paste0("exit",1:ncol(b))
d <- cbind(a,b)
d


On Thu, Sep 12, 2024 at 10:43 AM Francesca <francesca.pancotto using gmail.com> wrote:
>
> Dear contributors,
> I need to create a set of columns, based on conditions of a dataframe as
> follows.
> I have managed to do the trick for one column, but I do not seem to find
> any good example where the condition is extended to all the dataframe.
>
> I have these dataframe called c10Dt:
>
>
>
> id cp1 cp2 cp3 cp4 cp5 cp6 cp7 cp8 cp9 cp10 cp11 cp12
> 1  1  NA  NA  NA  NA  NA  NA  NA  NA  NA   NA   NA   NA
> 2  4   8  18  15  10  12  11   9  18   8   16   15   NA
> 3  3   8   5   5   4  NA   5  NA   6  NA   10   10   10
> 4  3   5   5   4   4   3   2   1   3   2    1    1    2
> 5  1  NA  NA  NA  NA  NA  NA  NA  NA  NA   NA   NA   NA
> 6  2   5   5  10  10   9  10  10  10  NA   10    9   10
> --
>
> Columns are id, cp1, cp2.. and so on.
>
> What I need to do is the following, made on just one column:
>
> c10Dt <-  mutate(c10Dt, exit1= ifelse(is.na(cp1) & id!=1, 1, 0))
>
> So, I create a new variable, called exit1, in which the program selects
> cp1, checks if it is NA, and if it is NA but also the value of the column
> "id" is not 1, then it gives back a 1, otherwise 0.
> So, what I want is that it selects all the cases in which the id=2,3, or 4
> is not NA in the corresponding values of the matrix.
> I managed to do it manually column by column, but I feel there should be
> something smarter here.
>
> The problem is that I need to replicate this over all the columns from cp2,
> to cp12, but keeping fixed the id column instead.
>
> I have tried with
>
> c10Dt %>%
>   mutate(x=across(starts_with("cp"), ~ifelse(. == NA)) & id!=1,1,0 )
>
> but the problem with across is that it will implement the condition only on
> cp_ columns. How do I tell R to use the column id with all the other
> columns?
>
>
> Thanks for any help provided.
>
>
> Francesca
>
>
> ----------------------------------
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list