Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

apply returns same value for all rows

I have a large dataframe and need to run t-tests on different combinations of columns (based on the column name) and ultimately get back the p-values for each row. However, when I ran the apply function on my dataframe, I get the same p-value for every single row.

Everyone says to use apply(df,1,function(x){t.test()} and I have done that, but I get the same result for each row.

I’ve simplified my code a bit here, I’ll ultimately be using grep for fetching column indices but putting in numbers has given me the same result.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Using the example data below:

> apply(test2,1, function(x) {t.test(test2[1:10], test2[11:14])$p.value})
    ABCC1     ABCC4     ABCG2      ABI1      ABL1 
0.6789077 0.6789077 0.6789077 0.6789077 0.6789077 

Example data:

test2 <- structure(list(a = c(-0.250175512841836, -0.598132059346627, 
2.36944314821787, 0.712175496367779, 0.554244500083983), b = c(-0.393946411778548, 
-1.98106284210261, -0.837040169410122, -0.375424411534213, -0.105374520148301
), c = c(0.567833193559877, 0.136364370754434, 2.50245414535438, 
-0.088430913069187, 0.893158718942739), d = c(-0.459274650062501, 
0.744994126390252, -1.28861019919615, 0.33548964839072, -0.0093089669382681
), e = c(-1.0203598678196, 0.0567193103158077, -0.311685225886462, 
-0.0739549362904639, -0.0492254321641561), f = c(-0.238227233511192, 
0.578013461518793, 0.741419593172567, -0.298619983451011, 0.224712996269695
), g = c(-0.706469879467227, 0.0437438246019302, 1.47410341528079, 
-0.150703626309354, -0.42885770081535), h = c(-0.619451795584007, 
-1.12320908559054, 2.90373539528014, 0.00703583668484074, 0.765518916949785
), i = c(-0.387644599880135, 0.176923681687052, 0.739332511375104, 
0.0960565706257697, 0.209166609859509), j = c(-0.123146246400315, 
-0.259771309210745, -1.31764790409046, -0.321380288291338, 0.688189220698862
), k = c(-0.277023010101889, 0.486542707980388, -0.621224972880764, 
0.350319335163238, 0.0708201926811306), l = c(-0.284338994897708, 
-0.58392166777083, 1.1654879751381, -0.288421241758868, -0.0324290411715858
), m = c(0.253071717309849, 0.288787254692779, -1.36850678881865, 
0.161053825472205, -0.316171510458368), n = c(0.308290287689747, 
-0.191408294902337, 0.824243786561308, -0.222951918876576, 0.277780358948823
)), row.names = c("ABCC1", "ABCC4", "ABCG2", "ABI1", "ABL1"), class = "data.frame")

>Solution :

Try:

apply(test2,1, function(x) {t.test(x[1:10], x[11:14])$p.value})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading