找回密码
 立即注册
查看: 4154|回复: 3

请教大神:将一张表展开的代码

[复制链接]
发表于 2017-2-6 15:17:34 | 显示全部楼层 |阅读模式
如下矩阵
     A    B    C   
[1,] "A1" "B3" "C1"
[2,] "A2" "B1" "C2"
[3,] "A3" "B3" "C3"
[4,] "A1" "B2" "C3"
[5,] "A3" "B3" "C2"
[6,] "A1" "B1" "C1"
希望通过高效的代码展开为如下形态(将每个观测展开,在具体变量值里记有(1)没有(0)):因为要处理的数据有二十万行左右,每个变量的值也有几个到数十个不等。
         A    B1    B2    B3    C1    C2    C3      
[1,] "A1" "0"   "0"   "1"   "1"   "0"   "0"
[2,] "A2" "1"   "0"   "0"   "0"   "1"   "0"
[3,] "A3" "0"   "0"   "1"   "0"   "0"   "1"
[4,] "A1" "0"   "1"   "0"   "0"   "0"   "1"
[5,] "A3" "0"   "0"   "1"   "0"   "1"   "0"
[6,] "A1" "1"   "0"   "0"   "1"   "0"   "0"

多谢大神了

回复

使用道具 举报

发表于 2017-2-7 13:43:21 | 显示全部楼层
想了半天,我只会循环... 【我是菜鸟】

#定义函数
featureExtend <- function(featureName,originalTable)
{
  originalRange <- originalTable[,featureName]
  nfeatureLevel <- nlevels(factor(originalRange))
  tmp <- matrix(0,nrow=nrow(originalTable),ncol=nfeatureLevel)
  colnames(tmp) <- levels(factor(originalRange))
  for(colIndex in 1:nfeatureLevel)
  {
    tmp[which(originalRange == colnames(tmp)[colIndex]),colIndex] <- 1;
  }
  
  return(tmp)
}

# 执行函数
> cbind(original[,"A"],featureExtend("B",original),featureExtend("C",original))

# 输出 - 少了第一列的列名
          B1  B2  B3  C1  C2  C3
[1,] "A1" "0" "0" "1" "1" "0" "0"
[2,] "A2" "1" "0" "0" "0" "1" "0"
[3,] "A3" "0" "0" "1" "0" "0" "1"
[4,] "A1" "0" "1" "0" "0" "0" "1"
[5,] "A3" "0" "0" "1" "0" "1" "0"
[6,] "A1" "1" "0" "0" "1" "0" "0"
回复

使用道具 举报

发表于 2017-2-7 17:23:49 | 显示全部楼层
感觉可以用table直接做 继续学习吧
Untitled.png
回复

使用道具 举报

发表于 2017-2-7 17:28:44 | 显示全部楼层
> A <- c(1:6)
> B <- c('B3','B1','B3','B2','B3','B1')
> mytest <- cbind(A,B)
> table(mytest[,"A"],mytest[,"B"])

#输出【完美!】
    B1 B2 B3
  1  0  0  1
  2  1  0  0
  3  0  0  1
  4  0  1  0
  5  0  0  1
  6  1  0  0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|R语言中文网

GMT+8, 2024-11-23 01:59 , Processed in 0.028099 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表