R 将二进制矩阵的单列拆分为多列

R 将二进制矩阵的单列拆分为多列,r,binary,R,Binary,我在R中有一个很大的数据集,其中几个人在一个区域的一列中的几行中列出 ID Elevation Year Individual.code 1 Area1 11.0 2009 AA 2 Area1 11.0 2009 AB 3 Area3 79.5 2009 AA 4 Area3 79.5 2009 AC 5 Area3 7

我在R中有一个很大的数据集,其中几个人在一个区域的一列中的几行中列出

      ID Elevation Year Individual.code
1  Area1      11.0 2009              AA
2  Area1      11.0 2009              AB
3  Area3      79.5 2009              AA
4  Area3      79.5 2009              AC
5  Area3      79.5 2009              AD
6  Area5      57.5 2010              AE
7  Area5      57.5 2010              AB
8  Area7     975.0 2011              AA
9  Area7     975.0 2011              AB
我想通过将“单个代码”拆分成二进制矩阵来创建一个矩阵,而不丢失其余变量,即ID、高程和年份

#     ID Elevation Year AA AB AC AD AE
#1 Area1      11.0 2009  1  1  0  0  0
#2 Area3      79.5 2009  1  0  1  1  0
#3 Area5      57.5 2010  0  1  0  0  1
#4 Area7     975.0 2011  1  1  0  0  0
这里有一种方法:

dat <- read.table(text = "      ID Elevation Year Individual.code
1  Area1      11.0 2009              AA
2  Area1      11.0 2009              AB
3  Area3      79.5 2009              AA
4  Area3      79.5 2009              AC
5  Area3      79.5 2009              AD
6  Areas      57.5 2010              AE
7  Area5      57.5 2010              AB
8  Area7     975.0 2011              AA
9  Area7     975.0 2011              AB", header = TRUE)

if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(qdapTools, dplyr)

mtabulate(split(dat[["Individual.code"]], dat[["ID"]])) %>%
    matrix2df("ID") %>%
    left_join(distinct(select(dat, -Individual.code)), .)  

##      ID Elevation Year AA AB AC AD AE
## 1 Area1      11.0 2009  1  1  0  0  0
## 2 Area3      79.5 2009  1  0  1  1  0
## 3 Area5      57.5 2010  0  1  0  0  1
## 4 Area7     975.0 2011  1  1  0  0  0
你可以试试dplyr/tidyr

或者,可以使用“从基础R重塑”


您好@Roland,我正在尝试这段代码,收到的错误消息是:unexpected in fun.aggregate=functionx as.integerlengthx>0我怀疑您正试图运行该行。但是,它属于之前的线路,必须与它一起运行或直接在它之后运行。
dat <- read.table(text = "      ID Elevation Year Individual.code
1  Area1      11.0 2009              AA
2  Area1      11.0 2009              AB
3  Area3      79.5 2009              AA
4  Area3      79.5 2009              AC
5  Area3      79.5 2009              AD
6  Areas      57.5 2010              AE
7  Area5      57.5 2010              AB
8  Area7     975.0 2011              AA
9  Area7     975.0 2011              AB", header = TRUE)

if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(qdapTools, dplyr)

mtabulate(split(dat[["Individual.code"]], dat[["ID"]])) %>%
    matrix2df("ID") %>%
    left_join(distinct(select(dat, -Individual.code)), .)  

##      ID Elevation Year AA AB AC AD AE
## 1 Area1      11.0 2009  1  1  0  0  0
## 2 Area3      79.5 2009  1  0  1  1  0
## 3 Area5      57.5 2010  0  1  0  0  1
## 4 Area7     975.0 2011  1  1  0  0  0
library(dplyr)
library(tidyr)
spread(dat, Individual.code, Individual.code) %>% 
                  mutate_each(funs((!is.na(.))+0L), AA:AE)   
#     ID Elevation Year AA AB AC AD AE
#1 Area1      11.0 2009  1  1  0  0  0
#2 Area3      79.5 2009  1  0  1  1  0
#3 Area5      57.5 2010  0  1  0  0  1
#4 Area7     975.0 2011  1  1  0  0  0
 res <- reshape(cbind(dat, Col=1), idvar=c('ID', 'Elevation', 'Year'), 
           timevar='Individual.code', direction='wide')
 res[is.na(res)] <- 0