Home > Back-end >  Combine lists based on values
Combine lists based on values

Time:04-01

I've got a dataframe in R (inputdata) with two columns (inputdata$col1, inputdata$col2) that I wish to combine.

The values of the list contain either the values 1 or 2 signifying cases & controls respectively.

If either consists of a 1, I wish the newlist to consist of a 1 but if both contain a 2 the newlist should contain a 2. How can I do this?

CodePudding user response:

We may use pmin - which returns the minimum value per row (assuming there are no other values except 1 and 2, missing values- NA are taken care of with na.rm = TRUE)

with(inputdata, pmin(col1, col2, na.rm = TRUE))
[1] 1 1 1 2 1

data

inputdata <- data.frame(col1 = c(1, 1, 2, 2, 2), col2 = c(1, 2, 1, 2, 1))

CodePudding user response:

An easy-to-follow approach is just to use Boolean comparisons (in fact, if you assigned your case/controls to TRUE/FALSE, it is even easier - but the way shown below could also be used for more than two classes).

Using the input data frame from akrun:

is1 <- inputdata[, 1] == 1 | inputdata[, 2] == 1
is2 <- inputdata[, 1] == 2 & inputdata[, 2] == 2

inputdata$newcol[is1] = 1
inputdata$newcol[is2] = 2

inputdata$newcol # This column now has your result.

Unassigned positions will be NA and you can check for these with which(is.na(inputdata$newcol)) to make sure the data are consistent.

  • Related