应用()两次使用不同的指数

时间:2017-03-30 13:26:33

标签: r

这可能是一个微不足道的问题,但我无法找到一个合适的方法来做到这一点。

我的数据框最后是带有病页的

data <- structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("A1", "A10", "A11", 
"A12", "A13", "A14", "A15", "A2", "A3", "A4", "A5", "A6", "A7", 
"A8", "A9"), class = "factor"), B = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("B1", "B2", "B3"
), class = "factor"), C = structure(c(1L, 1L, 12L, 12L, 14L, 
14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 17L, 18L, 19L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L), .Label = c("C1", 
"C10", "C11", "C12", "C13", "C14", "C15", "C16", "C17", "C18", 
"C19", "C2", "C20", "C3", "C4", "C5", "C6", "C8", "C9"), class = "factor"), 
    Desired = c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 
    2L, 2L, 1L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
    12L, 13L, 14L)), .Names = c("A", "B", "C", "Desired"), class = "data.frame", row.names = c(NA, 
-29L))

我的建议

data$Desired <- do.call(rbind(by(data$C,list(data$A),function (x) as.numeric(as.factor(x))))

但是我不知道如何通过()两次(使用B列)来直接获得我想要的输出。有什么帮助吗?

0 个答案:

没有答案