r - 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.
解决方案
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))
推荐阅读
- java - Kafka consumerGroup 丢失了所有分区中提交的偏移量信息,并从头开始消费偏移量
- javascript - beforeAll/beforeEach afterAll/afterEach 应该在`describe`里面?
- c# - 该文档没有包含 iTextSharp 的页面
- vb.net - 如何在刽子手游戏中分隔两个单词?
- python - 如何使用 matplotlib 设置多 xticks,多角度旋转
- generics - 当传递给函数中的泛型闭包时,泛型结构的寿命不够长
- go - 如何使取消构建键绑定起作用
- django - 在 Django 应用程序的容器实例中未读取 S3 存储桶的 Heroku 环境变量
- node.js - 如何为服务器生成 vapid-keys?
- python - UnboundLocalError:在 Python 中赋值之前引用了局部变量“x”