English 中文(简体)
表格中的工作——以比值和个人价值开展工作
原标题:Working in table - function to work with margin sums and individual values
  • 时间:2011-11-02 03:31:34
  •  标签:
  • r
  • function

在此,我的数据集很小。

set.seed(123)
X1 <- rep(1:3, each = 4)
X2 <- c(1:4, 1:4, 1:4)
Y <- rnorm (12, 4, 20)
myd <- data.frame(X1, X2, Y)

# "unmelten" form of the dataframe 
aggregate(Y ~ X1, myd,  c )
  X1         Y.1         Y.2         Y.3         Y.4
1  1  -7.2095129  -0.6035498  35.1741663   5.4101678
2  2   6.5857547  38.3012997  13.2183241 -21.3012247
3  3  -9.7370570  -4.9132394  28.4816359  11.1962765

实际数据矩阵在层面更大。 我想写的是使用熔炉构成数据的功能,如:

 Example for X1, Y1.1 cell 

  (X1,Y.1) - sum(X1 row) - sum(Y.1 column) + grand total (= sum(Y))
    -7.2095129 - ( -7.2095129 - 0.6035498 + 35.1741663  + 5.4101678) - ( -7.2095129 + 6.5857547 -9.7370570) + sum ( all values in the able)

表明我观点的其他两个例子。

For X2, Y1.1 cell
     (X2,Y.1) - sum(X2 row) - sum(Y.1 column) + grand total (= sum(Y))

For X3, Y.3 cell  
     (X3,Y.3) - sum(X3 row) - sum(Y.3 column) + grand total (= sum(Y))

Similarly this will output a matrix of 3 x 4 size for each cell in the matrix.

我正试图写出这方面的职能,但可以得出任何想法。 页: 1

最佳回答

说明

z1 <- aggregate(Y ~ X1, myd,  c )[,-1]
ans <- z1 - rowSums(z1) - colSums(z1) + sum(z1)

         [,1]      [,2]      [,3]      [,4]
[1,]  64.98307  65.92300  20.13181  34.45743
[2,]  31.60013 106.46100  75.71199 -40.37646
[3,] -17.03576  31.87768 108.41788  85.46648
问题回答

暂无回答




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签