R根据列值创建1/0 T/F对比度_喜欢_矩阵/新变量 [英] R create a 1/0 T/F contrast-_like_ matrix / new variable based on column value

查看:86
本文介绍了R根据列值创建1/0 T/F对比度_喜欢_矩阵/新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我们如何基于另一列中的唯一值以编程方式创建新变量?

In R, how can we programatically create new variables based on the unique values in another column?

我们可能从中开始的数据框架的简单示例:

A simple example of the data frame where we might start:

structure(list(obsNum = structure(c(1L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 2L, 3L), .Label = c("obs1", "obs10", "obs11", "obs2", 
"obs3", "obs4", "obs5", "obs6", "obs7", "obs8", "obs9"), class = "factor"), 
    charVector = structure(c(1L, 2L, 3L, 2L, 2L, 3L, 1L, 1L, 
    2L, 2L, 3L), .Label = c("blue", "green", "red"), class = "factor")), .Names = c("obsNum", 
"charVector"), class = "data.frame", row.names = c(NA, -11L))

obsNum  charVector
  obs1        blue
  obs2       green
  obs3         red
  obs4       green
  obs5       green
  obs6         red
  obs7        blue
  obs8        blue
  obs9       green
 obs10       green
 obs11         red

以及我想结束的地方:

structure(list(obsNum = structure(c(1L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 2L, 3L), .Label = c("obs1", "obs10", "obs11", "obs2", 
"obs3", "obs4", "obs5", "obs6", "obs7", "obs8", "obs9"), class = "factor"), 
    charVector = structure(c(1L, 2L, 3L, 2L, 2L, 3L, 1L, 1L, 
    2L, 2L, 3L), .Label = c("blue", "green", "red"), class = "factor"), 
    blue = c(1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L), green = c(0L, 
    1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L), red = c(0L, 0L, 
    1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L)), .Names = c("obsNum", 
"charVector", "blue", "green", "red"), class = "data.frame", row.names = c(NA, 
-11L))

obsNum charVector blue green red
  obs1       blue    1     0   0
  obs2      green    0     1   0
  obs3        red    0     0   1
  obs4      green    0     1   0
  obs5      green    0     1   0
  obs6        red    0     0   1
  obs7       blue    1     0   0
  obs8       blue    1     0   0
  obs9      green    0     1   0
 obs10      green    0     1   0
 obs11        red    0     0   1

我非常乐于接受多步骤解决方案,例如:首先创建新变量;然后针对charVec评估每个新变量(名称),一次评估一个变量.假设观察结果的顺序可以保留,创建一个单独的data.frame也可以很好地创建到起始文件中,也完全可以.

I'm very open to multi-step solutions, e.g.: first create the new variables; then evaluate each new variable (name) against charVec, one variable at a time. It's also completely fine to create a separate data.frame that could be cbind 'ed to the start file, assuming the order of the observations would be retained.

先谢谢您了!

推荐答案

cbind(dat, model.matrix(~ . - 1, dat["charVector"]))

##    obsNum charVector charVectorblue charVectorgreen charVectorred                                                                                                                                                                          
## 1    obs1       blue              1               0             0                                                                                                                                                                          
## 2    obs2      green              0               1             0                                                                                                                                                                          
## 3    obs3        red              0               0             1                                                                                                                                                                          
## 4    obs4      green              0               1             0                                                                                                                                                                          
## 5    obs5      green              0               1             0                                                                                                                                                                          
## 6    obs6        red              0               0             1                                                                                                                                                                          
## 7    obs7       blue              1               0             0                                                                                                                                                                          
## 8    obs8       blue              1               0             0                                                                                                                                                                          
## 9    obs9      green              0               1             0                                                                                                                                                                          
## 10  obs10      green              0               1             0                                                                                                                                                                          
## 11  obs11        red              0               0             1  

这篇关于R根据列值创建1/0 T/F对比度_喜欢_矩阵/新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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