[R] sum column by colnames

Alfredo Alessandrini alfreale74 at gmail.com
Thu Jan 21 12:02:27 CET 2010


I've try it...

But don't work.

> data1
              1           2          3           4         5           6
5     0.2481509   0.2481509   1.240754   0.7444526  0.000000   0.9926034
10    2.5269063   1.2634532   1.263453   2.5269063  1.263453   2.5269063
15    0.0000000   6.6856351   3.342818   1.6714088  0.000000   1.6714088
20    3.3955301   6.7910603   0.000000   0.0000000  3.395530   3.3955301
25   17.6053685  17.6053685   0.000000   0.0000000  0.000000   5.8684562
30    0.0000000   0.0000000   9.145319   0.0000000  0.000000   9.1453187
35   13.2743249   0.0000000  26.548650  13.2743249  0.000000   0.0000000
40    0.0000000   0.0000000  73.195722   0.0000000  0.000000   0.0000000
45    0.0000000  24.2592465   0.000000  24.2592465  0.000000   0.0000000
50    0.0000000  62.3859139 155.964785  62.3859139  0.000000  31.1929569
55   39.1359376 117.4078128  39.135938   0.0000000  0.000000   0.0000000
60    0.0000000  96.2453820   0.000000  48.1226910 48.122691   0.0000000
65    0.0000000  58.1866603   0.000000  58.1866603  0.000000   0.0000000
70    0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
75  163.3521313   0.0000000  81.676066   0.0000000  0.000000   0.0000000
80    0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
85    0.0000000   0.0000000 109.858092   0.0000000  0.000000   0.0000000
90    0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
95    0.0000000   0.0000000   0.000000 142.9800268  0.000000 142.9800268
100 161.4695650   0.0000000   0.000000   0.0000000  0.000000   0.0000000
105   0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
110   0.0000000 202.4565276   0.000000   0.0000000  0.000000   0.0000000
> data2
           1        4        5          6
5    0.00000  0.00000  0.00000  0.0000000
10   0.00000  0.00000  0.00000  0.5849502
15   0.00000  0.00000  0.00000  0.0000000
20   0.00000  0.00000  0.00000  0.0000000
25   0.00000 10.49815  0.00000  0.0000000
30   0.00000  0.00000  0.00000  0.0000000
35  11.66031 11.66031 46.64124  0.0000000
40  16.03441  0.00000 48.10322 16.0344054
45  21.26234 21.26234 63.78702 63.7870201
50  54.79779 54.79779 27.39890 27.3988967
55 103.49417  0.00000  0.00000 34.4980557
60   0.00000  0.00000 85.22630 42.6131524
65   0.00000 51.79699  0.00000 51.7969933
70  62.10195  0.00000 62.10195  0.0000000
75   0.00000  0.00000  0.00000  0.0000000
80   0.00000  0.00000  0.00000  0.0000000
85   0.00000  0.00000  0.00000  0.0000000
90   0.00000  0.00000  0.00000  0.0000000
95   0.00000  0.00000  0.00000  0.0000000
> m <- merge(data1, data2, by = 0, all = TRUE, sort = FALSE)
> m
   Row.names         1.x           2          3         4.x       5.x
1          5   0.2481509   0.2481509   1.240754   0.7444526  0.000000
2         10   2.5269063   1.2634532   1.263453   2.5269063  1.263453
3         15   0.0000000   6.6856351   3.342818   1.6714088  0.000000
4         20   3.3955301   6.7910603   0.000000   0.0000000  3.395530
5         25  17.6053685  17.6053685   0.000000   0.0000000  0.000000
6         30   0.0000000   0.0000000   9.145319   0.0000000  0.000000
7         35  13.2743249   0.0000000  26.548650  13.2743249  0.000000
8         40   0.0000000   0.0000000  73.195722   0.0000000  0.000000
9         45   0.0000000  24.2592465   0.000000  24.2592465  0.000000
10        50   0.0000000  62.3859139 155.964785  62.3859139  0.000000
11        55  39.1359376 117.4078128  39.135938   0.0000000  0.000000
12        60   0.0000000  96.2453820   0.000000  48.1226910 48.122691
13        65   0.0000000  58.1866603   0.000000  58.1866603  0.000000
14        70   0.0000000   0.0000000   0.000000   0.0000000  0.000000
15        75 163.3521313   0.0000000  81.676066   0.0000000  0.000000
16        80   0.0000000   0.0000000   0.000000   0.0000000  0.000000
17        85   0.0000000   0.0000000 109.858092   0.0000000  0.000000
18        90   0.0000000   0.0000000   0.000000   0.0000000  0.000000
19        95   0.0000000   0.0000000   0.000000 142.9800268  0.000000
20       105   0.0000000   0.0000000   0.000000   0.0000000  0.000000
21       110   0.0000000 202.4565276   0.000000   0.0000000  0.000000
22       100 161.4695650   0.0000000   0.000000   0.0000000  0.000000
           6.x       1.y      4.y      5.y        6.y
