Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply prop.test to each row in a dataframe

Tags:

r

Beginner here: I'm trying to apply the prop.test function in r to each row of a dataframe. If I have a large table, how can I apply the function row-wise without referencing each element? I tried the broom::tidy function but this provides only the proportion and I need the 95% confidence intervals.

> a<-data.frame(x=c(1,2,3,4),y=c(5,6,7,8))
> z=rowSums(a)
> b<-cbind(a,z)
> b
  x y  z
1 1 5  6
2 2 6  8
3 3 7 10
4 4 8 12

# inefficient code:
prop.test(b[1,1],b[1,3])
prop.test(b[2,1],b[2,3])
prop.test(b[3,1],b[3,3])
prop.test(b[4,1],b[4,3])

# doesn't provide 95% confidence intervals
broom::tidy(prop.test(b$x, b$z))
like image 783
Kris G Avatar asked Sep 20 '25 13:09

Kris G


1 Answers

We may need rowwise operation instead of applying prop.test on the entire columns

library(dplyr)
library(tidyr)
library(broom)
b %>%
     rowwise %>%
      summarise(out = list(prop.test(x, z) %>%
             tidy)) %>%
      ungroup %>%
      unnest(cols = c(out))

-output

# A tibble: 4 x 8
  estimate statistic p.value parameter conf.low conf.high method                                               alternative
     <dbl>     <dbl>   <dbl>     <int>    <dbl>     <dbl> <chr>                                                <chr>      
1    0.167      1.5    0.221         1  0.00876     0.635 1-sample proportions test with continuity correction two.sided  
2    0.25       1.12   0.289         1  0.0445      0.644 1-sample proportions test with continuity correction two.sided  
3    0.3        0.9    0.343         1  0.0809      0.646 1-sample proportions test with continuity correction two.sided  
4    0.333      0.75   0.386         1  0.113       0.646 1-sample proportions test with continuity correction two.sided  

Or in base R, it would be Map

do.call(rbind, Map(\(x, y) tidy(prop.test(x, y)), b$x, b$z))

-output

# A tibble: 4 x 8
  estimate statistic p.value parameter conf.low conf.high method                                               alternative
     <dbl>     <dbl>   <dbl>     <int>    <dbl>     <dbl> <chr>                                                <chr>      
1    0.167      1.5    0.221         1  0.00876     0.635 1-sample proportions test with continuity correction two.sided  
2    0.25       1.12   0.289         1  0.0445      0.644 1-sample proportions test with continuity correction two.sided  
3    0.3        0.9    0.343         1  0.0809      0.646 1-sample proportions test with continuity correction two.sided  
4    0.333      0.75   0.386         1  0.113       0.646 1-sample proportions test with continuity correction two.sided  
like image 130
akrun Avatar answered Sep 23 '25 05:09

akrun