首页 > 解决方案 > How to create a new variable that shows different combinations of 4 dummy variables?

问题描述

I have 4 dummy variables taking values 0 or 1 corresponding to the adoption or not of a certain technology. The data frame has over 14000 rows.

I want to loop over these 4 columns to give me the different combinations of == 1 into a new variable.

Data

structure(list(tech1 = structure(c(2L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), tech2 = structure(c(2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"), tech3 = structure(c(1L, 1L, 2L, 1L), .Label = c("0", "1"), class = "factor"), tech4 = structure(c(1L, 1L, 2L, 1L), .Label = c("0", "1"), class = "factor")), row.names = c(NA, 4L), class = "data.frame")

As different combinations are possible, my new variable should contain the information of which technologies each row indicates, that is, of the 4 technologies, which ones were adopted in each case.

Here is how the four first rows of the new variable could look like at the end (supposing "12" = adopted technologies 1 and 2 and so on):

Variable "Tech":

structure(list(Tech = structure(c(1L, 2L, 3L, 4L), .Label = c("12", "2", "234", "2"), class = "factor")),row.names = c(NA, 4L), class = "data.frame")

I have seen some functions that could work (e.g. aggregate), but I haven't found a solution so far.

标签: rloopsaggregatedummy-variable

解决方案


Without knowing what your desired end state is, with the apply function you can generate a list by row of the 1's in each column and a list by column of the 1's in each row.

m <- matrix(sample(0:1, 100, replace = TRUE), ncol = 4)
rows <- apply(m, 1, function(x) which(x == 1))
cols <- apply(m, 2, function(x) which(x == 1))

推荐阅读