0

Suppose I have the following sample data:

ID Group Score
1 A 1
2 A 3
3 A 2
4 B 5
5 B 1
6 C 1
7 C 2
8 C 4
9 D 1
10 D 3

I want to use group by with customized calculation: square root of the sum of squares of each score within each group.

  • The ID is unique. (Each row is unique)
  • There are 100+ groups
  • The calculation is based on variable "Score"
  • the # of rows in each group varies

For example, in the final output, only two columns:

Group AdjustedScore
A        3.74         **Square root of (1+9+4)
B        5.09         **Square root of (25+1)
C        4.58         **Square root of (1+4+16)
......
......

How can this be done in R? I am not good at R, thanks for any help.

azCats
  • 153
  • 13

1 Answers1

1

You can use :

library(dplyr)
df %>% group_by(Group) %>% summarise(Score = sqrt(sum(Score^2)))

#  Group Score
#  <chr> <dbl>
#1 A      3.74
#2 B      5.10
#3 C      4.58
#4 D      3.16

In base R, you can use aggregate :

aggregate(Score~Group, df, function(x) sqrt(sum(x^2)))

Or with data.table :

library(data.table)
setDT(df)[, sqrt(sum(Score^2)), Group]
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213