Home > Back-end >  how do I calculate proportions sqrt transformed by row?
how do I calculate proportions sqrt transformed by row?

Time:10-29

My data looks like this. how do I calculate proportions (sqrt transformed) by row (per Id)? This is a large dataset of over 10 000 records.

Id dogs cats birds fish 602 1 0 1 1 603 0 1 1 1 603 1 1 1 0

CodePudding user response:

Up front, I think something like this:

sqrt(dat[,-1] / rowSums(dat[,-1]))
#          Ap        Ak        Al        Aj
# 1 0.5773503 0.0000000 0.5773503 0.5773503
# 2 0.0000000 0.5773503 0.5773503 0.5773503
# 3 0.5773503 0.5773503 0.5773503 0.0000000

Step-through:

dat[,-1]
#   Ap Ak Al Aj
# 1  1  0  1  1
# 2  0  1  1  1
# 3  1  1  1  0

rowSums(dat[,-1])
# [1] 3 3 3

dat[,-1] / rowSums(dat[,-1])
#          Ap        Ak        Al        Aj
# 1 0.3333333 0.0000000 0.3333333 0.3333333
# 2 0.0000000 0.3333333 0.3333333 0.3333333
# 3 0.3333333 0.3333333 0.3333333 0.0000000

CodePudding user response:

Here is an alternative way with dplyr

library(dplyr)
df %>% 
  rowwise() %>% 
  mutate(across(starts_with("A"), ~sqrt(./sum(c_across(Ap:Aj)))))
  Id    Ap    Ak    Al    Aj
  <int> <dbl> <dbl> <dbl> <dbl>
1   602 0.577 0     0.577 0.577
2   603 0     0.577 0.577 0.577
3   603 0.577 0.577 0.577 0  
  •  Tags:  
  • r
  • Related