2012-11-28 48 views
6

如何減去兩個結構相同的列表中R.下面的函數創建數據集減去兩個相同結構清單中的R

dataSet <- function(x,y){ 
    data <- lapply(1:4, function(x) 
      do.call(cbind,do.call(cbind, 
           lapply(lapply(1:5,function(y) 
               cbind(rnorm(10))), data.frame)))) 
    pVariatesNames <- paste("s",seq(from=380, length.out=5),sep="") 
    nCasesNames <- c("Paper","R","G","B") 
    customerNames <- paste("Customer",seq(from=1, length.out=10),sep=" ") 
    names(data) <- nCasesNames 
    for(i in 1:4) { 
     colnames(data[[i]]) <- pVariatesNames 
     rownames(data[[i]]) <- customerNames 
    } 
    return(data) 
} 
dataSet1 <- dataSet(4,5); dataSet1 
dataSet2 <- dataSet(4,5); dataSet2 

我想從dataSet2

減去dataSet1的

回答

10

這取決於結構。在這種情況下,應該可以工作:

mapply('-',dataSet2,dataSet1,SIMPLIFY=FALSE)