Sum different columns in R dataframe -


I have a data frame with 144 columns and 1000 rows. There are 36 different variables in it, there are always 4 values ​​for the value - it seems that:

  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 do I have to do to make another yoga for ever Name the output with the elements of a vector, eg names = c ("AC_syn", "AC_non", ...) .

I think the command should be in some way:

  ddply (a.,), Abbreviation, name [1] = a [, 1] + one [, 2] + one [, 3] + one [, 4], ...)   

But should there be any more elegant way? Too many copy paste without work? I am happy for every thought and a little help!

Sorry I did not clarify this. Actually, I wanted to add the column after every fourth step (1-4, 5-8 .... 141-144) and wanted to rename the new dataframe with the variable given in the vector. Then for input:

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

It should answer

  12 15 37 ..... 20    

If you want to add to every four columns,

  #sample data sets .seed (1) (df & lt; - data.frame (repeat (8, rnorm (5)))) X1 X2 X3 X4 X5 X6 X7 # 1 -06264538 -08204684 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 -08356286 0,7383247 -0,6212406,94383621 0,07456498-1147075238#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 an indicator for columns for the sum - it starts every four columns

  (Ind. Lieutenant; - rep (1: 2, each = 4)) # [1] 1 1 1 1 2 2 2 2   

Yog sum according to Ind AS

  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,] You can then use  colnames  to assign column names.   

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -