DRPR
DRPR

Reputation: 269

split up data frame into chunks and then apply function

I have a (big)dataset which looks like this:-

dat <- data.frame(m=c(rep("a",4),rep("b",3),rep("c",2)),
          n1 =round(rnorm(mean = 20,sd = 10,n = 9)))

g <- rnorm(20,10,5)


dat
  m     n1
1 a 15.132
2 a 17.723
3 a  3.958
4 a 19.239
5 b 11.417
6 b 12.583
7 b 32.946
8 c 11.970
9 c 26.447

I want to perform a t-test on each category of "m" with vectorg like

n1.a <- c(15.132,17.723,3.958,19.329)

I need to do a t-test like t.test(n1.a,g)

I initially thought about breaking them up into list using split(dat,dat$m) and then use lapply, but it is not working .

Any thoughts on how to go about it ?

Upvotes: 0

Views: 902

Answers (2)

Matt
Matt

Reputation: 7385

Here's a tidyverse solution using map from purrr:

dat %>% 
  split(.$m) %>% 
  map(~ t.test(.x$n1, g), data = .x$n1)

Or, using lapply as you mentioned, which will store all of your t-test statistics in a list (or a shorter version using by, thanks @markus):

dat <- split(dat, dat$m)
dat <- lapply(dat, function(x) t.test(x$n1, g))

Or

dat <- by(dat, m, function(x) t.test(x$n1, g))

Which gives us:

  $a

    Welch Two Sample t-test

data:  .x$n1 and g
t = 1.5268, df = 3.0809, p-value = 0.2219
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -11.61161  33.64902
sample estimates:
mean of x mean of y 
  21.2500   10.2313 


$b

    Welch Two Sample t-test

data:  .x$n1 and g
t = 1.8757, df = 2.2289, p-value = 0.1883
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -7.325666 20.863073
sample estimates:
mean of x mean of y 
  17.0000   10.2313 


$c

    Welch Two Sample t-test

data:  .x$n1 and g
t = 10.565, df = 19, p-value = 2.155e-09
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  7.031598 10.505808
sample estimates:
mean of x mean of y 
  19.0000   10.2313 

Upvotes: 2

Ric S
Ric S

Reputation: 9247

In base R you can do

lapply(split(dat, dat$m), function(x) t.test(x$n1, g))

Output

$a

    Welch Two Sample t-test

data:  x$n1 and g
t = 1.9586, df = 3.2603, p-value = 0.1377
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -6.033451 27.819258
sample estimates:
mean of x mean of y 
  21.0000   10.1071 


$b

    Welch Two Sample t-test

data:  x$n1 and g
t = 2.3583, df = 2.3202, p-value = 0.1249
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -5.96768 25.75349
sample estimates:
mean of x mean of y 
  20.0000   10.1071 


$c

    Welch Two Sample t-test

data:  x$n1 and g
t = 13.32, df = 15.64, p-value = 6.006e-10
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 13.77913 19.00667
sample estimates:
mean of x mean of y 
  26.5000   10.1071 

Data

set.seed(1)
dat <- data.frame(m=c(rep("a",4),rep("b",3),rep("c",2)),
          n1 =round(rnorm(mean = 20,sd = 10,n = 9)))
g <- rnorm(20,10,5)

Upvotes: 1

Related Questions