将计数转换为r

时间:2017-07-24 13:14:36

标签: r

我的数据集如下所示

df <- data.frame( name = c("a", "b", "c"),
                  judgement1= c(5, 0, NA),
                  judgement2= c(1, 1, NA),
                  judgement3= c(2, 1, NA))

我想重塑数据框看起来像这样

# name  judgement1 judgement2 judgement3
# a         1          0          0
# a         1          0          0
# a         1          0          0
# a         1          0          0
# a         1          0          0
# b         1          0          0  
# b         0          1          0  
# b         0          0          1

等等。我已经看到在其他一些线程上建议无法解决,但它似乎不适用于当前版本的r。是否有可以将汇总计数转换为单独观察的包?

2 个答案:

答案 0 :(得分:1)

我写了一个功能,可以为您提供所需的输出:

untabl <- function(df, id.col, count.cols) {
  df[is.na(df)] <- 0                          # replace NAs
  out <- lapply(count.cols, function(x) {     # for each column with counts
    z <- df[rep(1:nrow(df), df[,x]), ]        # replicate rows
    z[, -c(id.col)] <- 0                      # set all other columns to zero
    z[, x] <- 1                               # replace the count values with 1 
    z
  })
  out <- do.call(rbind, out) # combine the list
  out <- out[order(out[,c(id.col)]),] # reorder (you can change this)
  rownames(out) <- NULL # return to simple row numbers
  out
}

untabl(df = df, id.col = 1, count.cols = c(2,3,4))
#   name judgement1 judgement2 judgement3
#1     a          1          0          0
#2     a          1          0          0
#3     a          1          0          0
#4     a          1          0          0
#5     a          1          0          0
#6     a          0          1          0
#7     b          0          1          0
#8     a          0          0          1
#9     a          0          0          1
#10    b          0          0          1

供您参考,reshape :: untable包含以下代码:

function (df, num) 
{
    df[rep(1:nrow(df), num), ]
}

答案 1 :(得分:1)

您可以尝试这样的事情:

df <- data.frame( name = c("a", "b", "c"),
                  judgement1= c(5, 0, NA),
                  judgement2= c(1, 1, NA),
                  judgement3= c(2, 1, NA))

rep.vec <- colSums(df[colnames(df) %in% paste0("judgement", (1:nrow(df)), sep="")], na.rm = TRUE)
want <- data.frame(name=df$name, cbind(diag(nrow(df))))
colnames(want)[-1] <- paste0("judgement", (1:nrow(df)), sep="")
(want <- want[rep(1:nrow(want), rep.vec), ])