[R] Dividing rows in groups

jim holtman jholtman at gmail.com
Sun Apr 24 17:38:09 CEST 2016


You can use 'dplyr':

> library(dplyr)
> df1 <- read.table(text = "ID       A             B
+ 1         1             2
+ 1         0             3
+ 2        5             NA
+ 2         1             3
+ 3         1             4
+ 4         NA           NA
+ 4         0             1
+ 4         3             0
+ 5         2             5
+ 5         7           NA", header = TRUE)
> df2 <- df1 %>%
+         group_by(ID) %>%
+         mutate(new_A = A / sum(A, na.rm = TRUE)
+             , new_B = B / sum(B, na.rm = TRUE)
+             )
>
> df2
Source: local data frame [10 x 5]
Groups: ID [5]

      ID     A     B     new_A new_B
   (int) (int) (int)     (dbl) (dbl)
1      1     1     2 1.0000000   0.4
2      1     0     3 0.0000000   0.6
3      2     5    NA 0.8333333    NA
4      2     1     3 0.1666667   1.0
5      3     1     4 1.0000000   1.0
6      4    NA    NA        NA    NA
7      4     0     1 0.0000000   1.0
8      4     3     0 1.0000000   0.0
9      5     2     5 0.2222222   1.0
10     5     7    NA 0.7777778    NA



Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sun, Apr 24, 2016 at 12:46 AM, Saba Sehrish via R-help <
r-help at r-project.org> wrote:

> Hi
>
>
> I have two data frames as shown below (second one is obtained by
> aggregating rows of similar IDs in df1.). They both have similar number of
> columns but rows of df2 are lesser than rows of df1.
>
>
> df1:
> ID       A             B
> 1         1             2
> 1         0             3
> 2        5             NA
> 2         1             3
> 3         1             4
> 4         NA           NA
> 4         0             1
> 4         3             0
> 5         2             5
> 5         7           NA
>
>
> df2:
> ID       A          B
> 1         1          5
> 2         6          3
> 3         1          4
> 4         3          1
> 5        9          5
>
> Now, to obtain weight of each value of df1, I want to divide each row of
> df1 by the row of df2 having similar ID. What I want is as below:
>
> ID    A    B
> 1    1    0.4
> 1    0    0.6
> 2    0.83  NA
> 2    0.17  1
> 3    1     4
> 4    NA    NA
> 4    0     1
> 4    1     0
> 5    0.22  1
> 5    0.78  NA
>
>
> Kindly guide me in this regard.
>
> Thanks
> Saba
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list