1    0.9926034   0.00000  0.00000  0.00000  0.0000000
2    2.5269063   0.00000  0.00000  0.00000  0.5849502
3    1.6714088   0.00000  0.00000  0.00000  0.0000000
4    3.3955301   0.00000  0.00000  0.00000  0.0000000
5    5.8684562   0.00000 10.49815  0.00000  0.0000000
6    9.1453187   0.00000  0.00000  0.00000  0.0000000
7    0.0000000  11.66031 11.66031 46.64124  0.0000000
8    0.0000000  16.03441  0.00000 48.10322 16.0344054
9    0.0000000  21.26234 21.26234 63.78702 63.7870201
10  31.1929569  54.79779 54.79779 27.39890 27.3988967
11   0.0000000 103.49417  0.00000  0.00000 34.4980557
12   0.0000000   0.00000  0.00000 85.22630 42.6131524
13   0.0000000   0.00000 51.79699  0.00000 51.7969933
14   0.0000000  62.10195  0.00000 62.10195  0.0000000
15   0.0000000   0.00000  0.00000  0.00000  0.0000000
16   0.0000000   0.00000  0.00000  0.00000  0.0000000
17   0.0000000   0.00000  0.00000  0.00000  0.0000000
18   0.0000000   0.00000  0.00000  0.00000  0.0000000
19 142.9800268   0.00000  0.00000  0.00000  0.0000000
20   0.0000000        NA       NA       NA         NA
21   0.0000000        NA       NA       NA         NA
22   0.0000000        NA       NA       NA         NA
> sapply(unique(union(names(data1), names(data2))), function(n)Reduce('+', m[grep(n, names(m))]))
list()
>


2010/1/20 Henrique Dallazuanna <wwwhsd at gmail.com>:
> Try this:
>
>> m <- merge(data1, data2, by = 0, all = TRUE, sort = FALSE)
>> sapply(unique(union(names(data1), names(data2))), function(n)Reduce('+', m[grep(n, names(m))]))
>
> On Wed, Jan 20, 2010 at 10:59 AM, Alfredo Alessandrini
> <alfreale74 at gmail.com> wrote:
>> Hi,
>>
>> I've this dataframes:
>>
>>> data1
>>             1        2 3         4             5                6
>> 5    0.4963017 0 0 0.2481509  1.9852069 0.4963017
>> 10   0.0000000 0 0 0.0000000  0.6317266 0.0000000
>> 15   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>> 20   0.0000000 0 0 3.3955301  0.0000000 0.0000000
>> 25   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>> 30   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>> 35   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>
>>> data2
>>           1        4              5                6
>> 5    0.00000  0.00000  0.00000  0.0000000
>> 10   0.00000  0.00000  0.00000  0.5849502
>> 15   0.00000  0.00000  0.00000  0.0000000
>> 20   0.00000  0.00000  0.00000  0.0000000
>> 25   0.00000 10.49815  0.00000  0.0000000
>>
>> Can I sum the dataframe, by colnames?
>>
>> The sum is:
>>
>>             1        2 3         4             5                6
>> 5    0.4963017 0 0 0.2481509  1.9852069 0.4963017
>> 10   0.0000000 0 0 0.0000000  0.6317266 0.5849502
>> 15   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>> 20   0.0000000 0 0 3.3955301  0.0000000 0.0000000
>> 25   0.0000000 0 0 10.498150   0.0000000 0.0000000
>> 30   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>> 35   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>
>>
>> Thanks,
>>
>> Alfredo
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>



More information about the R-help mailing list