Questions about DataFrame

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.

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> 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 !

Maybe the following?

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

An alternative:

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