0

The data is given as below.

x1<- c(1,0,0,0,1,1,1,0)
x2<- c(1,0,0,0,1,1,1,0)
x3<- c(1,0,0,0,1,1,1,0)
x4<- c(1,0,0,0,1,1,1,0)
x5<- c(1,0,0,0,1,1,1,0)
x6<- c(1,0,0,0,1,1,1,0)

my_data <- as.data.frame(cbind(x1, x2, x3, x4, x5, x6))

I want to use a loop to automate the following process:

my_data$a1 = ifelse(my_data$x1> 0 & is.na(my_data$x1) != T, 1, 0)
my_data$a2 = ifelse(my_data$x2> 0 & is.na(my_data$x2) != T, 1, 0)
my_data$a3 = ifelse(my_data$x3> 0 & is.na(my_data$x3) != T, 1, 0)
my_data$a4 = ifelse(my_data$x4> 0 & is.na(my_data$x4) != T, 1, 0)
my_data$a5 = ifelse(my_data$x5> 0 & is.na(my_data$x5) != T, 1, 0)
my_data$a6 = ifelse(my_data$x6> 0 & is.na(my_data$x6) != T, 1, 0)

Any help would be appreciated, thanks!

4 Answers 4

1

You can use the following code -

my_data[paste0('a', seq_along(my_data))] <-  +(my_data > 0 & !is.na(my_data))
my_data

#  x1 x2 x3 x4 x5 x6 a1 a2 a3 a4 a5 a6
#1  1  1  1  1  1  1  1  1  1  1  1  1
#2  0  0  0  0  0  0  0  0  0  0  0  0
#3  0  0  0  0  0  0  0  0  0  0  0  0
#4  0  0  0  0  0  0  0  0  0  0  0  0
#5  1  1  1  1  1  1  1  1  1  1  1  1
#6  1  1  1  1  1  1  1  1  1  1  1  1
#7  1  1  1  1  1  1  1  1  1  1  1  1
#8  0  0  0  0  0  0  0  0  0  0  0  0

This will assign 1 where the value is greater than 0 and is not NA. my_data > 0 & !is.na(my_data) returns a logical value (TRUE/FALSE) adding + ahead of it turns them to integers (1/0).

Sign up to request clarification or add additional context in comments.

Comments

0

You can use following for loop

for (i in 1:ncol(my_data)) {
 
   my_data[,paste0("a",i)] <- ifelse(my_data[,i] > 0 & !is.na(my_data[,i]),1,0)
  
}

Output

  x1 x2 x3 x4 x5 x6 a1 a2 a3 a4 a5 a6
1  1  1  1  1  1  1  1  1  1  1  1  1
2  0  0  0  0  0  0  0  0  0  0  0  0
3  0  0  0  0  0  0  0  0  0  0  0  0
4  0  0  0  0  0  0  0  0  0  0  0  0
5  1  1  1  1  1  1  1  1  1  1  1  1
6  1  1  1  1  1  1  1  1  1  1  1  1
7  1  1  1  1  1  1  1  1  1  1  1  1
8  0  0  0  0  0  0  0  0  0  0  0  0

Comments

0

We can use tidyverse

library(dplyr)
library(stringr)
df <-  my_data %>% 
    mutate(across(everything(), ~ +(. > 0 & !is.na(.)), 
     .names = "a{.col}")) %>% 
    rename_with(~ str_remove(., 'x'), starts_with('a'))
df
  x1 x2 x3 x4 x5 x6 a1 a2 a3 a4 a5 a6
1  1  1  1  1  1  1  1  1  1  1  1  1
2  0  0  0  0  0  0  0  0  0  0  0  0
3  0  0  0  0  0  0  0  0  0  0  0  0
4  0  0  0  0  0  0  0  0  0  0  0  0
5  1  1  1  1  1  1  1  1  1  1  1  1
6  1  1  1  1  1  1  1  1  1  1  1  1
7  1  1  1  1  1  1  1  1  1  1  1  1
8  0  0  0  0  0  0  0  0  0  0  0  0

Comments

0

data.table

x1<- c(1,0,0,0,1,1,1,0)
x2<- c(1,0,0,0,1,1,1,0)
x3<- c(1,0,0,0,1,1,1,0)
x4<- c(1,0,0,0,1,1,1,0)
x5<- c(1,0,0,0,1,1,1,0)
x6<- c(1,0,0,0,1,1,1,0)

my_data <- as.data.frame(cbind(x1, x2, x3, x4, x5, x6))
library(data.table)
setDT(my_data)[, (paste0("a", seq_len(length(names(my_data))))) := lapply(.SD, function(x) ifelse(x > 0 & !is.na(x), 1, 0))][]
#>    x1 x2 x3 x4 x5 x6 a1 a2 a3 a4 a5 a6
#> 1:  1  1  1  1  1  1  1  1  1  1  1  1
#> 2:  0  0  0  0  0  0  0  0  0  0  0  0
#> 3:  0  0  0  0  0  0  0  0  0  0  0  0
#> 4:  0  0  0  0  0  0  0  0  0  0  0  0
#> 5:  1  1  1  1  1  1  1  1  1  1  1  1
#> 6:  1  1  1  1  1  1  1  1  1  1  1  1
#> 7:  1  1  1  1  1  1  1  1  1  1  1  1
#> 8:  0  0  0  0  0  0  0  0  0  0  0  0

Created on 2021-06-06 by the reprex package (v2.0.0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.