# Questions about DataFrame

**URL:** https://discourse.julialang.org/t/questions-about-dataframe/89813
**Category:** New to Julia
**Tags:** question, dataframes
**Created:** [November 5, 2022, 2:05pm UTC](https://discourse.julialang.org/t/questions-about-dataframe/89813 "2022-11-05T14:05:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [November 5, 2022, 2:05pm UTC](https://discourse.julialang.org/t/questions-about-dataframe/89813/1 "2022-11-05T14:05:22Z")

</div>

I have a DataFrame like the following.I want to use the each row’s first ,second col and third ,forth col to do t test.

```julia
using DataFrames,HypothesisTests
a=DataFrame(x=[1,2,3,4,5,6,7,8,9,10.2],b=[0.1,0.5,1,2,3,4,3,5,6,7],c=[0.9,0.6,0.7,6,8,4,6,7,3,1],d=[0.2,2,0.3,6,3,5,6,8,7,9])
10×4 DataFrame
 Row │ x b c d
     │ Float64 Float64 Float64 Float64
─────┼────────────────────────────────────
   1 │ 1.0 0.1 0.9 0.2
   2 │ 2.0 0.5 0.6 2.0
   3 │ 3.0 1.0 0.7 0.3
   4 │ 4.0 2.0 6.0 6.0
   5 │ 5.0 3.0 8.0 3.0
   6 │ 6.0 4.0 4.0 5.0
   7 │ 7.0 3.0 6.0 6.0
   8 │ 8.0 5.0 7.0 8.0
   9 │ 9.0 6.0 3.0 7.0
  10 │ 10.2 7.0 1.0 9.0

julia> pvalue(OneSampleTTest([1.0,0.1],[0.9,0.2]))# like the left writings in each row.
0.9999999999999999

```

If I want to do the t test for this dataframe 's 10 rows.Should I do like the following?

```julia
julia> pval=Float64[]
Float64[]

julia> for i in 1:nrow(a)
    a1=[a[i,:][1],a[i,:][2]] #if I have 100 columns,it is not a good way
    a2=[a[i,:][3],a[i,:][4]]
    pv=pvalue(OneSampleTTest(a1,a2))
    push!(pval,pv)
end

julia> pval
10-element Vector{Float64}:
 0.9999999999999999
 0.978056288767978
 0.3119165215094772
 0.20483276469913345
 0.49999999999999944
 0.7951672353008665
 0.7048327646991336
 0.7048327646991336
 0.605136913422507
 0.6362752636432489

```

I used for loop through each row of the data frame and take the elements in each row to do the t test.Are there any easy ways to do so? Thanks for helping me !

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [November 5, 2022, 2:31pm UTC](https://discourse.julialang.org/t/questions-about-dataframe/89813/2 "2022-11-05T14:31:12Z")

</div>

Maybe the following?

```julia
julia> a=DataFrame(x=[1,2,3,4,5,6,7,8,9,10.2],b=[0.1,0.5,1,2,3,4,3,5,6,7],c=[0.9,0.6,0.7,6,8,4,6,7,3,1],d=[0.2,2,0.3,6,3,5,6,8,7,9]);

julia> a.pvals = map(eachrow(a)) do row
           a1 = collect(row[1:2])
           a2 = collect(row[3:4])
           pvalue(OneSampleTTest(a1, a2))
       end
10-element Vector{Float64}:
 0.9999999999999999
 0.978056288767978
 0.3119165215094772
 0.20483276469913345
 0.49999999999999944
 0.7951672353008665
 0.7048327646991336
 0.7048327646991336
 0.605136913422507
 0.6362752636432489

julia> df = DataFrame(rand(1000, 100), :auto);

julia> df.pvals = map(eachrow(df)) do row
           a1 = collect(row[1:50])
           a2 = collect(row[51:100])
           pvalue(OneSampleTTest(a1, a2))
       end

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 5, 2022, 10:52pm UTC](https://discourse.julialang.org/t/questions-about-dataframe/89813/3 "2022-11-05T22:52:00Z")

</div>

An alternative:

```julia
@views [pvalue(OneSampleTTest(r[1:2], r[3:4])) for r in eachrow(Matrix(a))] 

```
