Вопрос

I have a data frame with 144 columns and 1000 rows. It contains 36 different variables, always 4 values per variable - it looks that way:

1a-1d   \t 2a-2d   \t 3a-3d..........36a-36d
2 1 4 5 \t 3 4 5 3 \t 32 1 3 1.......3 12 4 1
.
.
4 5 2 6 \t 4 5 2 6 \t 23 5 2 5......3 1 5 6

What I want to do is to sum always a to d and name the output with the elements of a vector, e.g. names=c("AC_syn","AC_non",...).

I think the command must be somethin like:

ddply(a, .(), summarise, names[1]=a[,1]+a[,2]+a[,3]+a[,4], ...)

But there must be a more elegant way? Without too much copy-paste work? I am happy for every idea and little help!

Sorry that I did not make it clearer. Actually I wanted to have the sum of the columns after every fourth step (1-4, 5-8....141-144) and rename the new dataframe with the variables given in a vector. So for the input:

2 1 4 5 \t 3 4 5 3 \t 32 1 3 1.......3 12 4 1

It should reply

12 15 37.....20
Это было полезно?

Решение 2

If you want to sum every every four columns

# example data
set.seed(1)

(df <- data.frame(replicate(8,rnorm(5))))

          X1         X2         X3          X4          X5          X6          X7
#1 -0.6264538 -0.8204684  1.5117812 -0.04493361  0.91897737 -0.05612874  1.35867955
#2  0.1836433  0.4874291  0.3898432 -0.01619026  0.78213630 -0.15579551 -0.10278773
#3 -0.8356286  0.7383247 -0.6212406  0.94383621  0.07456498 -1.47075238  0.38767161
#4  1.5952808  0.5757814 -2.2146999  0.82122120 -1.98935170 -0.47815006 -0.05380504
#5  0.3295078 -0.3053884  1.1249309  0.59390132  0.61982575  0.41794156 -1.37705956
          X8
#1 -0.4149946
#2 -0.3942900
#3 -0.0593134
#4  1.1000254
#5  0.7631757

Create indicator for the columns to sum - this sums every four columns

(ind <- rep(1:2,each=4))
#[1] 1 1 1 1 2 2 2 2

Sum columns according to ind

t(rowsum(t(df),ind))

#              1          2
#[1,] 0.01992536  1.8065336
#[2,] 1.04472535  0.1292631
#[3,] 0.22529172 -1.0678292
#[4,] 0.77758346 -1.4212814
#[5,] 1.74295162  0.4238835

You can then use colnames to assign column names.

Другие советы

A fun way to do it relying on matrix multiplication:

First create an incidence matrix with only zeros and ones to post-multiply your data set (assuming it's called df):

M = matrix(0, 144, 36)
M = (row(M) >= {(col(M)-1)*4 + 1} & row(M) < {(col(M)-1)*4 + 5})*1

Then multiply M by df and name the columns:

sumvar = as.matrix(df) %*% M
names(sumvar) = c("AC_syn","AC_non",...)

sumvar will have 36 columns and 1000 rows. Just in case, M looks like this:

      [,1] [,2] [,3] [,4]...
[1,]    1    0    0    0
[2,]    1    0    0    0
[3,]    1    0    0    0
[4,]    1    0    0    0
[5,]    0    1    0    0
[6,]    0    1    0    0
[7,]    0    1    0    0
[8,]    0    1    0    0
...

Since your data is small, how about a FOR loop. This might be a crude way, but it works nevertheless -

j=seq(1,141,4)
k=j+3
for(i in 1:length(names))
 {
   new_col <- paste("sum_",i)
   ds$new_col <- rowSums(ds[,j[i]:k[i]])   
 }

ds is your dataset name.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top