将多个二进制列转换为单个分类列

编程入门 行业动态 更新时间:2024-10-26 16:23:52
本文介绍了将多个二进制列转换为单个分类列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个充满二进制变量的表,我想将其简化为分类变量。

I have a table full of binary variables that I would like to condense down to categorical variables.

非常简单,我有一个像这样的数据框:

Very simplistically, I have is a data frame like this:

data <- data.frame(id=c(1,2,3,4,5,6,7,8,9), red=c("1","0","0","0","1","0","0","0","0"),blue=c("0","1","1","1","0","1","1","1","0"),yellow=c("0","0","0","0","0","0","0","0","1")) data id red blue yellow 1 1 1 0 0 2 2 0 1 0 3 3 0 1 0 4 4 0 1 0 5 5 1 0 0 6 6 0 1 0 7 7 0 1 0 8 8 0 1 0 9 9 0 0 1

我想得到的是:

id color 1 1 red 2 2 blue 3 3 blue 4 4 blue 5 5 red 6 6 blue 7 7 blue 8 8 blue 9 9 yellow

I希望对此有一个非常简单的答案。

I hope there's a really simple answer for this.

推荐答案

您可以通过使用名称和 as.ologic 。但是,由于二进制列是要考虑的因素,因此您需要多做一些准备工作。

You can get the values by making use of the column names and as.logical. However, since your "binary" columns are factors, you need to go though a few more hoops:

> apply(data[-1], 1, function(x) names(x)[as.logical(as.numeric(as.character(x)))]) [1] "red" "blue" "blue" "blue" "red" "blue" "blue" "blue" "yellow"

将此绑定回第一列( data [1] )以获取所需的输出。

Bind this back with the first column (data[1]) to get the output you want.

cbind(data[1], color = apply(data[-1], 1, function(x) names(x)[as.logical(as.numeric( as.character(x)))])) # id color # 1 1 red # 2 2 blue # 3 3 blue # 4 4 blue # 5 5 red # 6 6 blue # 7 7 blue # 8 8 blue # 9 9 yellow

或者,您可以尝试以下操作:

Alternatively, you can try the following:

data[-1] <- lapply(data[-1], function(x) as.numeric(as.character(x))) temp <- subset(cbind(data[1], stack(data[-1])), values == 1, c("id", "ind")) temp[order(temp$id), ]

或者,您可以组合使用 dplyr和 tidyr,例如:

Or, you can use a combination of "dplyr" and "tidyr", like this:

library(dplyr) library(tidyr) data %>% group_by(id) %>% mutate_each(funs(an = as.numeric(as.character(.)))) %>% gather(color, val, -id) %>% filter(val == 1) %>% select(-val) %>% arrange(id) # Source: local data frame [9 x 2] # # id color # 1 1 red # 2 2 blue # 3 3 blue # 4 4 blue # 5 5 red # 6 6 blue # 7 7 blue # 8 8 blue # 9 9 yellow

更多推荐

将多个二进制列转换为单个分类列

本文发布于:2023-10-27 03:44:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   转换为

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!