user17122732
user17122732

Reputation: 57

how do I calculate proportions sqrt transformed by row?

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

Upvotes: 1

Views: 130

Answers (2)

TarJae
TarJae

Reputation: 79204

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  

Upvotes: 2

r2evans
r2evans

Reputation: 160817

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

Upvotes: 3

Related Questions