使用reshape包来组合列

时间:2012-06-01 10:09:26

标签: r casting reshape melt

我在数据框中有以下格式的数据:

id a b c d e f x y z
1  0 0 0 1 1 0 1 0 1 
2  1 0 0 1 0 0 1 0 0 

我想要做的是找出a和x在同一个地方有多少次,a和y在同一行中有1,依此类推。基本上,(a,b,c,d,e,f)和(x,y,z)应该被分组为两个变量。

我一直在尝试使用reshape包(使用熔化和强制转换)来做到这一点,我能想到的最好的东西看起来如下:

id a b c d e f x y z
1  1 0 0 0 1 0 10 5 3 
2  0 1 0 1 0 0 25 0 48

我想看到的是这样的:

  x    y    z
a 10   5    3
b 25   0    48

我真的很感激你的帮助。我是重塑包的新手。

此致

阿伦

3 个答案:

答案 0 :(得分:2)

如果您的数据位于数据框data中,则可以执行以下操作:

mdata <- melt(data, measure.vars=c("a","b","c","d","e","f"))
mmdata <- melt(mdata, measure.vars=c("x","y","z"))
colnames(mmdata) <- c("var1","val1","var2","val2")
mmdata <- mmdata[mmdata$val1 & mmdata$val2,]
cast(mmdata, var1~var2)

答案 1 :(得分:1)

这可能是一种解决方案,但可能会有更简单或更清洁的东西。它使用的是双sapply,我认为我不会在某一天使用它:-):

以下是您的数据:

d <- data.frame(a=c(1,0,1),
                b=c(1,0,0),
                c=c(0,1,1),
                x=c(1,1,1),
                y=c(1,0,0),
                z=c(1,0,1))

首先,我们在两组变量之间分割数据:

d1 <- d[,c("a","b","c")]
d2 <- d[,c("x","y","z")]

以下是代码:

tmptab <- function(v1,v2) {
  tab <- as.data.frame(table(v1,v2))
  result <- tab$Freq[tab$v1==1 & tab$v2==1]
  if (is.na(result)) result <- 0
  return(result)
}

sapply(d2, function(v) {
  sapply(d1, tmptab, v)
})

给出了:

  x y z
a 2 1 2
b 1 1 1
c 2 0 1

答案 2 :(得分:0)

挖掘骷髅......

这是一种可能的方法。不确定它是否比其他人有任何改进......

## Save ourselves some typing...
Group1 <- c("a", "b", "c", "d", "e")
Group2 <- c("x", "y", "z")

## melt the data
A <- melt(mydf, id.vars="id")

## Split and merge
B <- Reduce(function(x, y) merge(x, y, by = "id"), 
            split(A, A$variable %in% Group1))

## Subset
B.sub <- B[B$value.x == 1 & B$value.y == 1, c("variable.y", "variable.x")]

## Factor (so table works nicely)
## Without `factor`, rows or columns which are all zeroes
##   will be dropped during the tabulation, which may be fine
B.sub$variable.y <- factor(B.sub$variable.y, Group1)
B.sub$variable.x <- factor(B.sub$variable.x, Group2)

## Tabulate
table(B.sub)
#           variable.x
# variable.y x y z
#          a 1 0 0
#          b 0 0 0
#          c 0 0 0
#          d 2 0 1
#          e 1 0 1
相关问题