0

I have a following dataset (df):

ID IS_1 IS_2 IS_3
1 1 NA NA
2 NA 1 1
3 NA NA 1
4 NA 1 NA
5 1 1 1

The question is, I've been trying to make a new column called Result to find out what are the popular combinations of various columns (in real dataset I have about 24 of them, all numerical) with the following code:

i <- 1
for (i in 1:nrow(df)){
  df$Result[i] <- paste(df[i, c(2:4)], collapse = ";")
  i <- i + 1
}

The expected result is something like following, since the position of the IS_... matters:

ID Result
1 1;NA;NA
2 NA;1;1
3 NA;NA;1
4 NA;1;NA
5 1;1;1

But it computes proximately 150 000 rows per hour. Can anyone suggest a faster way?

0 Answers0