关联矩阵可在数据框中定位特定信息

时间:2019-01-04 14:11:51

标签: r

我正在尝试在r中建立一个关联矩阵,这将使我能够检查数据框的选定行中信息的关联。

如果我的数据如下所示:

    A   B   C
01  Cat Hat Car
02  Cat Coat Bike
03  Dog Hat Motorcycle
04  Dog Coat Car

我想使用目标行(在此示例中为A行)创建一个表,以生成如下表:

    Cat Hat Car
02  1   0   0
03  0   1   0
04  0   0   1

有人可以帮忙吗?谢谢

3 个答案:

答案 0 :(得分:3)

这里的技巧是将第一行提取为向量而不是数据帧。一个简单的比较就可以完成工作,即

df[-1,] == as.character(as.vector(df[1,]))
#       A     B     C
#02  TRUE FALSE FALSE
#03 FALSE  TRUE FALSE
#04 FALSE FALSE  TRUE

只需乘以1(一点点编码)即可转换为0/1

(df[-1,] == as.character(as.vector(df[1,])))*1
#   A B C
#02 1 0 0
#03 0 1 0
#04 0 0 1

答案 1 :(得分:0)

使用第1行作为数据框标题,并使用[ifelse]查找文本并替换为数字。

# Create object for dataframe.
cat <- c("Cat", "Dog", "Dog")
hat <- c("Coat", "Hat", "Coat")
car <- c("Bike", "Motorcycle", "Car")
# Create dataframe.
df1 <- data.frame(cat, hat, car)

# Create df2 to add the digit replacements,
# keeping df1 for later comparison.
df2 <- df1

# Use [ifelse] to find text and replace with numerics.
df2$cat <- ifelse(df1$cat == "Cat",1 ,0 )
df2$hat <- ifelse(df1$hat == "Hat",1 ,0 )
df2$car <- ifelse(df1$car == "Car",1 ,0 )

答案 2 :(得分:0)

@Sotos提供的解决方案更加优雅,但是您也可以使用/******** CONTROLLER*******/ (function() { 'use strict'; angular.module('app') .controller('labelController', ['$scope','$http','labelService', function($scope,$http,$labelService) { $scope.labels_portfolioConsultAgentAssignment = {}; $scope.labels_portfolioConsultAgentAssignment=$labelService.getPortfolioConsultAgentLabels(); }]); })(); /********** SERVICE ********/ angular.module('label-service', []) .factory('labelService', function($http) { var labelsPortfolioConsultAgentAssignment = $http.get('scripts/labels_portfolioConsultAgent.properties.txt'); var labels_portfolioConsultAgentAssignmentSerVar = []; return { //====================For PortfolioConsultAgentAssignment WebPage========================// getPortfolioConsultAgentLabels : function(){ labelsPortfolioConsultAgentAssignment.then(function (portfolioConsultAgentAssignmentLabels){ labels_portfolioConsultAgentAssignmentSerVar.PortfolioAssignment= portfolioConsultAgentAssignmentLabels.data.PortfolioAssignment; labels_portfolioConsultAgentAssignmentSerVar.TitleTable= portfolioConsultAgentAssignmentLabels.data.TitleTable; labels_portfolioConsultAgentAssignmentSerVar.IdAgent= portfolioConsultAgentAssignmentLabels.data.IdAgent; labels_portfolioConsultAgentAssignmentSerVar.AgentName= portfolioConsultAgentAssignmentLabels.data.AgentName; labels_portfolioConsultAgentAssignmentSerVar.PromotZone= portfolioConsultAgentAssignmentLabels.data.PromotZone; labels_portfolioConsultAgentAssignmentSerVar.AgentType= portfolioConsultAgentAssignmentLabels.data.AgentType; labels_portfolioConsultAgentAssignmentSerVar.AgentDetail= portfolioConsultAgentAssignmentLabels.data.AgentDetail; labels_portfolioConsultAgentAssignmentSerVar.CountResult = portfolioConsultAgentAssignmentLabels.data.CountResult; labels_portfolioConsultAgentAssignmentSerVar.AgentAssaign= portfolioConsultAgentAssignmentLabels.data.AgentAssaign; //Agents Detail Labels labels_portfolioConsultAgentAssignmentSerVar.AgentDetailID= portfolioConsultAgentAssignmentLabels.data.AgentDetailID; labels_portfolioConsultAgentAssignmentSerVar.AgentDetailDate= portfolioConsultAgentAssignmentLabels.data.AgentDetailDate; labels_portfolioConsultAgentAssignmentSerVar.AgentDetailStatus= portfolioConsultAgentAssignmentLabels.data.AgentDetailStatus; labels_portfolioConsultAgentAssignmentSerVar.AgentDetailConduct= portfolioConsultAgentAssignmentLabels.data.AgentDetailConduct; labels_portfolioConsultAgentAssignmentSerVar.AgentDetailRetainer= portfolioConsultAgentAssignmentLabels.data.AgentDetailRetainer; labels_portfolioConsultAgentAssignmentSerVar.AgentDetailFederativeEntity= portfolioConsultAgentAssignmentLabels.data.AgentDetailFederativeEntity; labels_portfolioConsultAgentAssignmentSerVar.AgentDetailPolicyNumber= portfolioConsultAgentAssignmentLabels.data.AgentDetailPolicyNumber; labels_portfolioConsultAgentAssignmentSerVar.AgentDetailPolicySize= portfolioConsultAgentAssignmentLabels.data.AgentDetailPolicySize; }); return labels_portfolioConsultAgentAssignmentSerVar; } });做这样的事情:

tidyverse

或者:

df[-1, ] %>%
 rename_at(1:3, funs(paste0(as.character(df[1,])))) %>%
 rowid_to_column() %>%
 gather(var, val, -rowid) %>%
 mutate(val = ifelse(val == var, 1, 0)) %>%
 spread(var, val) %>%
 select(-rowid)

  Car Cat Hat
1   0   1   0
2   0   0   1
3   1   0   0

首先,它使用第一行中的值设置列名称。其次,它将数据从宽转换为长。最后,它比较该值是否与列名相同,然后应用给定条件。