R,将列与行相关 [英] R, relating columns to row

查看:107
本文介绍了R,将列与行相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5列[每个列的名称代表每个候选人的发言。.

I have five columns[each column name represents each candidate say..

can1 can2 can3 can4 can5

,每列都有二进制数据(TRUE或FALSE),而我还有另一列CANDIDATES,其数据收集带有名称5个候选者中(因子= 5)(相同候选者)。
,所以它类似于

, each column has binary data(TRUE OR FALSE) and I have another column-CANDIDATES which has the data collection with names of the 5 candidates(factor=5)(the same candidates). so it is something like

can1 can2 can3 can4 can5 CANDIDATES

我想创建一个二进制的列,如果CANDIDATE的元素和相应的候选列(在第5列中),则该行为true

I want to create column which is binary, in which the row is true if the element of the CANDIDATE and the corresponding candidate column(in the 5 column) is true.. otherwise it must give false.

示例:

can1 can2  can3   can4   can5    CANDIDATES   new_colmn

TRUE TRUE  FASLE  TRUE   FALSE   can2          TRUE
FALSE TRUE FALSE FALSE   FALSE   can4          FALSE
FALSE TRUE TRUE  FALSE   FALSE   can2          TRUE
TRUE TRUE  FALSE FALSE   TRUE    can1          TRUE


推荐答案

我们可以使用矩阵索引创建新列:

We can use matrix indexing to create the new column:

df$new_column <- df[-ncol(df)][cbind(1:nrow(df), match(df$CANDIDATES, names(df)))]






说明

功能n调用 match(df $ CANDIDATES,names(df))是一种将CANDIDATES列与其他列名进行匹配的方法。而 1:nrow(df)仅输出从1到最后一行的序列。我们在一起得到:

The function call, match(df$CANDIDATES, names(df), is a way to match the CANDIDATES column to the other column names. And 1:nrow(df) simply outputs a sequence from 1 to the last row number. Together we get:

cbind(1:nrow(df), match(df$CANDIDATES, names(df)))
     [,1] [,2]
[1,]    1    2
[2,]    2    4
[3,]    3    2
[4,]    4    1

这是一系列的行,列组合。 R的优势之一是能够用两列矩阵对数据帧进行子集化。第一列表示行索引,第二列表示列索引。

This is a series of row, column combinations. One strength of R is the ability to subset a data frame with a two-column matrix. The first column will represent the row index, and the second column indicates the column index.

矩阵子集将强制转换为矩阵,如果我们的输入均为同一类型,则可以。这就是为什么我们仅将数据帧子集为逻辑列 df [-ncol(df)] 的原因。这样,就不会发生类型转换。

The matrix subsetting will coerce to matrix and that's okay if our input is of all the same type. That is why we subset the data frame to only the logical columns df[-ncol(df)]. That way no type conversion will occur.

结果:

df
   can1 can2  can3  can4  can5 CANDIDATES new_column
1  TRUE TRUE FASLE  TRUE FALSE       can2       TRUE
2 FALSE TRUE FALSE FALSE FALSE       can4      FALSE
3 FALSE TRUE  TRUE FALSE FALSE       can2       TRUE
4  TRUE TRUE FALSE FALSE  TRUE       can1       TRUE

这篇关于R,将列与行相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